How do I create a condition : on variable value changed

0 favourites
  • 4 posts
From the Asset Store
Fully commented source code/event sheet & sprites to create a space shooter game
  • 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.

  • 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:

    Set Global_var = 1"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.
  • 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.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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.

    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.

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