Welcome to Keen Software House Forums! Log in or Sign up to interact with the KSH community.
  1. You are currently browsing our forum as a guest. Create your own forum account to access all forum functionality.

Noob programming: play a sound if a group block turns on

Discussion in 'Programming (In-game)' started by Baboo85, Apr 15, 2020.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. Baboo85 Trainee Engineer

    Messages:
    3
    Hi,

    I'm a noob at programming in Space Engineers, this is the first time at all that I try to program.
    I read online many tips, guide and examples and tried to figure it out how the Space Engineers programming works I read those guides:
    https://steamcommunity.com/sharedfiles/filedetails/?id=360966557
    https://www.spaceengineerswiki.com/Programming_Guide/Action_List

    I was trying to set a program that every 1 second it checks the battery group status: if it's ON it plays a sound once, if it's off it does nothing.
    Just a "startup sound" that I would like to have in a ship.

    Here's the code:

    void Main(string argument)
    {
    if(((IMyBlockGroup)GridTerminalSystem.GetBlockGroupWithName("All Batteries")).Enabled==true)
    {
    ((IMySoundBlock)GridTerminalSystem.GetBlockWithName("Sound Block")).ApplyAction("PlaySound");
    } else {
    ((IMySoundBlock)GridTerminalSystem.GetBlockWithName("Sound Block")).ApplyAction("StopSound");
    }
    }

    The IMyBlockGroup doesn't accept the Enabled state. What can I do?

    For now I'm using the same code but with the first line with one Battery:

    if(((IMyBatteryBlock)GridTerminalSystem.GetBlockWithName("Battery Rear")).Enabled==true)

    Second problem is: how do I set it do run every second (inevitably with a timer block? There are no alternatives like "wait for 1000ms"?) and that it plays the sound only once if the battery is on?
    --- Automerge ---
    Ok solved. Basic programming skills that I hadn't (thanks to my high schools 16 years ago).

    Here's the script (not optimized I know, but working):

    int gogogo = 1;
    public Program()
    {
    Runtime.UpdateFrequency = UpdateFrequency.Update100;
    }
    public void Main()
    {
    if(((IMyBatteryBlock)GridTerminalSystem.GetBlockWithName("Battery Rear")).Enabled==true)
    {
    if(gogogo==0)
    {
    ((IMySoundBlock)GridTerminalSystem.GetBlockWithName("Sound Block")).ApplyAction("StopSound");
    ((IMySoundBlock)GridTerminalSystem.GetBlockWithName("Sound Block")).ApplyAction("PlaySound");
    gogogo = 1;
    //battery on, stop sound, play sound, set variable to already played
    }
    else
    {
    //sound already played, do nothing
    }
    }
    else
    {
    gogogo = 0;
    //battery off, set variable to play sound at battery on
    }
    }
     
    Last edited: Apr 15, 2020
  2. MarsAttacks Trainee Engineer

    Messages:
    23
    first u declare the object or a list of objects
    then in the Progam() u use GridTerminalSystem to populate those objects
    then in the main u do the logic.
     
  3. Malware Master Engineer

    Messages:
    9,860
  4. Baboo85 Trainee Engineer

    Messages:
    3
    Sorry the post merged so I think it's not visibile.
    I found myself the solution, the code works, I don't care (for now) to optimize it. It's just for me and I spent already too much time.

    Thank you anyway for the links, I'll save them and watch them next time I need.
     
Thread Status:
This last post in this thread was made more than 31 days old.