Engine executes post-script action before finishing running callback in all modules #1144
Labels
ags 4
related to the ags4 development
context: game logic
type: bug
unexpected/erroneous behavior in the existing functionality
Problem
Within AGS script there are several commands that change overall game state drastically, and these are not executed instantly when met in script, but scheduled to be executed after current script callback finishes and returns control to the engine. Examples are: ChangeRoom, RestoreGameSlot, and few more.
However, it became apparent that these commands are still executed too early if engine is processing same callback in multiple script modules. Instead of waiting until all module callbacks are called, the scheduled commands are executed as soon as the latest callback has returned to the engine.
For example, if there's on_mouse_click in two or more script modules, and the first module's callback results in ChangeRoom call, then ChangeRoom will be executed right after first callback, a new room is loaded, but then engine continues processing callbacks in remaining modules with the same parameters, which does not make much sense anymore because the room has changed.
This can potentially lead to various obscure bugs in the game, as callback function from first modules will be run in the old room and callbacks from other modules will be run in the new room.
The reason this problem was not documented earlier is probably that it's not easy to realize, as the game must have particular setup to cause noticeable errors.
Real-life example on forums: https://www.adventuregamestudio.co.uk/forums/index.php?topic=58479.msg636629836
AGS Version
Probably, any known version. Was tested in 3.5.0.
To Reproduce
Expected behavior
I believe that the scheduled post-script actions, such as ChangeRoom, must be postponed to run only after current callback was run in all the modules. What engine is currently doing may result in an unpredictable script behavior.
Alternate solution
If engine finds the scheduled event like above, it also runs ClaimEvent after script callback... but I'm not sure if it's going to be a reliable solution.
The text was updated successfully, but these errors were encountered: