Repeated function execution, with one call

Discussion and feedback on Construct 2

Post » Thu Nov 06, 2014 1:53 pm

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
B
9
S
4
G
1
Posts: 210
Reputation: 1,391

Post » Thu Nov 06, 2014 2:01 pm

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
B
9
S
4
G
1
Posts: 210
Reputation: 1,391

Post » Thu Nov 06, 2014 5:43 pm

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.
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
54
S
22
G
18
Posts: 2,123
Reputation: 17,150

Post » Thu Nov 06, 2014 5:48 pm

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 :-)
B
9
S
4
G
1
Posts: 210
Reputation: 1,391

Post » Thu Nov 06, 2014 6:29 pm

You should be using Else to enforce that the value is only evaluated once, in that time-frame.
ImageImageImage
B
71
S
22
G
258
Posts: 3,791
Reputation: 143,198

Post » Thu Nov 06, 2014 6:30 pm

how is this done. I am not sure. Can you provide a short example, inline here, in pseudocode
B
9
S
4
G
1
Posts: 210
Reputation: 1,391

Post » Thu Nov 06, 2014 6:39 pm

If S=1 => Action
Else
If S=2 => Action
...
ImageImageImage
B
71
S
22
G
258
Posts: 3,791
Reputation: 143,198


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 5 guests