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

Get help using Construct 2

Post » Sun Jun 21, 2015 2:56 pm

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.
Follow my progress on Twitter
or in this thread Archer Devlog
B
35
S
15
G
17
Posts: 944
Reputation: 12,210

Post » Sun Jun 21, 2015 3:41 pm

[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.
B
7
S
2
Posts: 38
Reputation: 553

Post » Sun Jun 21, 2015 3:48 pm

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.
B
51
S
24
G
6
Posts: 391
Reputation: 7,820

Post » Sun Jun 21, 2015 3:50 pm

This would be easier with a CAPX XD
B
7
S
2
Posts: 38
Reputation: 553

Post » Sun Jun 21, 2015 4:02 pm

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?

Image
Follow my progress on Twitter
or in this thread Archer Devlog
B
35
S
15
G
17
Posts: 944
Reputation: 12,210

Post » Sun Jun 21, 2015 4:38 pm

Add a System EveryTick to your loop condition. That should fix it.
B
7
S
2
Posts: 38
Reputation: 553

Post » Sun Jun 21, 2015 5:06 pm

tunepunk wrote: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?

Image


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"
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Sun Jun 21, 2015 6:00 pm

LittleStain wrote:
tunepunk wrote: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?

Image


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.
Follow my progress on Twitter
or in this thread Archer Devlog
B
35
S
15
G
17
Posts: 944
Reputation: 12,210

Post » Sun Jun 21, 2015 6:13 pm

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..
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Sun Jun 21, 2015 6:19 pm

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
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Next

Return to How do I....?

Who is online

Users browsing this forum: justifun, wizdigitech, Yahoo [Bot] and 8 guests