Picking nth object after picking a random object from family?

0 favourites
  • 10 posts
From the Asset Store
A collection of various zombie characters sprites for creating a 2D platformer or sidescroller game
  • OK, so here's what I'm trying to achieve.

    The set of car parts are created every 5 seconds.

    Then, I want to pick a random car part of the last set that was just created.

    That part will have an action, like set animation frame to 0.

    Right now nothing is changing on screen like a different car part animation frame.

    I'm quite new to this picking, I could sure use some advice:


  • My approach would be to give the sets an identifier as they are created, so the parts have a variable and before you create a set of them, you increase a global variable by 1 and set the car parts ID variable to that global variable. So you end up with something like : 5 parts created all with ID=1, 5 parts created all with ID=2 etc

    In your current logic you are picking a random car part instance out of ALL instances that exist. You could now limit this to pick a random car part instance where car part.ID=global variable. This picks one of the 5 most recently created car parts.

    Also with your current logic it looks like you start with a family then break it down into individual object types. I don't see any reason why it couldn't be family object isSelected : set animation frame to 0. Families are used to minimize the redundant events you can see here.

  • OK thanks!

    I'm having trouble with the details though.

    I currently have this:

    Maybe I didn't understand what you meant. The car parts change, but every 5 seconds (when the function is called) it changes the part's animation frame either too late (when already on screen) or it picks a set which part was already changed.

    Thanks again.

  • You're going to have to set the carpartID for individual objects if you are creating them like this, just add the action to each object created, not for the family at the bottom.

  • Heya,

    Thanks, I did that, only now nothing is happening. The car parts don't ever change to frame 0.

    The only thing I changed is to assign the CarPartID individually to CarPartID variable like you said?


    I don't know why it's not working? Thanks!

  • If you can't share the file you'll have to do some debugging yourself. Increase the time from 5 seconds for testing. Does the global variable increase as expected? Are the car parts created using that value for their assigned variable?

  • Hi!

    You're right. I stripped my working Capx to the basics, here it is:


    I checked the variables in the debug editor, and they are changing and increasing, for every part.

    It must be something simple I'm overlooking, although I can't find it.


  • Ya it's just getting a little confused because it's all happening in the same tick. If you pull the car has defect checks into a function, and after you've created the parts add a small wait or timer before calling the function then it's fine.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Ah, with 'pull' you meant 'put'. I put the defect part into a seperate function, and added a Wait 0 seconds before I called that second function.

    Thanks for that tip.

    Hey, why did it not 'work before'? I'd like to know the logic behind it.


  • I didn't see your sample file, so I am guessing, but when you use Create Object, the new instance does not get added to C2's internal list of objects until the next top level event after the event that created it. That means that it can not be picked or referenced in other functions until then.

    (Calling a function looks like it is on a different top level event, but the function is really just extending the event that calls it, and functions don't remember any picked objects).

    you can pick a new object by its UID, so if you have to call another function to do something to the object, you have to pass the UID as a parameter, and use that to pick it. Using instance variables, or anything else wont work immediately after the object was created.

    adding a "Wait 0 seconds" tells C2 to not execute actions after the wait until the end of the current tick - and by then new instances are available to be pick using all the other normal methods.

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