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

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Post Reply
MrChoke
Posts: 324
Joined: Sat Oct 25, 2014 7:20 pm

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

Post 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.
User avatar
Zo Kath Ra
Posts: 944
Joined: Sat Apr 21, 2012 9:57 am
Location: Germany

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

Post by Zo Kath Ra »

Every component has an onInit(self) function.
AFAIK it only gets called once, when you load a new game.
MrChoke
Posts: 324
Joined: Sat Oct 25, 2014 7:20 pm

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

Post 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.
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

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

Post 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.
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
MrChoke
Posts: 324
Joined: Sat Oct 25, 2014 7:20 pm

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

Post 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!
Post Reply