Legacy GM New to GML Coding and having issues with a bug Fix

D

Dewarriorz

Guest
This is my code, it is supposed to spawn 5 asteroids if none are present in the game room. It spawns the initial 5 but none after that.​

if (!instance_exists(obj_astroid))
{
instance_create(random(room_width),random(room_height),obj_astroid);
instance_create(random(room_width),random(room_height),obj_astroid);
instance_create(random(room_width),random(room_height),obj_astroid);
instance_create(random(room_width),random(room_height),obj_astroid);
instance_create(random(room_width),random(room_height),obj_astroid);
}


If I use the code below it spawns asteroids continuously​

if (!instance_exists(obj_astroid))

instance_create(random(room_width),random(room_height),obj_astroid);
instance_create(random(room_width),random(room_height),obj_astroid);
instance_create(random(room_width),random(room_height),obj_astroid);
instance_create(random(room_width),random(room_height),obj_astroid);
instance_create(random(room_width),random(room_height),obj_astroid);
 
I what events have you put the code? I suspect that you have it in the Create Event, when you want it in the Step Event.

The reason the second code would work (assuming again that you have this in the Create Event of the obj_astroid) is that because you do not have {} around the instance_create lines, it will basically only run the first instance create based on the if statement, and then regardless of whether that is true or not it will call all of the remaining 4 instance_creates. You can imagine it as being like this:
Code:
if (!instance_exists(obj_astroid))
{
  instance_create(random(room_width),random(room_height),obj_astroid);
}

instance_create(random(room_width),random(room_height),obj_astroid);
instance_create(random(room_width),random(room_height),obj_astroid);
instance_create(random(room_width),random(room_height),obj_astroid);
instance_create(random(room_width),random(room_height),obj_astroid);
So, for every obj_astroid that gets created, it will run that code and ignore the first instance create in the if, and then create 4 more obj_astroid instances (each of which will call the same code and create 4 more obj_astroid instances).

Put your first, original code in the Step Event and see if that then works.
 
D

Dewarriorz

Guest
I what events have you put the code? I suspect that you have it in the Create Event, when you want it in the Step Event.

The reason the second code would work (assuming again that you have this in the Create Event of the obj_astroid) is that because you do not have {} around the instance_create lines, it will basically only run the first instance create based on the if statement, and then regardless of whether that is true or not it will call all of the remaining 4 instance_creates. You can imagine it as being like this:
Code:
if (!instance_exists(obj_astroid))
{
  instance_create(random(room_width),random(room_height),obj_astroid);
}

instance_create(random(room_width),random(room_height),obj_astroid);
instance_create(random(room_width),random(room_height),obj_astroid);
instance_create(random(room_width),random(room_height),obj_astroid);
instance_create(random(room_width),random(room_height),obj_astroid);
So, for every obj_astroid that gets created, it will run that code and ignore the first instance create in the if, and then create 4 more obj_astroid instances (each of which will call the same code and create 4 more obj_astroid instances).

Put your first, original code in the Step Event and see if that then works.
I have this code in a step event that's why I'm so lost as to how to fix the problem.
 

Rob

Member
Code:
if instance_number(obj_asteroid) == 0{
   for (var i = 0; i < 5; i ++){
      xx = irandom(room_width);
      yy = irandom(room_height);
      instance_create(xx, yy, obj_asteroid);
   }
}
 
D

Dewarriorz

Guest
Code:
if instance_number(obj_asteroid) == 0{
   for (var i = 0; i < 5; i ++){
      xx = irandom(room_width);
      yy = irandom(room_height);
      instance_create(xx, yy, obj_asteroid);
   }
}
Rob should I use this code instead or do I add it in? Sorry I'm still learning.
 
D

Dewarriorz

Guest
Do I have to set Var i for it to work? Like I know that is setting a Var but like anywhere else besides the spawner. Just asking because it still isn't working and with me still learning advanced code, I'm a bit of a novice.
 
Last edited by a moderator:

Relic

Member
Can you clarify if your intention is to spawn 5 asteroids when there are none, or to always have 5 asteroids- as in when one is destroyed another immediately takes its place?

Sorry - just reread your first post and it clearly states to spawn 5 when there are none. Rob’s code is perfect on its own - so there is likely something else going on with your game. Any reason obj_spawner might have been destroyed and is unable to run this code?
 
D

Dewarriorz

Guest
Can you clarify if your intention is to spawn 5 asteroids when there are none, or to always have 5 asteroids- as in when one is destroyed another immediately takes its place?

Sorry - just reread your first post and it clearly states to spawn 5 when there are none. Rob’s code is perfect on its own - so there is likely something else going on with your game. Any reason obj_spawner might have been destroyed and is unable to run this code?

My intention is to always have 5, I have the code in a object without a sprite in the corner of the play area.
 

Relic

Member
If you are sure the spawner object is always active, how about how the asteroids are destroyed? Are you calling instance_destroy() at some point?
 
D

Dewarriorz

Guest
Yes instance_destroy is used in the the code after the score is added.
 

Rob

Member
I was writing at on my phone as I was falling asleep so sorry if I didn't explain anything.

All "var i" does is set up a for loop and "i < 5" will run the code in the for loop 5 times.

It's exactly the same as typing the below code 5 times
Code:
instance_create(irandom(room_width), irandom(room_height), obj_asteroid)
if you want the spawner to always make sure that there's 5 asteroids then the code will need modifying bcos at the moment it will spawn 5 only when there are 0 asteroids left

What we wanna do is check if there's less than 5, and keep creating asteroids until there are 5, then stop so, in your spawners [Step] event:

Code:
if (instance_count(obj_asteroid) < 5){
   instance_create(room_width/2, room_height/2, obj_asteroid);
}
[EDIT] I forgot to say that if you check out the in game manual if will show you many useful functions like instance_count and they make a coders life much easier. Even if you glance over the functions at least you might remember them and read them properly in the future when you need it. You can press F1 to load the manual or if you middle mouse click on a function then the manual will load to that page.
 
Last edited:

samspade

Member
This is my code, it is supposed to spawn 5 asteroids if none are present in the game room. It spawns the initial 5 but none after that.​

if (!instance_exists(obj_astroid))
{
instance_create(random(room_width),random(room_height),obj_astroid);
instance_create(random(room_width),random(room_height),obj_astroid);
instance_create(random(room_width),random(room_height),obj_astroid);
instance_create(random(room_width),random(room_height),obj_astroid);
instance_create(random(room_width),random(room_height),obj_astroid);
}


If I use the code below it spawns asteroids continuously​

if (!instance_exists(obj_astroid))

instance_create(random(room_width),random(room_height),obj_astroid);
instance_create(random(room_width),random(room_height),obj_astroid);
instance_create(random(room_width),random(room_height),obj_astroid);
instance_create(random(room_width),random(room_height),obj_astroid);
instance_create(random(room_width),random(room_height),obj_astroid);
I think there is likely something else going on as both your original code (first version) and rob's code should work if not interfered with. I would recommend using the debugger to figure out what is actually happening. If the debugger is too much (and GMS 1.4's debugger isn't as easy to use as GMS 2's) then you could also do it with show debug messages. Here are a couple I would try:

  • In a step event somewhere put show_debug_message("asteroid number = " + string(instance_number(obj_asteroid))); - it might make sense to tie this to a button press like so:
Code:
if (keyboard_check_pressed(ord("1"))) {
    show_debug_message("asteroid number = " + string(instance_number(obj_asteroid)));
}
  • In the create event of obj_asteroid put show_debug_message("created asteroid = " + string(id))
  • In the destroy event of obj_asteroid put show_debug_message("destroyed asteroid = " + string(id))
Now, whenever an asteroid is created you should get a create message along with a number string. Whenever an asteroid is destroyed you should get a destroyed message along with the same number string. And when you push 1 you will get a count of the number of asteroids that exist. This will allow you to track, in real time, the creation and destruction of asteroids. In order to help with this you might want to reduce the number of asteroids you spawn to 2.

You may also want to check inheritance. If you have any objects which are children of obj_asteroid, then they will be considered when checking for obj_asteroid.
 
W

Wraithious

Guest
Can or do the asreoids leave the room if they aren't destroyed before they move off screen? And if so do you destroy them if they leave the room? Do a draw_text and draw the string(instance_number) and see if they are actually getting destroyed
 
D

Dewarriorz

Guest
I did what you suggested Wraithious and its saying 0 exist when I'm clearly looking at a few.game.PNG
 
Last edited by a moderator:
W

Wraithious

Guest
Hmm it shouldnt say 0 of you can see them, I wasnt clear in instance_number that you have to put the object's name in that function so I'll write it out properly in case you haven't used instance_number before:
Code:
draw_text(view_xview[0], view_yview[0], string(instance_number(your_asteroid_object_name)));
 
D

Dewarriorz

Guest
Hmm it shouldnt say 0 of you can see them, I wasnt clear in instance_number that you have to put the object's name in that function so I'll write it out properly in case you haven't used instance_number before:
Code:
draw_text(view_xview[0], view_yview[0], string(instance_number(your_asteroid_object_name)));
thanks for the help, I now I have 3 Asteroids floating around in lala land that never come on screen. I feel like the issue is my wrapping code but when I tried changing it to the code below it made it so all 5 wrapped into oblivion.

move_wrap(true,true, sprite_width);
move_wrap(true,true, sprite_height);
 
Last edited by a moderator:
W

Wraithious

Guest
thanks for the help, I now I have 3 Asteroids floating around in lala land that never come on screen. I feel like the issue is my wrapping code but when I tried changing it to the code below it made it so all 5 wrapped into oblivion.

move_wrap(true,true, sprite_width);
move_wrap(true,true, sprite_height);
Ah I see, you could make a limit as to how far off screen they go, like if x < -500 || y < -500 etc.. destroy them
 
A

arirish

Guest
Just want to make sure here. In your initial post you talk about an 'obj_astroid'. All the code people have given you mentions 'obj_asteroid'. Wanted to make sure it's not a simple typo error.
 
D

Dewarriorz

Guest
I goofed on my spelling when I made the object, I've been using the correct one the whole time.
 
D

Dewarriorz

Guest
This is my current code it spawns 10 asteroids amazingly

if instance_number(obj_astroid) == 0{
for (var i = 0; i < 10; i ++){
xx = irandom(room_width);
yy = irandom(room_height);
instance_create(xx, yy, obj_astroid);
}
}

this is the original code

if instance_number(obj_asteroid) == 0{
for (var i = 0; i < 5; i ++){
xx = irandom(room_width);
yy = irandom(room_height);
instance_create(xx, yy, obj_asteroid);
}
}

I'm trying to modify it to respawn at 5 Asteroids instead of 0 and nothing I've tried has worked any help would be appreciated.
 
F

Falconsoft-Industries

Guest
Try this method, see below for solution:

if instance_number(obj_asteroid) <5{
for (var i = 0; i < 5; i ++){
xx = irandom(room_width);
yy = irandom(room_height);
instance_create(xx, yy, obj_asteroid);
}
 
D

Dewarriorz

Guest
thank you for the help falconsoft, sorry I'm a visual learner but your code just taught me how to adjust my code
 
Top