EVENT OneshotEvent
VARS
INT: _Flag = 0
ON
OnInit()
ACTIONS
IF "c1"
IsEqual(_Flag, 0)
THEN
Set(_Flag, 1)
CallFunction("DoSomething")
ENDIF
The function will potentially be called more than one time, because _Flag will reinitialize to zero, even though the game may have saved _Flag as 1.
Note that the behaviour of local variables in events has changed in DOS2: they are now reset to 0/null every time an event is entered.
The reason is that (also in DOS1) local variables used as parameter were assigned whenever an event was called, even if the conditions were not matched. This mainly caused problems in case you had a single handler for multiple events. E.g.
EVENT CombinedEvent
VARS
CHARACTER:_Char
ITEM:_Item
ON
OnCharacterEvent(_Char,"Event1")
OnCharacterEvent(_Item,"Event2")
ACTIONS
IF "!c1"
IsEqual(_Char,null)
THEN
// CharacterEvent called
Set(_Char,null)
ELSE
// ItemEvent called
Set(_Item,null)
ENDIF
Unlike what you may (and I did) think, in DOS1 this does not work. The reason is that whenever a CharacterEvent gets thrown, _Char will be initialised to the character. Only then the engine will check whether the event name was "Event1" and if so, it will execute the handler. It's similar for ItemEvent "Event2". This means that as soon as you have a character event that's
not "Event1" followed by an ItemEvent that
is "Event2" (or vice versa), inside the handler _Char/_Item will still have the value of the previous non-matching Character/ItemEvent that got thrown.
To avoid this problem in DOS2, all local event variables that have not been initialised by the event you are currently catching will always be set to 0/null. As a result, assigning initial values to local variables in events is now also forbidden (unless it's 0/null).
This would even be the case if _Flag was instead %Flag--defined as a global variable instead.
Even with global characters?
That is correct! And it's still the same in DOS2.
Hey Tinkerer

Thanks for the confirmation!
Hi

And you're welcome!