Jump to content

[Question] Where to run a single change after all prefabs load


Recommended Posts

Hi, 

I am trying to edit a shared loot table (volt goats, specifically). My questions around this are thus:

1: is this table set every time a prefab loads? (does spawning a goat reset the table? Or is it set once, and once only?)

2. If the answer is the latter, where could I put my edit such that it is performed after the table has been set? I could put it in a AddPrefabPostInit function, but it seems wasteful to do that every time a goat spawns.

I might just be over complicating this, but I am genuinely interested to know how you would do this for general knowledge. Does anyone have a link handy for what systems load in what order in DST?

Link to comment
Share on other sites

1 hour ago, Bukinnear said:

2. If the answer is the latter, where could I put my edit such that it is performed after the table has been set? I could put it in a AddPrefabPostInit function, but it seems wasteful to do that every time a goat spawns.

That's exactly what AddPrefabPostInit was made for. It is not a waste. It is required. Each instance is spawned using the prefab (think of them as assembly instructions to build an instance of an entity).

Edited by Ultroman
  • Thanks 1
Link to comment
Share on other sites

Steps I took to look into the solution:

Looked at lightninggold.lua to see how the lootdropper component was getting its data.

Saw SetSharedLootTable being used to specify it.

Scanned all files to find the function definition for SetSharedLootTable.

It's declared in the lootdropper.lua component file.

See the function edits a global variable 'LootTables'.

 

So there's a few ways to go about editing the definition that shouldn't result in reapplying the changes every time due to the unique nature in how this loot table is being handled- most of the time it is on a per entity basis and as such you'd use the prefab's post init callback to do the edits.

1) Use AddClassPostConstruct for the lightninggoat class, then redefine the loot table there; should only be called once.

2) Metatable hook LootTables for __newindex and find when 'k' == 'lightninggoat' or 'chargedlightninggoat' (whichever you're editing, or both) and then edit the 'v' value, afterwards unhook the metatable hook.

3) Use another post hook callback like (1) and do it, like AddGamePostInit.

  • Thanks 2
Link to comment
Share on other sites

@Ultroman Sorry, I should have clarified that the SetSharedLootTable is a bit of a special case. It isn't set in the prefab, it's set once on construction, by the looks of it, so there's no need to override it every time a goat spawns. Just need to set & forget after everything loads.

Thanks @CarlZalph! You started that comment thinking I hadn't done my homework, didn't you lol.

That was just what I needed. I couldn't figure out if lightninggoat has a class or not (I don't see any mention to it, at least) so the AddGamePostInit worked perfectly.

Metatables are an interesting concept, though, I just learnt something new!

Link to comment
Share on other sites

4 hours ago, Bukinnear said:

@CarlZalph! You started that comment thinking I hadn't done my homework, didn't you lol.

That was just what I needed. I couldn't figure out if lightninggoat has a class or not (I don't see any mention to it, at least) so the AddGamePostInit worked perfectly.

Metatables are an interesting concept, though, I just learnt something new!

Ah yeah, I try to vary up the amount of verboseness in posts directly related to the post count of the user.  Biased, sure, but I find those with low counts are generally more new to the modding scene and could use a more of a helping hand.

As far as I'm aware every prefab has a class from the definition of the 'Prefab' function:

Prefab = Class( function(self, name, fn, assets, deps, force_path_search)

I haven't actually used a class post init for a prefab before, so now I'm wondering if it'll work as expected.  Ah, another time to test perhaps.

 

Also when looking over this today I saw a bit of old code of mine in my 'helper' blank mod to do tests, and apparently I did help out with this scenario before:

The metatable approach I'd say would be one of the last-case scenarios to try to utilize for any situation, they can get real hairy real quick especially when making them as mod-friendly to avoid conflicts with other mods doing the same.  Have to let the code be able to be ran multiple times on the same base table without failing or causing infinite recursion from meta-meta interactions.

Link to comment
Share on other sites

I don't blame you, I did only just start modding, but I can completely understand where newbies are struggling - the documentation for DS modding is piss-poor at best, and completely missing at worst (or at norm). Usually with the instruction of "Just look at how other mods work!".

One top of that, this forum is a ***** to try and search.

I can see how metatables could go wrong in a hurry, and I imagine they add a bit of overhead to the whole show as well. It might help a lot for modding things like the evergreens prefab, though, which for some inexplicable reason is almost entirely local functions/variables >:C

Link to comment
Share on other sites

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
 Share

×
  • Create New...