Jump to content

Add to a character's PostInIt?


Recommended Posts

I've been under the impression you add something like this to the ModMain:

modimport("______________.lua")

and then in the aformentioned file you'd add something like this:

AddPlayerPostInit(function(inst)
	if inst.prefab == "____________" then
        --Code
	end
end)

This doesn't seem to be working though..  Am I overlooking something simple?

Link to comment
Share on other sites

@SuperMeatGoy

Hi again.  It appears to have almost worked..  The code ran through the character's prefab file, with the code imported, and went all the way through using the config variable, but failed to actually change the player's stat..  Any idea why this happened?

I can take this code and put it directly into the master_postinit and it works fine.  Is that the issue?  Master_postinit vs comon_postinit?

image.thumb.png.6d22c5a070c6c793b4711d0731cb7506.png

Edited by FurryEskimo
Link to comment
Share on other sites

Master_postinit runs on the server, common_postinit runs on both the server and the client.

I'm guessing that you're fiddling with the foodaffinity component, which means your code needs to run only on the server side.

So you need a mastersim check.

Edited by penguin0616
  • Like 2
Link to comment
Share on other sites

Sorry if I understand wrong, but can't you do like this for example?

AddPrefabPostInit("willow", function(inst)
	if inst.components.foodaffinity then
		inst.components.foodaffinity:AddPrefabAffinity("feijoada", TUNING.AFFINITY_15_CALORIES_HUGE)
	end
end)

I've been using this for my mod and seems to work without problems, of course you would need to make your changes.
 

Link to comment
Share on other sites

I think you can do like this but I didn't have time to test it properly.

local FavoriteFood = GetModConfigData("favoritefood")

AddPrefabPostInit("willow", function(inst)
	if inst.components.foodaffinity then
		inst.components.foodaffinity:AddPrefabAffinity(FavoriteFood, TUNING.AFFINITY_15_CALORIES_HUGE)
	end
end)

and in modinfo.lua

configuration_options =
{
    {
        name = "favoritefood",
        label = "Favorite Food",
        hover = "The character's favorite food.",
        options =
        {
            {description = "Meat Stew", 
			hover = "Meat Stew as favorite food.",
			data = "bonestew"},
            {description = "California Roll", 
			hover = "California Roll as favorite food.",
			data = "californiaroll"},
        },
        default = "bonestew",
    }

 

Edited by Kyno
oops :P
Link to comment
Share on other sites

@Kyno
I've already created the code, and it works fine.  The issue is that it's SO long (because there are ~170 different food items.)  I want to import this code into the character's master_postinit, rather than cause the file to become bloated with all this irritating, repetitive code.

PS- I tried making the data value the item's code name but it caused a ton of issues and didn't seem to work at all..  I created a post on the forums about configurable data with text values, but no one responded, so after a few days I deleted the post and just did it the way I knew would work.

Edited by FurryEskimo
Link to comment
Share on other sites

Ah, my bad. would this help you then?
 

-- winona.lua example
local function master_postinit(inst)
    inst.starting_inventory = start_inv[TheNet:GetServerGameMode()] or start_inv.default

    inst.components.health:SetMaxHealth(TUNING.WENDY_HEALTH)
    inst.components.hunger:SetMax(TUNING.WENDY_HUNGER)
    inst.components.sanity:SetMax(TUNING.WENDY_SANITY)

	local FavoriteFood = GetModConfigData("favoritefood", KnownModIndex:GetModActualName("YourMod"))
    inst.components.foodaffinity:AddPrefabAffinity(FavoriteFood, TUNING.AFFINITY_15_CALORIES_MED)

    inst.customidleanim = "idle_winona"

    inst.components.grue:SetResistance(1)

    if TheNet:GetServerGameMode() == "lavaarena" then
        event_server_data("lavaarena", "prefabs/winona").master_postinit(inst)
    end
end

 

Link to comment
Share on other sites

@Kyno
I wish, but I’m afraid not.  See, the trouble is that you can’t apply the same food bonus to all items, and sometimes the same food comes in various forms, such as with a spice.  When I was done writing this code it was around 800 lines long..  I took steps to reduce it, but it really is just, a lot, a lot..

I don’t want all that code in the character’s prefab file, but that’s currently where it is, and it works fine, but it makes working in the file more difficult.

 

Link to comment
Share on other sites

What penguin0616 meant was that you use it like this:

AddPrefabPostInit("your_character",function(inst)
	if not GLOBAL.TheWorld.ismastersim then
		return inst
	end
	--code
end)

If you would use it like this, it should work. You could also post the code you have to apply the food bonus if you want, perhaps I can help you reduce it a bit.

  • Like 2
  • Health 1
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...