loop freezes game

G

GARLIC BREAD!

Guest
I got this little piece of code in my game and when it "activates" it freezes my game


heres the code

dash_right_now = false//dashing as we speak
if (keyboard_check_pressed(ord("X"))) {
if (!place_meeting(x,y,obj_solid)) {
dash_right_now = true
while (dash_right_now = true){
vspeed = 50
if (place_meeting(x,y,obj_solid)) {
dash_right_now = false
}
}
}
}
 
if place_meeting doesn't return false, the loop will never end. Note: the instance is not actually moving during that loop, so if place_meeting returns false, it is going to continue to return false forever.
 
G

GARLIC BREAD!

Guest
so it would be like
if (place_meeting(x,y,obj_solid)) {
dash_right_now = true
}else{
dash_right_now = false
}
if place_meeting doesn't return false, the loop will never end. Note: the instance is not actually moving during that loop, so if place_meeting returns false, it is going to continue to return false forever.
 

Bayesian

Member
while (dash_right_now = true){
vspeed = 50
if (place_meeting(x,y,obj_solid)) {
dash_right_now = false
}
}
This is not how you use while. A while loop executes in one frame until its expression is invalidated. So you're starting your while loop when dash_right_now = true and then the frame never ends crashing your game. Just do if dash_right_now = true vspeed = 50. You're also checking for place_meeting at your current x and y which would require obj_solid to be inside you. You should check plus or minis 1 in the direction you want.
 
G

GARLIC BREAD!

Guest
t
This is not how you use while. A while loop executes in one frame until its expression is invalidated. So you're starting your while loop when dash_right_now = true and then the frame never ends crashing your game. Just do if dash_right_now = true vspeed = 50. You're also checking for place_meeting at your current x and y which would require obj_solid to be inside you. You should check plus or minis 1 in the direction you want.
this kinda helps, like the player has a jolt of energy to the ground but I'm trying to do it like a constant speed till he touches the ground
 

Bayesian

Member
like the player has a jolt of energy to the ground but I'm trying to do it like a constant speed till he touches the ground
That's because you have dash_right_now = false at the start of the step. so dash_right_now will only ever be true for one frame instead of every frame until you hit the ground.
 
Top