Repeated function execution, with one call

  • Hi,

    I've implemented a state machine using functions. Calling a function indicates an event, and causes actions that change the state. Each function includes as condition a specific state, so that the function is only be executed in a specific state -- all shown below. Oddly, the function F1 is executed twice, the first time when S is 1, and then when during the execution of actions the state is set to 2 (S=2), the function is executed again, and S is set to 3, even though the client only made one call.

    This is rather intuitive behavior, at least to me. Am I misunderstanding how function calls work?

    thanks,

    Dan

    F1/S=1 => Action 1, Action 2, S=2

    F1/S=2=> Action 3, Action 4, S=3

    Client code:

    Event -> S=1, F1

  • OK.

    Although, I still don't understand the behavior of function calls, i solved the problem by not sharing the state between the condition in the function declaration and the action. Instead, I pass the current state as parameter to the function and test the parameter for state

    F1, Arg(0)=1 => Action1, Action 2, S=2

    F1, Arg(0)=2 => Action, 3, Action 4, S=3

    Like this setting, say, S=2 in the first line, doesn't' trigger the second function

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 2 cases I think of:well, when you call a function, all his occurences of said function will be read and exectuted from top to bottom, most likely, the function ends, then the trigger is reread (since I think you have two times the function call), and the first call changed the value of the parameter, thus the second time will work too.

    other case, you have one call, but have sub events "S=1" "S=2".

    passing the parameter actually will change the value of S too, but since you do not test the value of S directly, the problem is gone. hope you understand.

  • Thanks.

    It seems from your answer, that if I reverse the sequence of function definition in the event sheet, to ensure that the next function doesn't satisfy the changed S value, the problem may also go away, also. Although, it wouldn't be a good idea -- since the code correctness depends on an implementation detail of how event processing works. If one day, the runtime is changed, the code will stop working, and no one will know why

  • You should be using Else to enforce that the value is only evaluated once, in that time-frame.

  • how is this done. I am not sure. Can you provide a short example, inline here, in pseudocode

  • If S=1 => Action

    Else

    If S=2 => Action

    ...

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)