Copying a value on object-creation.

0 favourites
  • 13 posts
From the Asset Store
Now you can drop everything in your game with sound :)
  • 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?

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

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

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

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

  • 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'.

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

  • It should work.. 100% totally absolutely should work.

    I demand screenshot..!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I would but I don't know how to add a screenshot to these forums :(

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

  • This might be something to do with the fact that all of this is happening inside an 'On Created' event in the first place.

    What happens is, when my ship is created, it hops to the 'On created' code for the ship, which then creates an icon to accompany it. So the code above is itself inside the ships 'On created' code.

    So the ship is created, it then creates the icon, it then sets a variable to the UID of the icon, so that they stay together and then, finally, it attempts to set the icon to an animation frame, as indicated by a variable in the newly created ship. That final part is the part which doesn't work, the rest works perfectly.

    So what I have is.

    Ship : On created : System Create 'Icon' (works)

                        Set ship variable to the new icon's UID (works)

                        Set the icon's frame to a variable which is entered in that ship-type in the editor (doesn't work).

    If I remove that third line and put it into a 'System : Each tick' event, it then works fine. Meaning that the code to set the animation from the ship's variable is ok, it just won't do it during the creation process of the ship. It's as if the variable isn't assigned until after the 'On create' or something.

    I am aware of course that the code would hop out to do an extra 'On create' for the icon, except in this case, I have no code for that. Not sure if the system hops out to do it's own internal stuff, but it hops back in and completes the 2nd line with no problems, just not the third line.

  • Ah you're setting the variable in the editor. Sounds like it should work then. Trying to think. If you haven't set the animation speed to 0 it could be playing through the animation and ending on the last frame. But you'd probably be able to see that happening.

  • Ah HAH, you've hit the nail on the head, that's exactly what was happening. I couldn't tell because they all started off clumped ontop of each other, and they were all showing the final frame... since one of them (tpyical, the final, i.e TOP one) was SUPPOSED to show the final frame I couldn't see the others animating.

    Good job mate, thanks for the help :)

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