#Race conditions

1 messages · Page 1 of 1 (latest)

warm salmon
#

Context:

  • I have a brick I move by UI controls.
  • The brick emits a bool signal whenever its area enters or leaves a prohibited group.
  • The UI sets a bool value with this signal.
  • The movement function of the UI first moves the brick, then checks this bool, and if it's true, it sets the brick back to a previous position.

Issue:

When you act the UI, and the brick first enters a prohibited group, it fires the signal, but the UI checks the (currently false) bool BEFORE receiving this safety signal, so the brick stays inside. Same occurs in reverse, the UI moves the brick outside, but the safety bool is still true, the signal doesn't reach on time, and resets the brick position back inside the prohibited group. The brick is now stuck.

#

Simplified problem code:

Brick safety area:

func on_body_entered(body:Node2D):
  if node.is_in_group('Wall'):
    collision_detected.emit(true)
func on_body_exited(body:Node2D):
  if collisions.size() == 0:
    collision_detected.emit(false)

UI Controls:

func ui_set_brick_position(brick:Brick, x:float, y:float):
  var rollback:Vector2 = brick.position

  brick.position = Vector2(x, y)

  if illegal_collision_detected: # var does not update on time
    brick.position = rollback
sage rain
warm salmon
#

I have tried waiting for the tree process frame and physics frame (and both), it still doesn't reach on time

sage rain
#

I honestly have no idea if there were a way to wait for signals mid-function call, but I do know if there were I would be terrified to use it. The better way to handle this is to probably 1) put your rollback logic in the part of your code that receives the signal OR 2) check for and handle illegal movement in the part of your code where you attempt to move.

warm salmon
#

I understand, I will see how I can move things around in my code, thanks

ivory oyster
#

Put your UI at the very bottom of the tree. This will make it render on top of everything and also be the last thing that executes with all of the latest information.

warm salmon
#

Already the case

ivory oyster
#

Then how is it acting on wrong information?

#

You said it's lagging behind and using old information which wouldn't be the case if it's at the bottom of the tree

warm salmon
#

Signals kinda do whatever I guess