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.

[Suggestion] Sleep / delay execution

Discussion in 'Programming Questions and Suggestions' started by Digi, Jan 2, 2015.

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

    Messages:
    2,377
    It's a bit of a pain to rely on timer blocks for any kind of delayed or repeated execution.

    We should have some sort of in-line delays that can essentially pause the code where it is for a certain amount of miliseconds then continue onward.

    With this, we would also need a "stop code" button and action for scripts and we should be able to infinitely loop a code if it contains some sort of delay between loops.
     
    Last edited by a moderator: Jan 2, 2015
  2. nmdanny Trainee Engineer

    Messages:
    23
    Definitely, I really hoped that programming would eliminate the need of having dozens of timers and such.
     
  3. fusurugi Junior Engineer

    Messages:
    521
    infinite loops are not going to happen, since they are exploitable by draining the server's cpu time.

    <personal opinion>
     
    Last edited by a moderator: Jan 2, 2015
  4. Digi Senior Engineer

    Messages:
    2,377
    You already have the option to do infinite loops using timer block, each second. The same limitation can be applied to the code version.
     
    Last edited by a moderator: Jan 2, 2015
  5. dzikakulka Trainee Engineer

    Messages:
    60
    So as of now it is impossible to duct tape a wait function that would not require stopping script? Whole 'while' statement seems unusable, I dunno how to make it otherwise...
     
  6. Moustachauve Trainee Engineer

    Messages:
    92
    They should offer us a way to execute code on each game update like they first suggested, with a way to get the elapsed time since last update. This way we could make precise timer
     
    Last edited by a moderator: Jan 3, 2015
  7. Meadea Trainee Engineer

    Messages:
    22
    Timer blocks can be activated instantly (i.e. without the delay), meaning you can make an instant loop that constantly executes programmable blocks.
     
  8. Digi Senior Engineer

    Messages:
    2,377
    It seems we can use System.Timers.Timer() to create and launch a repeatable timer... I tested it and used it in a few applications, one of them: http://steamcommunity.com/sharedfiles/filedetails/?id=368720072 which uses a timer to delay an action one time...

    It works fine but sometimes I get game crashes when I run a timer based script :/ I've sent the log to the devs, maybe they'll see what's up and maybe provide a safe delayed event function :}
     
  9. Digi Senior Engineer

    Messages:
    2,377
    Since Timer() was blocked, this suggestion still stands.

    We could really use some timed event system, a class that runs a timer once after at least 100ms and is also automatically discarded if the programmable block is out of power, destroyed or the code is recompiled.

    EDIT:
    Or at least some kind of diferent inputs for the programmable block without constantly reading block states...
     
    Last edited by a moderator: Jan 8, 2015
  10. Sirhan Blixt Apprentice Engineer

    Messages:
    449
    <del>There are plenty of ways to code around this problem. First, think about how Programmable Block code works: your starting point is the Main() method, which is called sixty times per second. You simply can't have code execution wait for any length of time, at least not for longer than it takes for the next tick to happen. That's one of the things that the "complexity limit" is supposed to prevent.

    However, you can simulate such a delay or timer by either using the real-time clock, or counting ticks. Tick count is easier, but less reliable, because you're not guaranteed to have 60 ticks per second, or ticks only 1/60 of a second apart. But if you were to do it, it'd look something like this:

    In this example, you do something every 6 ticks, or approximately 1/10 second. Since we're doing the >= comparison, we'll still catch that it's been at least six ticks since the last event. That's just bulletproofing because I am both paranoid and retentive.

    If you wanted to use the real-time clock instead, for more precise control, your code would look more like this (copied from another thread):

    Fair warning: both examples are written "by hand," without a development environment or the Programmable Block code editor. I cannot guarantee that they'll build or run, or that they're even syntactically correct. Consider them pseudocode.</del>

    All that is wrong, wrong, stupidly wrong. Forget I said it.

    A while statement is a looping construct that checks the loop condition before executing its code block. That's all it is. Not sure what else you thought it was supposed to do.
     
    Last edited by a moderator: Jan 9, 2015
  11. Digi Senior Engineer

    Messages:
    2,377
    Ehm... since when is Main() called 60 times per second ? It's called once when you execute "run".

    You can make it trigger 60 times per second with a timer block (the thing I'm trying to avoid...) by using "Trigger Now" in itself which adds significant amount of processing to the game, since it's done with blocks and not in an efficient code.

    Also you only need one variable to skip ticks and that doesn't have to be more than a simple int.

    A timer class (made specially for the game) that uses miliseconds or ticks or whatever they want would be way better than triggering the function over and over and over while checking blocks every time...
     
    Last edited by a moderator: Jan 9, 2015
  12. Sirhan Blixt Apprentice Engineer

    Messages:
    449
    Ugh--

    I had just tested that notion and had come back here to explain how faulty it was.

    Where did I get that stupid idea? From Marek Rosa's dev blog on the topic and nowhere else.

    What didn't register in my head, at the time, what this was brainstorming that had been done months ago and bore little resemblance to the programmable block that came out last week.

    So anyway, yeah-- no regular calls to Main() without a timer block.

    I've got some corrections to make. :(
     
Thread Status:
This last post in this thread was made more than 31 days old.