Page 1 of 1

Does anyone know a way to detect reloading of a saved game?

Posted: Tue Oct 27, 2015 12:20 am
by MrChoke
I am wondering if anyone knows how to check for this via scripts. I have heard that minmay may know. Hopefully he can respond in his usual fast way!

I need to know this now so that I can call setMaterial() again on any objects that need it after reload.

Re: Does anyone know a way to detect reloading of a saved ga

Posted: Tue Oct 27, 2015 2:28 am
by Zo Kath Ra
Every component has an onInit(self) function.
AFAIK it only gets called once, when you load a new game.

Re: Does anyone know a way to detect reloading of a saved ga

Posted: Tue Oct 27, 2015 3:09 am
by MrChoke
Zo Kath Ra wrote:Every component has an onInit(self) function.
AFAIK it only gets called once, when you load a new game.
Yes, its only called once, when you start a new game, not reload one. So it does not solve my problem.

Re: Does anyone know a way to detect reloading of a saved ga

Posted: Tue Oct 27, 2015 8:59 am
by minmay
Easiest way in Grimrock 2: minimalSaveState objects are respawned every time the game is reloaded, so they, along with their components, will return to their initial state every time the game is reloaded, which means their onInit hooks will be run again. So you'd just place one object like this one on any map:

Code: Select all

defineObject{
  name = "reload_detector",
  placement = "floor",
  minimalSaveState = true,
  components = {
    {
      class = "Null",
      onInit = function(self) -- whenever this onInit hook executes, it means the game was started or loaded
        reloadScript.script.onReload()
      end,
    },
  },
  editorIcon = 1,
}
Easiest way in Grimrock 1: The "fx" object is never saved. Spawn an "fx" object with a known id and parameters that will keep it alive (like a particle system with infinite emission time) and check for its existence every frame. If it doesn't exist on a frame, then that means the game was just loaded, and you can respawn it and run your on-reload code.

Harder, inferior way (in both games): Abuse the fact that functions are serialized with an environment of the first object encountered with a reference to them, as covered in this thread. The bizarre (yet popular among lazy programmers) Lua construct "function(a) return function() --[do something with a] end end" can probably be used as well.

Re: Does anyone know a way to detect reloading of a saved ga

Posted: Tue Oct 27, 2015 1:15 pm
by MrChoke
minmay wrote:Easiest way in Grimrock 2: minimalSaveState objects are respawned every time the game is reloaded, so they, along with their components, will return to their initial state every time the game is reloaded, which means their onInit hooks will be run again. So you'd just place one object like this one on any map:

Code: Select all

defineObject{
  name = "reload_detector",
  placement = "floor",
  minimalSaveState = true,
  components = {
    {
      class = "Null",
      onInit = function(self) -- whenever this onInit hook executes, it means the game was started or loaded
        reloadScript.script.onReload()
      end,
    },
  },
  editorIcon = 1,
}
Easiest way in Grimrock 1: The "fx" object is never saved. Spawn an "fx" object with a known id and parameters that will keep it alive (like a particle system with infinite emission time) and check for its existence every frame. If it doesn't exist on a frame, then that means the game was just loaded, and you can respawn it and run your on-reload code.

Harder, inferior way (in both games): Abuse the fact that functions are serialized with an environment of the first object encountered with a reference to them, as covered in this thread. The bizarre (yet popular among lazy programmers) Lua construct "function(a) return function() --[do something with a] end end" can probably be used as well.
Wow, that is easy. This stuff is all I need to move forward. Thanks!