locked
Methods Executions RRS feed

  • Question

  • Hello everyone!

    I have a winforms program where i let the user make their scripts using lua as programming language. I have created a function c# that my users call in lua called auto(int time). When a user define auto(2000) i need that function keep executing every 2 seconds, until my user disable this function.

    My doubt is: Whats is the best way to achieve that? 

    - Timers?
    - Tasks ?
    - Others?

    And i was thinking if i can do it in background to avoid consume a lot of memory. Hope someone can help me!
    Kind regards!


    If you get your question answered, please come back and Alternate TextMark As Answer.
    Web Developer

    Tuesday, November 24, 2015 3:44 PM

Answers

  • Nah, the timer is cheap-as-free.  Don't worry about that.

    Your Lua script is a little weird.  If I read it correctly, you are calling "auto" to tell the framework that you want this script to be executed once every 1000ms.  Then you follow up that call with the code that you want executed every second.

    I'd structure it a little differently.

    I guess I have to know the context in which the original script is running, and if it is a script that sets up other callbacks or if it is the call-back itself.

    If it's the former (A script that sets up timer callbacks) then you might want to do it like this.  Have the auto function take two arguments.  1)  The interval at which to call the callback function and 2) the callback function itself.

    It's okay for the callback function to bind to p.hp or any other variables that are in the same lexical scope.  Then the embedded implementation can set a timer and stash the function away to be called when the timer expires.

    auto( 1000, function ()
       if p.hp < 90 then
          -- do something
       end
    end )
    
    
    

    alternate syntax:

    local function callback()
       if p.hp < 90 then
          -- do something
       end
    end
    
    auto( 1000, callback )


    On the other hand, if the idea is that the code is, itself, a recurring script and you just want to allow the script to decide how often it is to be called, then I would have it return how long it wants to wait before being called again.

    if p.hp < 90 then
      -- do something
    end
    return 1000

    Then the framework will call the script and expect the timer interval to come back as the return value.  If the interval changes, then you can update the timer frequency.

    Your original syntax could be used like this too where calling "auto(1000)" simply sets the current timer interval for this callback, but starting the timer and getting the script called for the first time would be done externally.

    ---

    Any of this helpful?

    • Proposed as answer by April WX Thursday, December 3, 2015 3:06 AM
    • Marked as answer by DotNet Wang Thursday, December 3, 2015 5:08 AM
    Tuesday, November 24, 2015 8:01 PM

All replies

  • The Lua contexts are not thread-safe.  If you expect auto(int time) to make use of your lua_State and also have the user running script concurrently in the same lua_State context, it will not work.  Assuming you get that part straightened this out, then a timer should be fine.

    Create a timer component, set its interval to the value passed in as time and enable the timer.

    When the user disables the function, disable the timer.

    Not sure how you think doing it "in the background" might avoid consuming more memory than some other approach.  You'll have to elaborate on what you're thinking and what you're concerned about.

    Tuesday, November 24, 2015 4:47 PM
  • Well, idk if have explained well. But let me try to show you a scenario.

    My user can add from 1 to 10 scripts on his scripts tab. Each script have his own separate work.
    I already have lua integrate with C#. so if i call for example say('message') on lua, it ill show a messagebox on application.

    What i mean is: If the user create an script like this:
    auto(1000)
    if p.hp < 90 then
      -- do something
    end

    I need to make this working for every 1 second, while script is enabled. But image that he can made 10 scripts like that. Timers will consume much memory? etc Thats was my 'memory concern'

    If you get your question answered, please come back and Alternate TextMark As Answer.
    Web Developer

    Tuesday, November 24, 2015 5:40 PM
  • Nah, the timer is cheap-as-free.  Don't worry about that.

    Your Lua script is a little weird.  If I read it correctly, you are calling "auto" to tell the framework that you want this script to be executed once every 1000ms.  Then you follow up that call with the code that you want executed every second.

    I'd structure it a little differently.

    I guess I have to know the context in which the original script is running, and if it is a script that sets up other callbacks or if it is the call-back itself.

    If it's the former (A script that sets up timer callbacks) then you might want to do it like this.  Have the auto function take two arguments.  1)  The interval at which to call the callback function and 2) the callback function itself.

    It's okay for the callback function to bind to p.hp or any other variables that are in the same lexical scope.  Then the embedded implementation can set a timer and stash the function away to be called when the timer expires.

    auto( 1000, function ()
       if p.hp < 90 then
          -- do something
       end
    end )
    
    
    

    alternate syntax:

    local function callback()
       if p.hp < 90 then
          -- do something
       end
    end
    
    auto( 1000, callback )


    On the other hand, if the idea is that the code is, itself, a recurring script and you just want to allow the script to decide how often it is to be called, then I would have it return how long it wants to wait before being called again.

    if p.hp < 90 then
      -- do something
    end
    return 1000

    Then the framework will call the script and expect the timer interval to come back as the return value.  If the interval changes, then you can update the timer frequency.

    Your original syntax could be used like this too where calling "auto(1000)" simply sets the current timer interval for this callback, but starting the timer and getting the script called for the first time would be done externally.

    ---

    Any of this helpful?

    • Proposed as answer by April WX Thursday, December 3, 2015 3:06 AM
    • Marked as answer by DotNet Wang Thursday, December 3, 2015 5:08 AM
    Tuesday, November 24, 2015 8:01 PM
  •  Any of this helpful?

    Absolutely sure that it's very helpful. I'll try to make the auto interpreter works with this callback you suggest now and see if it works. I'll post my feedback here as soon as i can!

    If you get your question answered, please come back and Alternate TextMark As Answer.
    Web Developer

    Wednesday, November 25, 2015 11:34 AM