Turning a switch device on via code

I am calling MySwitch.TurnOn(Player) via code, which does change the switch state in the game, visually. However, it seems that the TurnedOnEvent is not being called, as I have a piece of code that waits for it.

If I turn that switch on via the Player interacting with it directly ingame, the piece of code that waits for it successfully runs. Why would that happen and how do I fix it?

Code that waits for the TurnedOnEvent

 WaitForAnyBombArmed()<suspends>:void=
        Print("Waiting for any bomb armed")
        AnyBombArmed.TurnedOnEvent.Await()
        Print("A bomb was armed")

Code that turns the switch on

AnyBombPlantedSwitch.TurnOn(ArmingPlayer)

Is it possible that WaitForAnyBombArmed() is called after AnyBombPlantedSwitch.TurnOn(ArmingPlayer) ? Are you sure it is even called ? Also are you sure you’re dealing with the right object, not an empty reference ?

I’m pretty sure that this is supposed to work :thinking:

I think @im_a_lama is right. This is either an order of operations issue, or more likely (and I can’t be sure without seeing all the code) that the wait event is not being called at all.

1 Like

@im_a_lama @mikezteh69 Sorry for the late reply. I don’t think this is an race condition issue, as the event wait part is called on OnBegin, and the TurnOn is being called by the player when he reaches the switch. The only thing that changed when I did the test that worked was that the TurnOn method was not called, I pressed the button as the player, then the event seems to have been triggered.

1 Like