How do I call call a function on instance variable change?

  • I have a Family of objects. Whenever a particular instance variable on any of those change i want to call a function. What is the best way to go about this? I also want the function to be called once, not every tick while the value is not same.

    I tried hundreds of different things now but can't seem to grasp it. And this is how my latest try looks:

    For each "Family1"

    variable 1 = 0

    Else

    Trigger Once > call function

    I can't seem to get it to work as intended.

  • [For each "Family1"]

    [variable 1 = 0]

    set 'boolean' to false

    [Else & 'boolean' = false]

    set 'boolean' to true

    Other code here

    The '>' Are indents

    [Event]

    [Sub-event]

    'Boolean' would be a boolean (True/False) instance variable on the Family1 instance

    That should work.

    I can clarify if you don't understand, I'm bad at explaining.

  • Else

    Run if the previous event did not run. Note that this condition does not pick any objects: if it follows an event that picks objects, in the Else event all instances revert to picked again. Else can only follow normal (non-triggered) events. It can also follow another Else event with other conditions to make an "if - else if - else" chain.

    yeah i just recreated your example and I think the For each is not going to help you at all i disable the for each and the code works somewhat. I cant figure out either on how the heck to separate the sprites after using destroy function. The better question is what action are we trying to create. There may be other options to use beside function.

  • This would be easier with a CAPX XD

  • Heres an example of what Im trying to do. The problem is that the for each loop only runs once. So how do i repeat it?

  • Add a System EveryTick to your loop condition. That should fix it.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Heres an example of what Im trying to do. The problem is that the for each loop only runs once. So how do i repeat it?

    You have the trigger once condition there, right?

    If you remove it, it might not only trigger once..

    I'm also wondering why you have the else statements, by the way..

    I would just do it like this

    system pick all z-order

    z-order move to layer "infront"

    system pick z-order by comparison z-order.y+z-order.elevation > Playerbase.y

    z-order move to layer "Behind"

  • > Heres an example of what Im trying to do. The problem is that the for each loop only runs once. So how do i repeat it?

    >

    >

    >

    You have the trigger once condition there, right?

    If you remove it, it might not only trigger once..

    I'm also wondering why you have the else statements, by the way..

    I would just do it like this

    system pick all z-order

    > z-order move to layer "infront"

    system pick z-order by comparison z-order.y+z-order.elevation > Playerbase.y

    > z-order move to layer "Behind"

    Thanks. I did that before, but what im trying to do at this point is to optimize my code to use less CPU, hence the trigger once conditions. What i dont want is for values to be set, layers to changed and zSorting done every tick.... For Example: The Z sorting could be done only when an object switches layer to either infront or behind. If im not doing that the Zsorting only will use about 10-20% CPU with lots of objects on screen. Sorting smart, I managed to get it down to 0.8% CPU for sorting, by only sorting only things that needs to be sorted, Like overlapping objects, only when they need to be sorted.

  • Trigger once while true will only trigger once while true..

    Hence it only triggers once..

    I would do the z-sorting on collision with player..

    That will only trigger once if the player collides, but depending on how you've set things up, it could cause issues if you want the player to be able to walk through..

  • The easiest way to check if a variable changes:

    create a second variable called OldVariable

    system compare Variable is not OldVariable

    call function

    set OldVariable to Variable

  • Trigger once while true will only trigger once while true..

    Hence it only triggers once..

    I would do the z-sorting on collision with player..

    That will only trigger once if the player collides, but depending on how you've set things up, it could cause issues if you want the player to be able to walk through..

    Yeah, i had some issues with that too. I tried soooooo many ways to optimize now im going nuts. Haha. Even dummy objects to use a second collision box. Any way, i want to limit the CPU usage in any way in can, and right now Z Ordering is my biggest resource hog, so i have to keep trying to find ways to optimize it.

    Im gonna try using the oldValue idea there...

  • Finally!!!!!!

    I this is all i needed. Now my z ordering only uses 0.6% CPU even with LOTS of objects on the screen.

    Sharing if anyone else might need to optimize.

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