adventuregamestudio / ags

AGS editor and engine source code
Other
695 stars 158 forks source link

Script API: game_start is not guaranteed to run when game is loaded #1155

Open ivan-mogilko opened 3 years ago

ivan-mogilko commented 3 years ago

Problem: There's a behavior that may be easily overlooked, but in ags the game may be started by restoring a save. From what I know, this is done by passing -loadsavedgame command argument, or if loading a save from another game within multigame collection: then RunAGSGame is called followed by restoring a save in a new game context. In this case "game_start" callback is not run, and that's not a mistake in code, but a deliberate action.

This does not seem to be a common knowledge, and I doubt this is noted in the manual, but this may lead to obscure errors in the game. For instance, in the case I found, there was a plugin initialization that was not serialized in a save (one may argue that's also a plugin error though). Another example I may think of is if game_start creates or writes into a file, which then affects the game if present. Ofcourse these problems could be overcame by also handling this in "game restored" event, but like I said, I doubt many people would realize this is an issue.

Possible solutions Changing this behavior and forcing "game_start" to be run even in the above case is the direct solution, but I have doubts. Unnecessary extra initialization is not a big deal, but may this script contain something that is not supposed to be run if there's a save scheduled to be restored? E.g. does PlayVideo work in game_start?

An alternative is to implement a "game_init" or "game_load" (or find a better name) script callback to be run after game is loaded into the engine, but before "game_start", meant for guaranteed initialization, and consider "game_start" to be something that is run only when the game actually starts anew.

Are there any other potential problems and solutions related to this?

ericoporto commented 3 years ago

There's an event that is triggered when loading a save, called eEventRestoreGame

https://adventuregamestudio.github.io/ags-manual/Globalfunctions_Event.html#on_event

If there's code in the plug-in that needs to run on restore too, that's a problem with the plug-in code. In the plug-in interface there are handlers for picking up when the engine starts and when a game restore happens.

morganwillcock commented 3 years ago

I think really this is part of a larger problem of lack of opportunity for state control to be managed by the game creator. If the scripting language was able to run through hooks at key events during engine start, game start, save game restore, etc, and have the opportunity to modify resources/state then that is potentially much more flexible. It also helps with the save game situation as it would allow for fallback behaviour when loading data after the game itself has been upgraded. 'Multigame collections' would/should be handled as dynamic content extension within a single game, which also removes all the problems of where the config and translations come from.

ivan-mogilko commented 3 years ago

There's an event that is triggered when loading a save, called eEventRestoreGame

Yes, I mentioned this event in my post, but noted that the issue itself may not be realized by game developers, as it's not a common knowledge that game_start may not be called, and it's not clear that you may need to do plugin reinitialization on restore (besides this need arises only if game launches with restoring a save, not with any restore).