How do I Create Random Objects?

Get help using Construct 2

Post » Thu Apr 13, 2017 3:01 am

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?
B
18
S
8
G
5
Posts: 30
Reputation: 3,908

Post » Thu Apr 13, 2017 3:58 am

Put your sprites into a family.

On click button
Repeat 8 times - create object family.
Mistakes were made.
B
52
S
26
G
109
Posts: 1,606
Reputation: 61,633

Post » Thu Apr 13, 2017 4:21 am

oosyrag wrote: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
B
18
S
8
G
5
Posts: 30
Reputation: 3,908

Post » Thu Apr 13, 2017 4:33 am

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.
B
33
S
10
G
2
Posts: 46
Reputation: 3,063

Post » Thu Apr 13, 2017 4:40 am

So non repeating random. This plugin may be of use plugin-smart-random_t163624?&hilit=Smart+random

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.
Mistakes were made.
B
52
S
26
G
109
Posts: 1,606
Reputation: 61,633

Post » Thu Apr 13, 2017 4:51 am

oosyrag wrote:So non repeating random. This plugin may be of use plugin-smart-random_t163624?&hilit=Smart+random

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?
B
18
S
8
G
5
Posts: 30
Reputation: 3,908

Post » Thu Apr 13, 2017 5:09 am

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:
Code: Select all
Repeat Array.Width times - Set Array at loopindex, to loopindex

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: Select all
Set PickedIndex to floor(random(Array.Width))
Set PickedNumber to Array.At(PickedIndex)
Array - Delete Index PickedIndex


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.
Mistakes were made.
B
52
S
26
G
109
Posts: 1,606
Reputation: 61,633

Post » Thu Apr 13, 2017 5:33 am

Thank you so much. Will follow this and let you know :)
B
18
S
8
G
5
Posts: 30
Reputation: 3,908

Post » Thu Apr 13, 2017 10:53 am

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.
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Sat Apr 15, 2017 12:37 am

99Instances2Go wrote: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. :(
B
18
S
8
G
5
Posts: 30
Reputation: 3,908


Return to How do I....?

Who is online

Users browsing this forum: richardfu, zenox98 and 23 guests