How do I create a condition : on variable value changed

Get help using Construct 2

Post » Thu Oct 01, 2015 8:50 am

I would like some actions to run every time a certain global variable is modified in my code, but I can't find the way to do that it in the editor. Does a condition like

"On Variable value change"

exist in Construct 2?

I can imagine creating another global variable, checking if the first variable is different from the second, then copying the first variable's value to the second at the end of my event sheet, but it seems quite complex for such an easy thing.
B
30
S
6
G
1
Posts: 350
Reputation: 2,756

Post » Thu Oct 01, 2015 9:04 am

Instead of doing it like this, can't you just make a function that you call every time you change it in the code. For instant if you need something to happen depending on its value:

Code: Select all
Set Global_var = 1
Function.call("Global var changed")


Function "Global Var Changed"
---> If Global_var = 1 <Do something>
---> If Global_var = 2 <Do something>


Then you don't have to constantly check it, because you know you only call the function if a change takes place.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,838

Post » Thu Oct 01, 2015 9:25 am

You can do that with functions as @nimos100 described. Or even a bit different - I would split that into two functions "setMyGlobal" (where you change value of your global) and "someActions". Second one should be invoked by the first one, and all logic other than setting a global variable value I would put to this second function. I find it more organized/semantic, but it obviously depends on what actions you need to do ;).

But if you need to invoke it in the "observer" way, then each observer should have it's own trigger. So the trick with variables solves the issue then.
ImageImageImage
B
27
S
16
G
74
Posts: 983
Reputation: 41,685

Post » Thu Oct 01, 2015 9:31 am

@nimos100
Your solution works perfectly and is easier than mine. Thanks a lot!
I conclude that the condition "on value changed" simply doesn't exist, but as you stated, calling a function when modifying the value is probably more efficient.

@BackendFreak
Thanks for giving further informations. In this case I'll use nimos' solution (which is already implemented and working) but will definitely keep yours in mind for the next time. ;)
B
30
S
6
G
1
Posts: 350
Reputation: 2,756


Return to How do I....?

Who is online

Users browsing this forum: cb130felix, Kermad, Lancifer, Matei511, mongoose665, piddlyPoo, yiuyiu6666 and 3 guests