Jump to content

Recommended Posts

Hello again.

I'm trying to get a character to change forms during the "wild" nightmare phase down in the caves. Nothing is happening and I'm not sure what I've missed. Here's what I'm working with, borrowed and edited from the monkeys:

 

local function SetNormalKin(inst)
    inst:RemoveTag("nightmare")
        inst.AnimState:SetBuild("kin")
        inst.components.combat.damagemultiplier = 1
        inst.components.locomotor.walkspeed = 5
end

local function SetNightmareKin(inst)
    inst:AddTag("nightmare")
        inst.AnimState:SetBuild("kintransform")
        inst.components.combat.damagemultiplier = 5
        inst.components.locomotor.walkspeed = 9
end

 

local function TestNightmareArea(inst, area)
    if (TheWorld.state.isnightmarewild or TheWorld.state.isnightmaredawn)
        and inst.components.areaaware:CurrentlyInTag("Nightmare")
        and not inst:HasTag("nightmare") then

        SetNightmareKin(inst)
    elseif (not TheWorld.state.isnightmarewild and not TheWorld.state.isnightmaredawn)
        and inst:HasTag("nightmare") then

        SetNormalKin(inst)
    end
end

local function TestNightmarePhase(inst, phase)
    if (phase == "wild" or phase == "dawn")
        and inst.components.areaaware:CurrentlyInTag("Nightmare")
        and not inst:HasTag("nightmare") then

        SetNightmareKin(inst)
    elseif (phase ~= "wild" and phase ~= "dawn")
        and inst:HasTag("nightmare") then

        SetNormalKin(inst)
    end
end

 

local function master_postinit(inst)

    inst:WatchWorldState("nightmarephase", TestNightmarePhase)
    inst:ListenForEvent("changearea", TestNightmareArea)
    inst:AddComponent("areaaware")

  • Like 1

Okay, thanks. Turns out there was a conflict with another function I'd written up and tested as successful earlier, then forgot about whilst working on this one. The problem seems quite obvious, now. *facepalm* But I'm still at a loss. Is it possible to have the aforementioned transformation be triggered by two separate events? Those being the cave's wild nightmare phase and critically low sanity? In other words, I'd like a way to combine the above function with this one:

 

local function TurnShadow(inst)
        inst.AnimState:SetBuild("kintransform")
        inst.components.combat.damagemultiplier = 5
        inst.components.locomotor.walkspeed = 9
        end
        

local function TurnKin(inst)
        inst.AnimState:SetBuild("kin")
        inst.components.combat.damagemultiplier = 1
        inst.components.locomotor.walkspeed = 5
        end
        

local function OnSanityDrop(inst, data)
      if inst.components.sanity.current < 10 and not inst:HasTag("playerghost") then  
     inst.task = inst:StartThread(function() TurnShadow(inst) end)
     elseif inst.components.sanity.current > 10 and not inst:HasTag("playerghost") then  
     inst.task = inst:StartThread(function() TurnKin(inst) end)
end
end

 

local function master_postinit(inst)

inst:ListenForEvent("sanitydelta", OnSanityDrop)

Sure, but since you're threading the transformations, you might run into situations where the nightmare phase activates one transformation and sanity triggers another (or the same). You'd need to keep your character in the know about any transformation that is currently happening. You could make sure to always set a bool called TurnShadowTransformationHappening. That said, I'm not sure why you'd be threading these transformations. They seem to be instant.

What exactly is the problem?

Use inst:DoTaskInTime function instead, there's no reason to spin a thread for this.

 

As for when things should actually do the changes you can either keep a counter on the player instance and make the events only fire on changes, or use a table and see if it has any key-value pairs at all to poll if it should be in the state or not.

 

local wantstotransform = false
for _,_ in pairs(inst.mymagicalhashmap)
do
  wantstotransform = true
  break
end

if wantstotransform
then
  if not alreadytransformed
  then
    -- transform
  end
else
  if alreadytransformed
  then
    -- revert
  end
end

Alternatively you can go a little less into the abstraction to simplify the first code bit using the "next" function:

local wantstotransform = next(inst.mymagicalhashmap) ~= nil

if wantstotransform
<other code>

 

Edited by CarlZalph
Added next method for table-has-value checking

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