• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML Visual Create multiple of same instance on start (DND)

S

sparky100

Guest
Hi,

I'm brand new to GMS2 and Im stuck on something that should be quite simple. I have created a ball object and I want to create lets say 20 of these balls at random positions accross the screen at game start. I tried using a 'for loop' then create instance for x amount of iterations but it doesnt work. Any ideas what Im doing wrong?

Many thanks.
 

Slyddar

Member
There's a repeat code block, which you can use to do something a number of times. So you can use that with the instance_create code block to create multiple instances of the ball. For the x position you would use random_range(0, room_width) and random_range(0, room_height) for the y position. To make them spawn at the start, ensure you use it in a create event, or even a room_start/game_start event if that suits.
 
S

sparky100

Guest
There's a repeat code block, which you can use to do something a number of times. So you can use that with the instance_create code block to create multiple instances of the ball. For the x position you would use random_range(0, room_width) and random_range(0, room_height) for the y position. To make them spawn at the start, ensure you use it in a create event, or even a room_start/game_start event if that suits.
Hello, thanks for your help. Unfortunately it hasnt worked (see screenshot), keep getting the error below.


Screenshot 2020-07-06 at 22.32.46.png


Screenshot 2020-07-06 at 22.39.44.png
 

woods

Member
its not drag and drop.. also GMS 1.4.9999
but maybe it can give ya some insight ;o)

controller create event
ball_count = 0

controller step event
if ball_count <= 20
{
instance_create(random_range(0, room_width),random_range(0, room_height),obj_ball)
ball_count += 1
}

just place the controller object in the room.. and it makes the 20 random balls.
 
S

sparky100

Guest
its not drag and drop.. also GMS 1.4.9999
but maybe it can give ya some insight ;o)

controller create event
ball_count = 0

controller step event
if ball_count <= 20
{
instance_create(random_range(0, room_width),random_range(0, room_height),obj_ball)
ball_count += 1
}

just place the controller object in the room.. and it makes the 20 random balls.

if I try this it fills the screen with balls within a second. Cant understand why its not stopping when ball_count gets to 20?
 
Put this in your controller object's Create event:
Code:
repeat(20)instance_create(random(room_width),random(room_height),obj_ball)
I just tested it myself, works like a charm. That said, I'm running GMS 1.4, so I dunno if the repeat statement is still a thing.

EDIT: I should also mention, this creates all 20 instances of obj_ball instantly, rather than one at a time.
 
S

sparky100

Guest
Put this in your controller object's Create event:
Code:
repeat(20)instance_create(random(room_width),random(room_height),obj_ball)
I just tested it myself, works like a charm. That said, I'm running GMS 1.4, so I dunno if the repeat statement is still a thing.

EDIT: I should also mention, this creates all 20 instances of obj_ball instantly, rather than one at a time.
Thanks for the reply, this is driving me insane. I'm using IDE 2.2.5.481 so had to use instance_create_layer, but again it just fills the screen?
 

woods

Member
sparky100

if I try this it fills the screen with balls within a second. Cant understand why its not stopping when ball_count gets to 20?


i made an empty project and dropped this in there. worked just fine for me.. (again.. im on GMS1.4)
a common mistake to make is..
<= vs >=

do you have anything in the obj_ball?
make sure the ball_count = 0 is in the CREATE event, not in the step event.. that would reset the count to 0 and keep filling the room..



...brainstorming at this point ;o)
 

woods

Member
walls.. ball.. horses.. potatoes... meh ;o)
as long as you are setting your variable count in the create event, and increasing that same variable for each instance created in the step event, it SHOULD be fine.




i would doublecheck that you have ball_count or wall_count var set in the create event... its those little things that come back around to bite us in the arse
 

descrubb

Member
Hello, thanks for your help. Unfortunately it hasnt worked (see screenshot), keep getting the error below.


Screenshot 2020-07-06 at 22.32.46.png
You're creating obj_ball in the obj_ball create event.

Which means every time you create a obj_ball, you create 2 more. which is an infinite loop.


These both fill the screen with balls :(

Screenshot 2020-07-06 at 23.52.51.png

Screenshot 2020-07-06 at 23.55.16.png
Each o_wall has it's own ball_count which means for every o_wall you spawn, 20 more will be created.



You could have another object like obj_ball_spawner which is primarily used for creating balls/walls.

Then in the obj_ball_spawner create event, you would have your:
GML:
repeat(20)
{
instance_create_layer( random(room_width), random(room_height), "Instances", obj_ball)
)
Just remember to put the obj_ball_spawner in the room.
 
Last edited:

woods

Member
You're creating obj_ball in the obj_ball create event.

i missed that bit ;o)



all of this should be in the controller object... not the ball / wall object
 

Slyddar

Member
Hello, thanks for your help. Unfortunately it hasnt worked (see screenshot), keep getting the error below.
You did the right thing for Drag and Drop, but as mentioned above, it's an infinite loop. When you create a ball, it runs it's own create code, which creates 2 more balls, and each one of those creates 2 balls each, and so on. Gamemaker recognises this problem, and doesn't allow it.
Like I mentioned in my comment originally, place it in a room_start or game_start event, so it only runs once per room. I mentioned a create event too, but assumed you would know not to do it in it's own create event. Guess I shouldn't assume anything.
 
Top