Jump to content

Can you transfer a variable between modmain and player prefab?


Recommended Posts

Hello, 

I am tiding up my mod's script and I've come to this bump. 

My character can change forms when a key is pressed (so the script for that has to be in modmain), at the same time - they have a beard mechanic where they switch builds (in their prefab).

When the character switches forms, they still keep the "beard" so is there anyway to get modmain to recognise what is happening in the player prefab? I'm currently using 4 separate tags to work around this - so if I could get this down to a single variable that would be REALLY helpful. 

Link to comment
Share on other sites

Here's the code I'm trying to get to work
 

Spoiler

local function GetFeatherStage(inst)
local FeatherStage = FeatherStage
        if inst.AnimState.GetBuild == "wakes" then
        FeatherStage =0
        elseif inst.AnimState.GetBuild == "wakes_short" then
        FeatherStage =1
        elseif inst.AnimState.GetBuild == "wakes_med" then
        FeatherStage =2
        elseif inst.AnimState.GetBuild == "wakes_long" then
        FeatherStage =3
        end    
        return FeatherStage
end

local function WearHood(inst)
    local FeatherStage = GetFeatherStage(inst)
    if inst:HasTag("IsNotHooded") then ---Activate hood
        local headitem = inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HEAD)
        if inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HEAD) then
            inst.components.inventory:Unequip(GLOBAL.EQUIPSLOTS.HEAD)
            inst.components.inventory:GiveItem(headitem)
            inst.AnimState:OverrideSymbol("swap_hat", "nil", "nil")
        end
        inst.SoundEmitter:PlaySound("dontstarve/wilson/backpack_close", "open")
        inst.AnimState:SetBuild("wakes_hood")
        inst.components.temperature.inherentinsulation = (60)
        inst.components.health:SetAbsorptionAmount(0.50)
        inst:RemoveTag("IsNotHooded")    
    elseif not inst:HasTag("IsNotHooded") then ---Deactivate hood
        inst.SoundEmitter:PlaySound("dontstarve/wilson/backpack_open", "open")
        if FeatherStage == 0 then
            inst.AnimState:SetBuild("wakes")
        elseif FeatherStage == 1 then
            inst.AnimState:SetBuild("wakes_short")
        elseif FeatherStage == 2 then
            inst.AnimState:SetBuild("wakes_med")
        elseif FeatherStage == 3 then
        inst.AnimState:SetBuild("wakes_long")
        end
        inst.components.temperature.inherentinsulation = (0)
        inst.components.health:SetAbsorptionAmount(0)
        inst:AddTag("IsNotHooded")
        end
    end

 

Link to comment
Share on other sites

@Scrumch

I assume this feather stage is important and networked, so it should be done via a networked variable.

The function GetBuild from the AnimState should be called via: AnimState:GetBuild()

 

If the variable isn't needing to be networked, then you could get away with storing it onto the player reference itself via: playerinst.featherstage = #

Ensure to save this variable in the OnSave and OnLoad callbacks, see Klei code for other prefabs for that.

 

The hotkey would be able to interface with both of these methods because both of these variables should be on the local player instance.

However, I'd recommend attaching the hotkey to an RPC call if something special is supposed to happen on the server-side.

 

Further I'd study Woodie's prefab the most.  It has pretty much everything you'd need to get this up and running.

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