Jump to content

How can I get prefab code of item instance (AddPrefabPostInit)


Recommended Posts

Hi all,

I'm writing a mod to tweak the saddles in DST. I have the following configuration option:

{
        name = "saddle_basic",
        label = "Saddle uses",
        options = 
        {
            {description = "Default (5)", data = "default"},
            {description = "10", data = 2},
            {description = "15", data = 3},
            {description = "20", data = 4},
            {description = "30", data = 6},
            {description = "40", data = 8},
            {description = "50", data = 10},
            {description = "Infinite", data = 0}
        },
        default = 0,
    },

The name is exactly the prefab code (so on for war saddle, ...). Then in the tweak function:

	function TweakDurability(inst)
		local name = inst.prefab_code	-- <-- get prefab code of "inst" here
		local configData = GetModConfigData(name)	-- load config data
        -- doing tweak stuffs here
        --
        --
    end
    
    AddPrefabPostInit("saddle_basic", TweakDurability)
	AddPrefabPostInit("saddle_war", TweakDurability)
	AddPrefabPostInit("saddle_race", TweakDurability)

My question here is how to get prefab code from inst?, some thing like inst.prefab_code. Since inst.name would return the full name of game object, e.g "War Saddle" or "Glossamer Saddle".

Thanks.

Link to comment
Share on other sites

3 hours ago, theseeker028 said:

@rezecib It worked! Thanks very much!!

Though from what it's sounding like you're having a generic function that you're going to if then else tree you way through with the different prefab types.

Might I recommend making three individual functions that wrap a generic function?

Something like:

function TweakDurability(inst, var1, var2)
    inst.somevar1 = var1
    inst:somefunc1(var2)
end
local var1_basic = GetModConfigData(blahblahblah)
local var2_basic = GetModConfigData(blahblahblah)
local var1_war = GetModConfigData(blahblahblah)
local var2_war = GetModConfigData(blahblahblah)
local var1_race = GetModConfigData(blahblahblah)
local var2_race = GetModConfigData(blahblahblah)
AddPrefabPostInit("saddle_basic",
    function(inst)
        TweakDurability(inst, var1_basic, var2_basic)
    end
)
AddPrefabPostInit("saddle_war",
    function(inst)
        TweakDurability(inst, var1_war, var2_war)
    end
)
AddPrefabPostInit("saddle_race",
    function(inst)
        TweakDurability(inst, var1_race, var2_race)
    end
)

Also GetModConfigData can be cached into the mod's "global" scope on a mod's init since it wouldn't normally be possible to edit the config of a mod in-game.

Link to comment
Share on other sites

And if you want to avoid having so much repeating code, you can do something like this:

local prefabs = {"saddle_war", "saddle_race", "saddle_basic"}
for _,prefab in pairs(prefabs) do
	local var1 = GetModConfigData(prefab.."_var1")
	local var2 = GetModConfigData(prefab.."_var2")
	AddPrefabPostInit(prefab, function(inst) Tweak(inst, var1, var2)
end

 

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...