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.

Delay function?

Discussion in 'Programming Questions and Suggestions' started by gamma_rad, Jan 3, 2015.

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

    Messages:
    4
    I cant seem to find the Delay function.

    Thread.Sleep gives me a compilation failure. is there any way to add Delays into the script?
     
  2. Cuber Apprentice Engineer

    Messages:
    262
    At this time there's no stable way of doing it as far as I know. You can use a Timer block to call your program at intervals, instead of pausing a while loop.
    System.Timers.Timer works but it's not stable, for me it always stops the program execution after the timer has elapsed ~1000 times.
     
  3. gamma_rad Trainee Engineer

    Messages:
    4
    In that case, how do I make the timer pause them program and reactivate it at a certain point? my program is suppose to replace the message of the antenna every few seconds. making a programmable block for every message is unrealistic.
     
  4. A2K Trainee Engineer

    Messages:
    18
    There is a limit on number of instructions executed in single script run, but you can use timer block to trigger your code. Timer block minimal interval is one second, but you can set up multiple timers with time offsets. Script global variables are persisted, you can use them to count seconds.
     
  5. Cuber Apprentice Engineer

    Messages:
    262
    Could you please tell more about your idea? I could write you an example :)
     
  6. gamma_rad Trainee Engineer

    Messages:
    4
    Its probably going to be a complex code to write, even as an example but here it is:

    I want to do a numbers station (Wiki link) something akin to the Russian man or Lincolnshire Poacher. a little mystery station that pops in a message and then falls silent for a long time just to mess with people on the server, and if they manage to track down the source of the transmission and get past the bobby traps then there's a supply cache reward waiting for them.

    instead of a voice reading out those numbers, its a beacon with changing numbers. something like this
    With a second delay between each number, then a 5 second delay between a repetition/next cluster, and then turn off the beacon for some time, originally hoped for a random time between an hour and two hours but doubtful it will run so it will have a 1 hour external timer block. Maybe I'll truncate the "message" to make it display for a shorter time and make tracking harder.

    I already have a coarse code loaded which is mostly a Workshop script with tiny alternations.

    Code:
    //Parameters  
    string Original_Name = "#"; //This is the name of the Beacon initially
    string Header = "79712"; //The "Header" of the transmission
    string Message = "1805218659120321116813270"; //The "Message"
    string End = "83931"; // End signal
    string Signoff = "00000"; //Signoff
    int Delay1 = 5;
    int Delay2 = 1;
    //Paramater End
    
    //Magic starts here.
    void Main() 
    {
        Rename(Original_Name,Header); 
        Delay(Delay1); // Actual function needs to be written
        //Some function/command to discover the last digit of the previous block, store in E variable
        Rename(E,Message)
        //Repeat for the other strings
        //Trigger a 1hour timer block thats meant to repeat the program in an hour.
    }
    //Still magic... touch at your own risk, part of Rename Function
    List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();  
    
    //Rename Function based on:
    // Space Engineers Mass Rename code  
    // Licensed under GNU GPL by MSS (Mjolnir Space Syndicate)   
    void Rename(string from, string to)   
    {   
      blocks.Clear();   
      GridTerminalSystem.SearchBlocksOfName(from,blocks);  
      
      string[] blockSettings;
      for(int i=0;i<blocks.Count;i++)  
      {  
        var block = blocks[i];
        blockSettings = block.CustomName.Split(" "[0]);  
        if(blockSettings[0]==from)  
        {  
          blockSettings[0]=to; 
          block.SetCustomName(String.Join(" ",blockSettings)); 
        }  
      }  
    }
    //Trying to figure out how to make the rename be for 1 letter each time rather than the whole string at once and add a delay of 1 sec(based on Delay2 parameter) between rename updates
      
    One thing thats fairly important to me in this script is the easiness that is to change the numbers.
     
  7. Cuber Apprentice Engineer

    Messages:
    262
    Okay, I've got a working program that almost does what you want but what exactly do you mean by "make the rename be for 1 letter each time rather than the whole string at once and add a delay of 1 sec(based on Delay2 parameter) between rename updates"?
     
  8. gamma_rad Trainee Engineer

    Messages:
    4
    stringMessage="1805218659120321116813270"
    and intDelay2=1;

    From the example code will make the antenna/Beacon display

    1 then a delay of 1 second, then display 8, delay then 0 and on so. then lets say we change message to 321 and Delay1 to 5 it will rename the beacon to 3, wait 5 second, rename it to 2, wait 5 seconds, and 1. and in the end turn of the beacon off

    normal programming it should be something like

    what I had in mind and still haven't tested yet was something like this:
    Code:
    for (int i=0;i<strlen(message);i++)
    {
    Display=message[i];
    Rename(PreviousName,Display);
    Delay(Delay2)
    }
     
  9. Zourin Apprentice Engineer

    Messages:
    142
    with timer blocks, it's easy.

    1s ("heartbeat") timers are good for continuous management of ship/station systems. Every second it'll check to see if anything needs to be done or not, do it if necessary, then re-execute shortly later. You should be able to link multiple different program blocks to every pass, making individual program tweaking a bit more sane than 'all in one' code.

    If you need something more involved, then use a delay program!


    All you need is an extra program block and a timer. The delay program should check the state of the 'parent' program block. Disable the 'parent' program block and then start the delay timer on the first pass. On the second pass (parent program block is off), revert changes if needed, stop the delay timer, and re-enable the parent program block.

    Front-loaded executions can be done before the timer is triggered, delayed-action executions can be done in the delay-programs' second-pass code.
     
    Last edited by a moderator: Jan 3, 2015
  10. A2K Trainee Engineer

    Messages:
    18
    I dealt with similar problem yesterday, I needed to wait for sensors update which takes several game engine iterations. I did not need to wait to a specific amount of time, just for specific number of iterations. Do I simply called TriggerNow on timer block which called Run on my programmable block, then counted number of times my programmable block code is executed storing the count in a variable.
    If there is some way to get a timestamp, clock count or just a date, you could implement very precise delay() function using such timer block loop and checking the timestamp on every iteration.
     
Thread Status:
This last post in this thread was made more than 31 days old.