Jump to content

Recommended Posts

Hello, I'm trying to remove/change the characterspecific function of Wickerbottom's books in order to let my mod character use them, but I can't seem to find a way to override the MakeBook function properly. I've tried it multiple ways and I'm not sure what could be missing.

 

First attempt:

function MakeBook(name, usefn, bookuses )    local function fn(Sim)    	local inst = CreateEntity()    	local trans = inst.entity:AddTransform()    	local anim = inst.entity:AddAnimState()        local sound = inst.entity:AddSoundEmitter()        anim:SetBank("books")        anim:SetBuild("books")        anim:PlayAnimation(name)        MakeInventoryPhysics(inst)                -----------------------------------                inst:AddComponent("inspectable")        inst:AddComponent("book")        inst.components.book.onread = usefn        inst:AddComponent("characterspecific")        inst.components.characterspecific:SetOwner("facet")                inst:AddComponent("inventoryitem")        inst:AddComponent("finiteuses")        inst.components.finiteuses:SetMaxUses( bookuses )        inst.components.finiteuses:SetUses( bookuses )        inst.components.finiteuses:SetOnFinished( onfinished )        MakeSmallBurnable(inst)        MakeSmallPropagator(inst)        return inst    end    return Prefab( "common/"..name, fn, assets, prefabs) endAddPrefabPostInit("book_sleep", MakeBook)AddPrefabPostInit("book_gardening", MakeBook)AddPrefabPostInit("book_brimstone", MakeBook)AddPrefabPostInit("book_birds", MakeBook)AddPrefabPostInit("book_tentacles", MakeBook) 

Having it this way throws this error: http://i.gyazo.com/b99b29b5e73d61e4af6e27a81bb43977.png

 

Second attempt:

local function newMakeBook(name, usefn, bookuses )    local function fn(Sim)    	local inst = CreateEntity()    	local trans = inst.entity:AddTransform()    	local anim = inst.entity:AddAnimState()        local sound = inst.entity:AddSoundEmitter()        anim:SetBank("books")        anim:SetBuild("books")        anim:PlayAnimation(name)        MakeInventoryPhysics(inst)                -----------------------------------                inst:AddComponent("inspectable")        inst:AddComponent("book")        inst.components.book.onread = usefn        inst:AddComponent("characterspecific")        inst.components.characterspecific:SetOwner("facet")                inst:AddComponent("inventoryitem")        inst:AddComponent("finiteuses")        inst.components.finiteuses:SetMaxUses( bookuses )        inst.components.finiteuses:SetUses( bookuses )        inst.components.finiteuses:SetOnFinished( onfinished )        MakeSmallBurnable(inst)        MakeSmallPropagator(inst)        return inst    end    return Prefab( "common/"..name, fn, assets, prefabs) endlocal function bookPostPrefab(inst)    inst.MakeBook = newMakeBookend AddPrefabPostInit("book_sleep", bookPostPrefab)AddPrefabPostInit("book_gardening", bookPostPrefab)AddPrefabPostInit("book_brimstone", bookPostPrefab)AddPrefabPostInit("book_birds", bookPostPrefab)AddPrefabPostInit("book_tentacles", bookPostPrefab)AddPrefabPostInit("books", bookPostPrefab) -- This was an act of desperation on my part.

This way doesn't have have any visible effect at all, not even an error. Books are still removed from the character's inventory when he tries to craft them.

@Raksen MakeBook is a local function, you can't just pass that into a PostInit to override it.

You also can't access it through the inst variable, as it's not a child of inst.

I suggest you read on how post init functions work, and also on parent-child variables.

 

In this case, you can simply access the characterspecific component through the inst variable.

local function SetSpecificOwner(inst)    inst.components.characterspecific:SetOwner("your_player_prefab")endAddPrefabPostInit("book_sleep", SetSpecificOwner)-- etc..

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