AndakRainor wrote:Are all functions of a component saved for a non minimalState object ?
If I get it right a component with a onInit function that spawns things should not belong to a minimalState object. On the other hand saving the onInit function of such a component does not make sens as it won't run more than once.
Yes. Every component property has to be saved. But a single function will not be saved more than once; the first time a reference to a function is found during serialization, the function gets serialized, then every future reference to that function is just saved as, well, a reference. (I explain the side effects as far as environment
here, but it's not really relevant for this question.)
AndakRainor wrote:Is there a way to get rid of the function after it is run ? Maybe giving it the nil value at the end of the function ?
I doubt there is any way to remove or change an onInit hook dynamically. However, you can destroy the component or the object, then it won't have to be saved anymore. Here is an example:
Code: Select all
defineObject{
name = "mine_chasm",
components = {
{
class = "Pit",
onInit = function(self)
for i=0,3 do
local dx,dy = getForward(i)
local adjacent = false
for e in self.go.map:entitiesAt(self.go.x + dx, self.go.y + dy) do
if e.name == "mine_chasm" or e.name == "mcm" then
adjacent = true
break
end
end
if not adjacent then
spawn("mine_chasm_edge", self.go.level, self.go.x, self.go.y, i, 0)
end
end
self.go:spawn("mcm")
self.go:destroy()
end,
},
},
replacesFloor = true,
placement = "floor",
editorIcon = 40,
automapTile = "chasm",
}
defineObject{
name = "mcm",
components = {
{
class = "Pit",
},
},
replacesFloor = true,
placement = "floor",
automapTile = "chasm",
minimalSaveState = true,
}
This adapts mine_chasm to be usable with minimalSaveState. This mine_chasm object destroys itself after spawning the chasm edges and an "mcm" object, which occurs at dungeon initialization, so it will never have to be saved. "mcm" is a hidden object that is just a PitComponent with an automap tile and minimalSaveState.