Godot Version
4.5
Question
The script I’m writing needs to check if the mouse is in a certain spot, rotate the camera 90 degrees, then wait until it’s outside of that spot for the previous code to run again. As far as I can tell you can only do that with signals, but I can’t figure out how on earth they work.
` var gameWidth: float = get_window().get_size().x
var mousepos = get_viewport().get_mouse_position()
var mousex = mousepos.x
var camy = neck.rotation.y
signal abletoturn()
if mousepos.x > ((gameWidth/10)*9):
print("turnright")
neck.rotation.y += deg_to_rad(90)
if mousepos.x < ((gameWidth/10)*1):
print("turnleft")
neck.rotation.y -= deg_to_rad(90)
await abletoturn
print("canmoveagain")
if mousepos.x < ((gameWidth/10)*2) or mousepos.x > ((gameWidth/10)*8):
abletoturn.emit()`
Are you running this in some node’s _process() or something else that loops? You probably should be. If so, I would just make discrete cases with if/elif/elif logic as follows. Your 3 cases are mutually exclusive for a single check/frame.
#pseudo code
if mouse > 90% of the game width
if abletoturn
turnright()
abletoturn = false
elif mouse < 10% of the game width
if abletoturn
turnleft()
abletoturn = false
elif mouse between 20% and 80% of game width
set abletoturn = true
I guess this fails if you manage to move (in one from frame) from one side of the screen all the way to the other 10% block. You could fix that by having two separate abletoturn booleans (one for left and one for right, and then detatch the last elif block and turn it into an independent if/elif for mouse > 20% and mouse <80% enabling the opposite rotation.
1 Like
You don’t need signals or await for this, a simple bool variable should do. It needs to be declared outside of the function, so it can store its value.
Also, your last if-condition seems incorrect for what I think you want to do.
var able_to_turn := true
#...
if able_to_turn and mousepos.x > ((gameWidth/10)*9):
print("turnright")
neck.rotation.y += deg_to_rad(90)
able_to_turn = false
elif able_to_turn and mousepos.x < ((gameWidth/10)*1):
print("turnleft")
neck.rotation.y -= deg_to_rad(90)
able_to_turn = false
elif mousepos.x > ((gameWidth/10)*2) and mousepos.x < ((gameWidth/10)*8):
able_to_turn = true
1 Like
thanks so much, worked perfectly.
I’ve been losing my mind over programming camera movement for this for agesss 