How do I Create Random Objects?

0 favourites
  • 10 posts
From the Asset Store
Fully commented source code/event sheet & sprites to create a space shooter game
  • Hello fellow developers and helpers of this fantastic community. I have 100 sprite objects. I have a Generate button, on clicking that Generate button, 8 out of 100 sprite objects should be created. Every time user clicks generate a 8 sprite objects should be created.

    How do I achieve this?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Put your sprites into a family.

    On click button

    Repeat 8 times - create object family.

  • Put your sprites into a family.

    On click button

    Repeat 8 times - create object family.

    Thank you but now how do I make sure only unique sprites are displayed? I am getting duplicates

  • How about join those 8 sprites into a single container? This way, you can create 1 sprite and the rest will be created as well.

  • So non repeating random. This plugin may be of use

    First though, some advice setting up. Are your Sprite objects unique in that they need to be separate objects? This will be significantly simpler if you were able to use one object with 100 different animation frames as opposed to 100 different Sprite objects.

    Otherwise I think you may need an event for each unique object. Roll a value from 1 to 100, if 1, create object a, if 2 create object b, ect...

    Edit: let me know if you want to avoid using plugins, a similar way to get non repeating integers from a range can be done with arrays as well, but it will be more complicated. Still relatively simple if you are comfortable with arrays though.

  • So non repeating random. This plugin may be of use

    First though, some advice setting up. Are your Sprite objects unique in that they need to be separate objects? This will be significantly simpler if you were able to use one object with 100 different animation frames as opposed to 100 different Sprite objects.

    Otherwise I think you may need an event for each unique object. Roll a value from 1 to 100, if 1, create object a, if 2 create object b, ect...

    Edit: let me know if you want to avoid using plugins, a similar way to get non repeating integers from a range can be done with arrays as well, but it will be more complicated. Still relatively simple if you are comfortable with arrays though.

    Yes all my objects are unique. It has a cost value to it which is different. I have given each object its own variable called as cost which stores its particular cost.

    I would love to use arrays then use a plugin. How can that be achieved using arrays?

  • Create an array, width set to the size of your number pool (100), height and depth 1. Populate the array with the numbers 0-99, an easy way to do this is:

    Repeat Array.Width times - Set Array at loopindex, to loopindex[/code:37glqkyh]
    Now to pick a number out of the array, use a variable (I would make it an instance variable for the array) called PickedIndex. Also one more (global) variable to save the number that got picked.
    
    To pick a number, you can use:
    [code:37glqkyh]Set PickedIndex to floor(random(Array.Width))
    Set PickedNumber to Array.At(PickedIndex)
    Array - Delete Index PickedIndex[/code:37glqkyh]
    
    In summary, this picks an index from the array based on the size of the array (so it works even when it starts shrinking), saves the value at that index, then deletes that index so you won't be able to get that number again next time.
    
    The troublesome part for you now is to create a set of events to determine which object to spawn based on that number (there is probably a better way to do this, but it evades me at the moment). Highly recommend putting this set of events in a function you can call, with the parameter being which object to create. Note this is actually 0-99 now, rather than 1-100. You can do the latter simply by using loopindex+1 instead when populating the array.
    
    To reset the number pool, resize the array to what you want it to and refill it as done above.
  • Thank you so much. Will follow this and let you know

  • I think that using an Array (as suggested) is the best solution.

    But, lets do it 'just for the sake of it' without an array. Not as a viable solution ....

    More to explain why attempts that rely on 'picking a newly created object in the same event' have no chance to succeed.

    It is fully (for a change) commented. (but the comments have no spellchecker, so excuse me for ....)

    https://www.dropbox.com/s/kgzh1hcy8yjxp ... .capx?dl=0

    Of course, i played false, because instances holding different values in 1 instance variable, is in fact an array. And that argument is also the best argument to not shy away from using arrays.

  • I think that using an Array (as suggested) is the best solution.

    But, lets do it 'just for the sake of it' without an array. Not as a viable solution ....

    More to explain why attempts that rely on 'picking a newly created object in the same event' have no chance to succeed.

    It is fully (for a change) commented. (but the comments have no spellchecker, so excuse me for ....)

    https://www.dropbox.com/s/kgzh1hcy8yjxp ... .capx?dl=0

    Of course, i played false, because instances holding different values in 1 instance variable, is in fact an array. And that argument is also the best argument to not shy away from using arrays.

    Are you by any chance using r241 beta update? I cannot use your capx to see what you have done since I am on stable 239 version. <img src="{SMILIES_PATH}/icon_e_sad.gif" alt=":(" title="Sad">

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