How do I spawn a random unique?

  • Hey community!

    I would like to test an experiment but I don't really know how to create it in the first place and I hope perhaps someone here knows the answer :)

    So, I have 15 objects. Everytime the game level starts I want to spawn 3 random of these 15 objects, there can only by one of each object. All of the 15 objects have 1 unique item connected to itself and every time I spawn 3 random of these 15 objects I want there to be 1 unique item spawned below these 3 objects that have spawned. Does it make sense? The unique item has to be connected to one of these three that have already been spawned.

    It should be like, which one of these 3 objects owns this item? And then when you press on the correct one an animation starts and then you get a reward. Picture for visualization :P

  • Put the 15 objects into an array. Choose one at random and delete from the array. Repeat 3 times. While you are picking one at random, take another field from the row which is the related item and add it to a different array. In the second array you will have 3 items to then choose from to pick as the unique item.

  • plinkie Thanks for your response! I have no idea how to execute it but I will try and read up on Arrays and see if I can make it happen. Is it ok if I tag you in this thread if I have any questions during my test? :)

  • plinkie This was pretty advanced. Do you have any suggestion on a good tutorial or any other example I can watch to try and learn more about arrays and my specific issue?

  • plinkie Sorry for tagging you but you are the only one that has responded on my question.

    I have figured out how to put the objects into the array and how I choose and delete them after that.

    The only thing I can't figure out now is how to relate an item to the spawned object and how to add that one into another array. I can't find any tutorials going through this either. It's so frustrating to be stuck :P

  • Here is a very simple example, there are many ways of doing this, but I have tried to keep it simple with only one array.

    https://www.sizzle-games.com/array_example.c3p

  • sizcoz I will check it out immediately. I'm so happy that you took your time giving me an example :)

  • When you first add the 15 items to an array, say at Y=0, you can add the related object at Y=1. So you have Object A at 0,0 and the related object at 0,1. Object B at 1,0 and related object at 1,1. When you pick and delete the object from Y=0 you can also pick the related object at Y=1, say you are doing it in a loop which is likely, the object is at loopindex,0 and related object at loopindex,1.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • sizcoz I will check it out immediately. I'm so happy that you took your time giving me an example :)

    No worries, this example chooses 3 from 4 items, but you can obviously add more.

  • sizcoz I checked out your example. And I'm starting to get a hang of it. But what I still have a problem to figure out is how can I only spawn one of the related objects?

    For example: I have imported 15 different sprites. First I want the array to choose 3 of these 15 sprites and when that is done I want the game to choose 1 of the related items connected to that one specific character. So in this case I want the sprites to be placed in 3 different boxes (cat, horse and dog).

    When this is done I want one related item to be spawned in a different box below the 3 other boxes. The saddle for example. When the user sees the saddle they think "oh, this belongs to the horse" and when they press the horse they have finished the level. Is this possible to execute with arrays or have I done it too complicated?

  • When you first add the 15 items to an array, say at Y=0, you can add the related object at Y=1. So you have Object A at 0,0 and the related object at 0,1. Object B at 1,0 and related object at 1,1. When you pick and delete the object from Y=0 you can also pick the related object at Y=1, say you are doing it in a loop which is likely, the object is at loopindex,0 and related object at loopindex,1.

    Oh yes, this is great! But can I somehow spawn 3 of these 15 objects in 3 separate boxes away from each other and then spawn 1 of the related objects to those 3 in a fourth box below? I have 15 separate sprites for the objects and 15 separate sprites for the related object. Have I done it too complicated?

  • You don't create separate sprites but rather you have 1 sprite object copied 3 or 4 times. You then use instance variables. So when you grab each object it links to 1, 2 or 3 of an array and then you make each sprite relate to those numbers. So the 3 sprites plus the 1 one smaller sprite are always in the layout view and you change how they look based on the items you picked from the array etc. Add me on discord #4434 if you want any more help or this will just become a lot of spam.

  • You don't create separate sprites but rather you have 1 sprite object copied 3 or 4 times. You then use instance variables. So when you grab each object it links to 1, 2 or 3 of an array and then you make each sprite relate to those numbers. So the 3 sprites plus the 1 one smaller sprite are always in the layout view and you change how they look based on the items you picked from the array etc. Add me on discord #4434 if you want any more help or this will just become a lot of spam.

    Adding you now :D

  • plinkie Seems like I need your username on Discord as well. Tried with plinkie#4434 but couldn't find you :p

  • ahh ok it's ant#4434 :)

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