[Solved] How to creat random objects


#1

Suppose I have 5 sprite object (A,B,C,D,E).
I want to create sprite randomly when I press left mouse button.But how can I make events to choose the objects? And I want the sprite’s name (A,B) only spawn only once. And I don’t want to create a object over another object. But I don’t know how to do that. Any help will be great.Thanks in advance.


#2

I would use JavaScript.


#3

:fearful:

I think its possible with Pick random object command…Please help me…you know I don’t know anything about coding.


#4

May be make a variable “Random” !
On click trigger once- set value of “Random” to random in range of 1-5 ! Then if value of random =1 and no of obj 1 is <1 (if you want that object to spawn only once during the scene) --> create obj 1. if 2 and no of obj2 <1 then create obj 2 …! And like this! I think this is what you wanted and hope this works!:slight_smile:


#5

Random with javascript! first create a variable with a random value:
var rand = Math.random();
Then use it however you want. To set it to a gdevelop variable, use
runtimeScene.getVariables().get(“Variable_name”).setNumber(rand); I think.
Change Variable_name with the variable name of your choice. The variable will be a scene variable.
The number will be a random number between 0 and 1.
Examples number generated with Math.random():
0.9709515186932751
0.6778020490841714
0.945450984675839
Other possibility:
var rand = gdjs.randomInRange(minimumnumber, maximumnumber);
Replace maximum and minimum number to generate a random number between the two given.
Example:
gdjs.randomInRange(1, 7) will give either 1,2,3,4,5,6 or 7.
Use the runtime scene statement above to set it to a gdevelop variable.


#6

Thanks a loooooooooooooooooooot! I have solve the random spawn system but I didn’t understand this quout…can you tell me the action and condition names?


#7

Thanks it was helpful.


#8

Value of scene variable =…
No of objects on scene =…


#10

Bro sorry! I failed . Now I can create object Randomly. But Object (A & B) is not creating only once.
File link. Please help.


#11

THE_GEM_TUTORIALS

Help Please!


#12

Send screenshot of the events


#13

#14

If you want something to happen only once, use trigger once


#15

Do you want it to be created once per click (1)or once in the entire game/match(2)?
If you want to do (1) the put: Trigger once condition
If you want to do (2):
If the variable is 1 and you have to create obj 1:
Value of Scene variable is 1 &
No of *obj 1 * is <1
Trigger once---------->create obj 1

Note:This one will create no objects if you get variable 1 again and you already have obj 1 created before(you will have to put seperate events for that)
And if you dont want the objs to pile up on each other then just put a condition :The cursor or touch is on object (obj1,2…) and invert the condition

Hopefully this helped. :grin:

And I am sorry I dont have pc right now so I could not check the file you sent


#16

Hey Thanks …And sorry for becoming so dumb. I am having this problem When value variable is 1 again no object is created.I want what action should I use when Number of obj A & B is = 1 and Value of variable is 1 & 2.


#17

Make the variable Random in range again!! Do that cycle continues till you get 3 or 4 or…


#18

I did that but sometime I click buttone but no object created.


#19

Send screenshot of your event!
And do that event that i told you for every single object not just one!


#20


You can Ignore the red area.
I have a object group named Random and a global variable named Random.

You can have the file here


#21

I dont know what’s wrong ! Sorry but I think you should have got your result right if you did what I said! And again I am not on pc so I cant help much! Sorry