EmptyEpsilon icon indicating copy to clipboard operation
EmptyEpsilon copied to clipboard

infinite loop during scenario teardown

Open StarryWisdom opened this issue 4 years ago • 3 comments
trafficstars

@hemmond noticed a possible infinite loop during scenario teardown, a minimal example being

function create() PlayerSpaceship():setTemplate("Atlantis"):onDestroyed(create) end create()

The relevent part of the conversation about potential fixes is below

Starry: probably onDestroyed should only be called if the scenario isn't being torn down hemmond: Well... OnDestroy callback can also be used to print score of that specified ship upon scenario teardown or ship destruction. Starry: maybe during scenario shutdown creating new objects should fail? hemmond: That could work, but also could break scenarios like this which will instantiate and then print values, it will try to instantiate (and fail) and if the code will continue, it will break on the new ship control code print. hemmond: And if you combine both (badly) when you first create new ship and print its control code and then retrieve score from the old ship, it will just fail on new ship and will not give you the score... Starry: if end game stats are wanted maybe a OnScenarioEnd callback could be added and then one of the earlier breaking changes Starry: At least if it's "no objects during scenario teardown" when that breaks it's more obvious as to why. hemmond: Well... I would prefer to add getServerInfo().scenarioState and if you want to get stats when ship is destroyed and also respawn a new one when it is not during teardown, I'd like to use that scenarioState to wrap my new ship instatiation with an IF statement... hemmond: Then the "no new objects during teardown" will not hurt me. hemmond: The loop could be really nasty and hard to track down (as during teardown a lot of stuff is happening), but without a way to sanitise my code to avoid hitting this limitation, this rule can be just as bad as the loop itself. Starry: What states do you want? hemmond: Well... at least running/finished(victory/defeat)/teardown Starry: This may tie into it being awkward for the Web interface to figure out if a script has started

StarryWisdom avatar Jun 20 '21 19:06 StarryWisdom

Full example where this was found is in Chaos of War (new ship is spawned when previous is destroyed - so it spawns the rest of the ships and then immediately destroy them until it hits the limit of respawns).

hemmond avatar Jun 20 '21 19:06 hemmond

Not firing callbacks during teardown makes the most sense. As the game could be in inconsistent state during teardown.

daid avatar Jun 21 '21 09:06 daid

Full example where this was found is in Chaos of War (new ship is spawned when previous is destroyed - so it spawns the rest of the ships and then immediately destroy them until it hits the limit of respawns).

When that routine was first written, there was no respawn limit and I ran into an infinite loop. I put in the respawn limit to avoid the infinite loop. Now it at least stops when it reaches the limit.

Xansta avatar Mar 07 '22 16:03 Xansta