• 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 Trying to increase value in with statement [SOLVED]

Zabicka

Member
I'm trying to increase my vspeed of specific bullet with my global.grav variable, but it doesn't seem to work.

Step Event:
Code:
if (shoot) {
    sprite_index = sWorm_Shoot;
    with (instance_create_layer(x, y-8, "Bullets", oBullet)) {
        direction = random_range(65, 115);
        speed = random_range(1, 3);
        vspeed += global.grav; //0.2
    }
} else {
    sprite_index = sWorm_Move;
}
I need to increase vspeed for that specific created Bullet every tick in the game. I tried to put vspeed += global.grav; to bullet's step event, but it affected all bullets in the game.
 
Last edited:

Cameron

Member
Looks to me like you are creating the instance and thus only running the increment once. If you want it to increase every tick then put the incrementation code in the oBullet step event.
 
Depending on how your game is supposed to work, you could try either of these two things. Give the bullets an if statement that only increases the vspeed if the statement is true, or have an instance-level variable that is set to 0 in the Create event but is given a value in the if statement - increase the vspeed by this variable instead.
 

Zabicka

Member
Looks to me like you are creating the instance and thus only running the increment once. If you want it to increase every tick then put the incrementation code in the oBullet step event.
Thank you for your reply, it works, but I need to increase that number only for that specific created bullet. Putting that code in oBullet step event causes to affect every bullet.
 

Cameron

Member
Thank you for your reply, it works, but I need to increase that number only for that specific created bullet. Putting that code in oBullet step event causes to affect every bullet.
What makes that bullet different from the other bullets? Why is that bullet increasing from gravity every step and not the others?
 

Zabicka

Member
Depending on how your game is supposed to work, you could try either of these two things. Give the bullets an if statement that only increases the vspeed if the statement is true, or have an instance-level variable that is set to 0 in the Create event but is given a value in the if statement - increase the vspeed by this variable instead.
Yes, that works! Thank you!
 

Zabicka

Member
What makes that bullet different from the other bullets? Why is that bullet increasing from gravity every step and not the others?
I wanted for one enemy to have special bullet patern, because every enemy uses the same oBullet object. And I didn't wanted to use multiple oBullet objects, just for setting vspeed.
 

Cameron

Member
I wanted for one enemy to have special bullet patern, because every enemy uses the same oBullet object. And I didn't wanted to use multiple oBullet objects, just for setting vspeed.
One simple way is to have an additional variable, I don't know the name of your enemy so I will call it "grav_bullet". This will be a boolean variable meaning it will either be true or false. Then in the create event of the bullet set the grav_bullet to false, this initializes the variable. So, all the bullets will have this toggled as false. Like so:
Code:
grav_bullet=false;
Then when you create the bullet, your with(instance_create_layer()) statement, if the bullet is going to be the special type of bullet toggle that as true.
Then in the step event of the bullet you can say:
Code:
if grav_bullet{
     vspeed+=global.grav;
}
if the grav_bullet is fallse it will not run this code.

Edit: Looks like you have this solved now. Nevermind :)
 

Zabicka

Member
One simple way is to have an additional variable, I don't know the name of your enemy so I will call it "grav_bullet". This will be a boolean variable meaning it will either be true or false. Then in the create event of the bullet set the grav_bullet to false, this initializes the variable. So, all the bullets will have this toggled as false. Like so:
Code:
grav_bullet=false;
Then when you create the bullet, your with(instance_create_layer()) statement, if the bullet is going to be the special type of bullet toggle that as true.
Then in the step event of the bullet you can say:
Code:
if grav_bullet{
     vspeed+=global.grav;
}
if the grav_bullet is fallse it will not run this code.

Edit: Looks like you have this solved now. Nevermind :)
That works too! Thank you so much! :)
 
Top