Copying a value on object-creation.

Get help using Construct 2

Post » Fri Nov 09, 2012 11:17 pm

I have a situation where, when an object is created, it creates a matching icon, which copies the main object's UID (so I can keep the two matched up) and then sets the animation-frame of the icon, depending on a value stored in an instance variable of the created object. This is because all of the icons are single-frame and stored together as a single object, 1 to each frame (makes other parts of the program simpler).

Slightly complex explanation, but here's the bit I'm having trouble with. I do the above in an 'On created' event. The first two parts work, it does create a matching icon, it also correctly sets the variable for the icon, to the UID of the created object. But then it doesn't set the animation frame for the icon, based on the variable in the newly created object.

Only thing I can think of is, when it's inside the 'On created' event, are the new object's variables set before the actions take place? Because the variable is set in the editor but it won't transfer into the animation frame of the icon.

The only solution right now (which works but seems wasteful) is to set the animation frame every tick. Once it's got past the 'On created' event, the variables all seem to be in place.

I guess my question is, when I do an 'On created', are the instance variables which I've typed in editor immediately assigned, or aren't they assigned until after that event?farflamex2012-11-09 23:18:17
B
25
S
5
G
3
Posts: 446
Reputation: 5,633

Post » Sat Nov 10, 2012 1:23 am

This should work, i have a ton of objects that have their frame set according to a variable on the 'parent' object...
Care to post a screenshot or .capx?
B
28
S
8
G
1
Posts: 469
Reputation: 4,683

Post » Sat Nov 10, 2012 1:37 am

I'll try to work that out. It's strange, that's what's happening. My object is in a family, maybe that's part of the problem?

In typing, here's what I have....

EnemyShips(family) on Created - System : Create object 'Icon' on layer 0
                             - Icon : Set 'Match' to EnemyShips.UID
                             - Icon : Set animation frame to Enemyships.Iconframe

That's probably not padded correctly, but the gist of it is, create the icon when an object in the 'Enemyships' family is created. Set the variable 'Match' to the UID of the new object. Those two parts work.

Then set the animation frame of the new icon to the value in 'Iconframe' which is set correctly in the editor for the objects. This part doesn't work.

Maybe it's something to do with using families in this case?farflamex2012-11-10 01:39:18
B
25
S
5
G
3
Posts: 446
Reputation: 5,633

Post » Sat Nov 10, 2012 1:48 am

ohh i see now, it is slightly different to what I do..

Instead of having icon.match you could have enemyships.match and do:

+ Enemyships.match = 0
      >create object icon
      >set enemyships.match to icon.uid
      >icon set animation frame to ...

you can match them either way, it's just as easy to pick out the correct instance.. just if you do it this way you can also use the match variable as a kind of 'trigger once'.

(The reason why you don't just use trigger once in the first place is incase it needs to apply to two or more instances in one or two consecutive ticks.. that way it'll break and only apply to one of them -'For each' may fix this but i think the way above is simpler)keepee2012-11-10 01:49:52
B
28
S
8
G
1
Posts: 469
Reputation: 4,683

Post » Sat Nov 10, 2012 2:06 am

Well you're right, the other way around does work just as well, but the problem is, that part was working anyway :)

The part that isn't working is where I set the animation frame. The frame is set to a number, which is set in the editor for the newly created object, in an instance variable 'Iconframe'. It seems to always set the frame to the final frame in the sequence.

So what seems to happen is, the object is created, it creates the icon, it matches up the variable with the UID, then it tries to set the icon animation frame with the variable, but can't do so. The variable is definitely set correctly because if I set the animation frame on every tick using the exact same action, it shows correctly.farflamex2012-11-10 02:07:17
B
25
S
5
G
3
Posts: 446
Reputation: 5,633

Post » Sat Nov 10, 2012 3:03 am

I meant to include in the last comment that you should try it that way just to see if it is actually a bug with the 'on object created'.
B
28
S
8
G
1
Posts: 469
Reputation: 4,683

Post » Sat Nov 10, 2012 3:04 am

I did switch it around as you suggested, and it still works. But still no luck with the other problem. For now I'll do the update every tick, but I'll keep looking into it as it seems like it should work in the creation event.
B
25
S
5
G
3
Posts: 446
Reputation: 5,633

Post » Sat Nov 10, 2012 4:23 am

It should work.. 100% totally absolutely should work.
I demand screenshot..!
B
28
S
8
G
1
Posts: 469
Reputation: 4,683

Post » Sat Nov 10, 2012 5:14 am

I would but I don't know how to add a screenshot to these forums :(
B
25
S
5
G
3
Posts: 446
Reputation: 5,633

Post » Sat Nov 10, 2012 3:46 pm

If you're doing it like this:

System: Create EnemyShip
EnemyShip: Set Iconframe to 5

then after the create action it jumps to the 'on created' trigger and runs the code there and then goes on to the next line 'set Iconframe'. So the variable won't be set until after 'on created' runs. I'm guessing that's what's happening.
B
54
S
28
G
18
Posts: 1,520
Reputation: 25,020

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 4 guests