• 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!

GameMaker If statement not working

D

dragon_slayer25

Guest
I've been trying to make a spawner in a game and the code wont spawn anything.
in the step event
Code:
if(obj_stats.level < 5 and spawn == true) {
    alarm[0] = 30
    spawn = false
    alarm[2] = 32
}
else if(obj_stats.level < 10 and spawn == true){
    alarm[0] = room_speed * 7.5
    alarm[1] = room_speed * 10
    spawn = false
    alarm[2] = room_speed * 11
}
else if(obj_stats.level < 20 or obj_stats.level = 20 and spawn == true){
    alarm[0] = room_speed * 5
    alarm[1] = room_speed * 7.5
    spawn = false
    alarm[2] = room_speed * 10
}
else  if(obj_stats.level > 20 and spawn == true){
    alarm[0] = room_speed * 2.5
    alarm[1] = room_speed * 5
    spawn = false
    alarm[2] = room_speed * 10
}
and the level is set to 1 and the spawn is true
the alarm 0 is
Code:
slime1x = random_range(32,288)
slime1y = random_range(32,605)

instance_create_depth(slime1x,slime1y,0,obj_slime)
and spawner 1 is the same with just different object and variable name.
I dont know how to fix this problem and my game does not spawn a single slime.
 
M

MirthCastle

Guest
If that is your step event - those alarms are getting reset to the number you specify each and every step - they would never run down. Where are you setting spawn = true?

Try putting -> show_debug_message("<5") or something in each on to see if they are even being processed. Tell us if they are showing up in the console. If they are, you know your alarms are probably getting reset.

if(obj_stats.level < 20 or obj_stats.level = 20 and spawn == true)
should be:

if (obj_stats.level <= 20 and spawn == true)
 
Last edited by a moderator:

PlayerOne

Member
You are - from what I can understand - using alarms to spawn enemies?

The if statement isn't the problem, the alarm is. Once the first if statements checks as true you are continuously resetting the alarm to 30. Causing the alarms to not countdown at all.
 

chamaeleon

Member
@dragon_slayer25 I'm not going to try to figure out what the problem is, but instead tell you to get really comfortable with using the debugging tools, break points and show_debug_message(), and things like that. Sprinkle show_debug_message() calls before, inside (both at the beginning and the end if values are changed), and after if statements with pertinent information (the variables used in the if statement makes an excellent data point for this). Doing this when you run into problems could help you solve problems within in seconds or minutes depending on the scope instead of (im?)patiently waiting for someone else to do it for you. If you don't see a message you know a particular piece of code is not being executed. This means it can be useful to put a show_debug_message() at the beginning of a script or event for example, as a sanity check to ensure the code you're trying to fix is actually running.
 
D

dragon_slayer25

Guest
If that is your step event - those alarms are getting reset to the number you specify each and every step - they would never run down. Where are you setting spawn = true?

Try putting -> show_debug_message("<5") or something in each on to see if they are even being processed. Tell us if they are showing up in the console. If they are, you know your alarms are probably getting reset.



should be:

if (obj_stats.level <= 20 and spawn == true)
I am setting spawn true in the alarm 2 and i still cant get my code to function. please if you have anymore advice please help me.
 
S

Silver_Mantis

Guest
I am setting spawn true in the alarm 2 and i still cant get my code to function. please if you have anymore advice please help me.


If that is your step event - those alarms are getting reset to the number you specify each and every step - they would never run down. Where are you setting spawn = true?

Try putting -> show_debug_message("<5") or something in each on to see if they are even being processed. Tell us if they are showing up in the console. If they are, you know your alarms are probably getting reset.



should be:

if (obj_stats.level <= 20 and spawn == true)

This is not correct. The correct format would be:
Code:
if (obj_stats.level < 5) && (spawn == true)
{

}
First, change your code to that format.

Next, do not put code inside alarms. Leave them blank and perform a check within the Step Event like this:
Code:
if (!alarm[2])
{
spawn = true;
}

Try these changes, and then let's see what happens.
 

FrostyCat

Redemption Seeker
This is not correct. The correct format would be:
Code:
if (obj_stats.level < 5) && (spawn == true)
{

}
First, change your code to that format.
if (obj_stats.level <= 20 and spawn == true) is perfectly valid syntax. That's not where the problem is.

Next, do not put code inside alarms. Leave them blank and perform a check within the Step Event like this:
Code:
if (!alarm[2])
{
spawn = true;
}
Try these changes, and then let's see what happens.
I'm really sick of this "put all code in the Step event" movement, and one point I particularly dislike is this "don't use Alarm events" advice.

There is nothing wrong with using the alarm event in this situation, as long as it is used to reset the spawn flag and runs at the right time. Like your previous advice, you are trying to unteach perfectly valid technique.

Worse yet, if the original poster takes your advice as removing the alarm event altogether, that alarm would stop counting down. This is a well-known behaviour of the alarm system. You'd add to the problem instead of helping solve it.

Rookies these days seem to have a much harder time learning alarms than when I was a rookie a decade ago. One reason is this fashion of hogging all code in the Step event and making it as fat as possible, which in itself isn't a good code organization habit to start with.
 

Relic

Member
Do you only have one instance of obj_stats in your room at a time?

When you set the alarms it will be for the instance of your spawner. Is the spawning code in the right place? Did you put an instance of the spawner in the room too?

If none of these are the issue, paste in your alarm code.

I've just notices you tagged this thread GMS 2. I don't think room_speed works in studio 2. You have to use game_get_speed(gamespeed_fps) - or gamespeed_microseconds
 
M

MirthCastle

Guest
I've just notices you tagged this thread GMS 2. I don't think room_speed works in studio 2. You have to use game_get_speed(gamespeed_fps) - or gamespeed_microseconds
It works, I was just using it yesterday.

You make good points - I didn't think that the object itself does not exist in the room situation. Easy mistake by newbs and vets alike.
 
Top