Jump to content

Recommended Posts

I guess I assumed tags would be persistent when loading a world....but it appears this is not the case.

 

Basically my code is something like:

theMob = GLOBAL.SpawnPrefab("beefalo")if theMob then   theMob:AddTag("SuperSpecialThing")end

This works just fine for my current session. I can find entities with the tag "SuperSpecialThing", etc.

 

Except when I reload. 

 

The beefalo is still there...but no longer has that tag. 

 

 

So, what's the best way to keep track of these special mobs on load if not by tags? 

 

Basically, I am injecting a behavior node into their brains which is triggered whether they have this tag or not. I don't want ALL beefalo, etc to behave this way, only the ones I've created with my mod.

 

Thanks!

Well, you could do some super mad hacking like I did in "Alchemical Brewage" to keep track of entities under potion effects.

 

EDIT: Made a dummy prefab with a component that keeps a list of instances affected. The loadpostpass function is essential in this. It only fires with such a complicated setup, sadly.

Edited by Mobbstar

Ok, here's how I did it for anyone else wondering.

 

I already had a component I was modifying (hounded.lua). So I created a table to store mobs created from hounded in this table.

local function houndedComponentOverride(self)   ...   self.currentMobs = {}   ...     self.someFunction - function(self)        -- Eventually, I created the mob somewhere in here        local theMob = GLOBAL.SpawnPrefab(prefab)        -- Function to add this to the table (basically self.currentMobs[theMob] = true)        self:AddMobToTable(theMob)     end        -- HERE ARE THE IMPORTANT FUNCTIONS     local origSave = self.OnSave     self.OnSave = function(self)        data = origSave(self)                -- ONLY SAVE THE GUID of the mobs!!!        local mobs = {}        for k,v in pairs(self.currentMobs) do            saved = true            table.insert(mobs, k.GUID)        end        data.mobs = mobs        return data     end     -- This is called after the mobs are spawned by the game     self.LoadPostPass = function(self,newents,savedata)        if savedata and savedata.mobs then            for k,v in pairs(savedata.mobs) do                local targ = newents[v]                if targ then                    -- Add this mob back to our structure!!!                    self:AddMobToTable(targ.entity)                                        -- Add the tags back to this mob                    targ.entity:AddTag("superSpecialTag")                                    end            end        end     end     ... endAddComponentPostInit("hounded",houndedComponentOverride)

To summarize, 

 

Save your prefabs in a list.

 

In the OnSave, save the GUID of the prefabs into the data structure.

 

Implement LoadPostPass function. Find the mobs in the savedata and tag them again. Be sure to add them back to your structure so you can keep saving them!

 

Hope this helps.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...