Jump to content

Issue with overriding a function in books.lua


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.

Link to comment
Share on other sites

@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..
Link to comment
Share on other sites

...Wow, you know I really have no idea why I didn't just try that in the first place. Something in me told me it wouldn't work because of the MakeBook function.

 

Thanks a bunch for the help!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...