Jump to content

Character Mod Crashing [bad argument #3 to 'FindEntities' (number expected, got nil)]


Recommended Posts

Hello all,

So I really enjoyed playing the Wilburn character mod for DST (linked at end), and hope to get it working again. When I get pigmen followers, after a while I get this error.

[string "scripts/simutil.lua"]:138: bad argument #3 to 'FindEntities' (number expected, got nil)

LUA ERROR stack traceback:
=[C]:-1 in (method) FindEntities (C) <-1--1>
scripts/simutil.lua:138 in (global) GetClosestInstWithTag (Lua) <136-140>
scripts/behaviours/findlight.lua:42 in (method) PickTarget (Lua) <41-44>
scripts/behaviours/findlight.lua:18 in (method) Visit (Lua) <16-39>
scripts/behaviours/chattynode.lua:12 in (method) Visit (Lua) <9-36>
scripts/behaviourtree.lua:578 in (method) Visit (Lua) <554-610>
scripts/behaviourtree.lua:643 in (method) Visit (Lua) <633-664>
scripts/behaviourtree.lua:578 in (method) Visit (Lua) <554-610>

This is the client log:

Spoiler

[00:12:48]: Spawning player at: [Load] (307.02, 0.00, 99.11)	
[00:12:48]: ReceiveResumeNotification
[00:12:48]: Validating portal[2] <-> <nil>[2] (inactive)	
[00:12:48]: Validating portal[7] <-> <nil>[7] (inactive)	
[00:12:48]: Validating portal[10] <-> <nil>[10] (inactive)	
[00:12:49]: Deserializing tile data (425 x 425)
[00:12:49]: WARNING! Could not find region 'FROMNUM' from atlas 'FROMNUM'. Is the 

region specified in the atlas?
[00:12:49]: Looking for default texture 'FROMNUM' from atlas 'FROMNUM'.
[00:12:49]: WARNING! Could not find region 'FROMNUM' from atlas 'FROMNUM'. Is the 

region specified in the atlas?
[00:12:49]: Looking for default texture 'FROMNUM' from atlas 'FROMNUM'.
[00:13:52]: ModWorkshop::OnError: Failed getting version check details
[00:13:52]: ModWorkshop::CompleteCallback (failure, Failed getting version check 

details) empty
[00:15:52]: ModWorkshop::CompleteCallback (failure, Failed getting version check 

details) empty
[00:16:18]: [string "scripts/simutil.lua"]:138: bad argument #3 to 'FindEntities' 

(number expected, got nil)
LUA ERROR stack traceback:
=[C]:-1 in (method) FindEntities (C) <-1--1>
scripts/simutil.lua:138 in (global) GetClosestInstWithTag (Lua) <136-140>
   tag = lightsource
   inst = 101820 - pigman (valid:true)
   radius = nil
   x = 150.65083312988
   y = 5.7617548065991e-008
   z = -55.635459899902
scripts/behaviours/findlight.lua:42 in (method) PickTarget (Lua) <41-44>
   self =
      lastchecktime = 0
      lastresult = READY
      parent = table: 7181B138
      status = READY
      inst = 101820 - pigman (valid:true)
      name = FindLight
scripts/behaviours/findlight.lua:18 in (method) Visit (Lua) <16-39>
   self =
      lastchecktime = 0
      lastresult = READY
      parent = table: 7181B138
      status = READY
      inst = 101820 - pigman (valid:true)
      name = FindLight
scripts/behaviours/chattynode.lua:12 in (method) Visit (Lua) <9-36>
   self =
      parent = table: 7181B480
      name = ChattyNode
      inst = 101820 - pigman (valid:true)
      status = READY
      chatlines = table: 2226D238
      children = table: 7181B160
      lastresult = READY
   child = table: 7181B110
scripts/behaviourtree.lua:578 in (method) Visit (Lua) <554-610>
   self =
      lasttime = 209.16667757556
      name = Priority
      parent = table: 7181B5C0
      status = READY
      period = 1
      children = table: 7181AB48
      lastresult = READY
   time = 209.16667757556
   do_eval = true
   oldidx = nil
   old_event = nil
   found = false
   idx = 5
   child = table: 7181B138
   should_test_anyway = nil
scripts/behaviourtree.lua:643 in (method) Visit (Lua) <633-664>
   self =
      name = Parallel
      status = READY
      parent = table: 416EAEE8
      children = table: 7181B4A8
      lastresult = READY
   done = true
   any_done = true
   idx = 2
   child = table: 7181B480
scripts/behaviourtree.lua:578 in (method) Visit (Lua) <554-610>
   self =
      lasttime = 209.16667757556
      name = Priority
      status = READY
      period = 0.5
      children = table: 7181B610
      lastresult = FAILED
   time = 209.16667757556
   do_eval = true
   oldidx = nil
   old_event = nil
   found = false
   idx = 9
   child = table: 7181B5C0
   should_test_anyway = nil
scripts/behaviourtree.lua:22 in (method) Update (Lua) <20-27>
   self =
      root = table: 416EAEE8
      inst = 101820 - pigman (valid:true)
      forceupdate = false
scripts/brain.lua:212 in (method) OnUpdate (Lua) <205-214>
   self =
      bt = Priority - READY <FAILED> ()>0.00
   >Parallel - FAILED <READY> ()>0.00
   >   >PanicHaunted - FAILED <READY> ()>0.00
   >   >ChattyNode - READY <READY> ()>0.00
   >   >   >Panic - READY <READY> ()>0.00
   >Parallel - FAILED <READY> ()>0.00
   >   >OnFire - FAILED <READY> ()>0.00
   >   >ChattyNode - READY <READY> ()>0.00
   >   >   >Panic - READY <READY> ()>0.00
   >ChattyNode - FAILED <READY> ()>0.00
   >   >Parallel - FAILED <READY> ()>0.00
   >   >   >AttackMomentarily - SUCCESS <READY> ()>0.00
   >   >   >ChaseAndAttack - FAILED <READY> ()>0.00
   >ChattyNode - FAILED <READY> ()>0.00
   >   >Parallel - FAILED <READY> ()>0.00
   >   >   >Leader Phlegmed - FAILED <READY> ()>0.00
   >   >   >Rescue Leader - READY <READY> ()>0.00
   >ChattyNode - FAILED <READY> ()>0.00
   >   >Parallel - FAILED <READY> ()>0.00
   >   >   >Dodge - FAILED <READY> ()>0.00
   >   >   >RunAway - READY <READY> ()>0.00
   >RunAway - FAILED <READY> ()>0.00
   >ChattyNode - FAILED <READY> ()>0.00
   >   >FaceEntity - FAILED <READY> ( [**truncated**]
      events = table: 71817330
      behaviourqueue = table: 71817150
      stopped = false
      inst = 101820 - pigman (valid:true)
scripts/brain.lua:135 in (method) Update (Lua) <105-147>
   self =
      hibernaters = table: 186A1B48
      updaters = table: 186A1B20
      tickwaiters = table: 186A1AF8
      instances = table: 186A1AD0
   current_tick = 6275
   waiters = table: 7CCF2238
   k = --brain--
sleep time: 0.00
Priority - READY <FAILED> ()>0.00
   >Parallel - FAILED <READY> ()>0.00
   >   
[00:16:18]: [string "scripts/simutil.lua"]:138: bad argument #3 to 'FindEntities' 

(number expected, got nil)
LUA ERROR stack traceback:
    =[C]:-1 in (method) FindEntities (C) <-1--1>
    scripts/simutil.lua:138 in (global) GetClosestInstWithTag (Lua) <136-140>
    scripts/behaviours/findlight.lua:42 in (method) PickTarget (Lua) <41-44>
    scripts/behaviours/findlight.lua:18 in (method) Visit (Lua) <16-39>
    scripts/behaviours/chattynode.lua:12 in (method) Visit (Lua) <9-36>
    scripts/behaviourtree.lua:578 in (method) Visit (Lua) <554-610>
    scripts/behaviourtree.lua:643 in (method) Visit (Lua) <633-664>
    scripts/behaviourtree.lua:578 in (method) Visit (Lua) <554-610>
    scripts/behaviourtree.lua:22 in (method) Update (Lua) <20-27>
    scripts/brain.lua:212 in (method) OnUpdate (Lua) <205-214>
    scripts/brain.lua:135 in (method) Update (Lua) <105-147>
    scripts/update.lua:222 in () ? (Lua) <149-228>
	
[00:28:50]: Force aborting...

 

I believe it has something to do with the pigbrain code, but I'm not sure what to do to make it find an expected number. This is the pigbrain code, though:

Spoiler

require "behaviours/wander"
require "behaviours/follow"
require "behaviours/faceentity"
require "behaviours/chaseandattack"
require "behaviours/runaway"
require "behaviours/doaction"
require "behaviours/findlight"
require "behaviours/panic"
require "behaviours/chattynode"
require "behaviours/leash"


local MIN_FOLLOW_DIST = 2
local TARGET_FOLLOW_DIST = 5
local MAX_FOLLOW_DIST = 9
local MAX_WANDER_DIST = 20

local LEASH_RETURN_DIST = 10
local LEASH_MAX_DIST = 30

local START_RUN_DIST = 3
local STOP_RUN_DIST = 5
local MAX_CHASE_TIME = 10
local MAX_CHASE_DIST = 30
local SEE_LIGHT_DIST = 20
local TRADE_DIST = 20
local SEE_TREE_DIST = 15
local SEE_TARGET_DIST = 20
local SEE_FOOD_DIST = 10

local KEEP_CHOPPING_DIST = 10

local RUN_AWAY_DIST = 5
local STOP_RUN_AWAY_DIST = 8

local function ShouldRunAway(inst, target)
    return not inst.components.trader:IsTryingToTradeWithMe(target)
end

local function GetTraderFn(inst)
    local x, y, z = inst.Transform:GetWorldPosition()
    local players = FindPlayersInRange(x, y, z, TRADE_DIST, true)
    for i, v in ipairs(players) do
        if inst.components.trader:IsTryingToTradeWithMe(v) then
            return v
        end
    end
end

local function KeepTraderFn(inst, target)
    return inst.components.trader:IsTryingToTradeWithMe(target)
end

local function FindFoodAction(inst)
    local target = nil

	if inst.sg:HasStateTag("busy") then
		return
	end
    
    if inst.components.inventory and inst.components.eater then
        target = inst.components.inventory:FindItem(function(item) return inst.components.eater:CanEat(item) end)
    end
    
    local time_since_eat = inst.components.eater:TimeSinceLastEating()
    local noveggie = time_since_eat and time_since_eat < TUNING.PIG_MIN_POOP_PERIOD*4
    
    if not target and (not time_since_eat or time_since_eat > TUNING.PIG_MIN_POOP_PERIOD*2) then
        target = FindEntity(inst, SEE_FOOD_DIST, function(item) 
				if item:GetTimeAlive() < 8 then return false end
				if item.prefab == "mandrake" then return false end
				if noveggie and item.components.edible and item.components.edible.foodtype ~= FOODTYPE.MEAT then
					return false
				end
				if not item:IsOnValidGround() then
					return false
				end
				return inst.components.eater:CanEat(item) 
			end)
    end
    if target then
        return BufferedAction(inst, target, ACTIONS.EAT)
    end

    if not target and (not time_since_eat or time_since_eat > TUNING.PIG_MIN_POOP_PERIOD*2) then
        target = FindEntity(inst, SEE_FOOD_DIST, function(item) 
                if not item.components.shelf then return false end
                if not item.components.shelf.itemonshelf or not item.components.shelf.cantakeitem then return false end
                if noveggie and item.components.shelf.itemonshelf.components.edible and item.components.shelf.itemonshelf.components.edible.foodtype ~= FOODTYPE.MEAT then
                    return false
                end
                if not item:IsOnValidGround() then
                    return false
                end
                return inst.components.eater:CanEat(item.components.shelf.itemonshelf) 
            end)
    end

    if target then
        return BufferedAction(inst, target, ACTIONS.TAKEITEM)
    end

end


local function KeepChoppingAction(inst)
    return inst.components.follower.leader and inst.components.follower.leader:GetDistanceSqToInst(inst) <= KEEP_CHOPPING_DIST*KEEP_CHOPPING_DIST
end

local function StartChoppingCondition(inst)
    return inst.components.follower.leader and inst.components.follower.leader.sg and inst.components.follower.leader.sg:HasStateTag("chopping")
end


local function FindTreeToChopAction(inst)
    local target = FindEntity(inst, SEE_TREE_DIST, function(item) return item.components.workable and item.components.workable.action == ACTIONS.CHOP end)
    if target then
        return BufferedAction(inst, target, ACTIONS.CHOP)
    end
end

local function HasValidHome(inst)
    return inst.components.homeseeker and 
       inst.components.homeseeker.home and 
       inst.components.homeseeker.home:IsValid()
end

local function GoHomeAction(inst)
    if not inst.components.follower.leader and
        HasValidHome(inst) and
        not inst.components.combat.target then
            return BufferedAction(inst, inst.components.homeseeker.home, ACTIONS.GOHOME)
    end
end

local function GetLeader(inst)
    return inst.components.follower.leader 
end

local function GetHomePos(inst)
    return HasValidHome(inst) and inst.components.homeseeker:GetHomePos()
end

local function GetNoLeaderHomePos(inst)
    if GetLeader(inst) then
        return nil
    end
    return GetHomePos(inst)
end

local function RescueLeaderAction(inst)
    return BufferedAction(inst, GetLeader(inst), ACTIONS.UNPIN)
end

local function GetFaceTargetFn(inst)
    return inst.components.follower.leader
end

local function KeepFaceTargetFn(inst, target)
    return inst.components.follower.leader == target
end

local PigBrain = Class(Brain, function(self, inst)
    Brain._ctor(self, inst)
end)

-- Tests if 'Guy' has the Authority Symbol in hand.
local function GuyHasWilburnScepter(guy)
    if guy then
        return guy.components.inventory
            and guy.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
            and guy.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS).prefab == "wilburnscepter"
    else
        return false
    end
end
-- Supposed to prevent pigs from attacking your target. It doesn't work so I'm not using it.
-- (HELP NEEDED !!!)
local function NoFight(inst)
    if inst and inst.components.combat then
        inst.components.combat.target = nil
        if inst.components.combat.retargettask ~= nil then
            inst.components.combat.retargettask:Cancel()
            inst.components.combat.retargettask = nil
        end
    else
        return
    end
end

function PigBrain:OnStart()
    --print(self.inst, "PigBrain:OnStart")
    local day = WhileNode( function() return TheWorld.state.isday end, "IsDay",
        PriorityNode{
            ChattyNode(self.inst, STRINGS.PIG_TALK_FIND_MEAT,
                DoAction(self.inst, FindFoodAction )),
            IfNode(function() return StartChoppingCondition(self.inst) end, "chop", 
                WhileNode(function() return KeepChoppingAction(self.inst) end, "keep chopping",
                    LoopNode{ 
                        ChattyNode(self.inst, STRINGS.PIG_TALK_HELP_CHOP_WOOD,
                            DoAction(self.inst, FindTreeToChopAction ))})),
            ChattyNode(self.inst, STRINGS.PIG_TALK_FOLLOWWILSON, 
                Follow(self.inst, GetLeader, MIN_FOLLOW_DIST, TARGET_FOLLOW_DIST, MAX_FOLLOW_DIST)),
            IfNode(function() return GetLeader(self.inst) end, "has leader",
				ChattyNode(self.inst, STRINGS.PIG_TALK_FOLLOWWILSON,
					FaceEntity(self.inst, GetFaceTargetFn, KeepFaceTargetFn ))),

            Leash(self.inst, GetNoLeaderHomePos, LEASH_MAX_DIST, LEASH_RETURN_DIST),

            -- Added a condition to the RunAway node. It prevents pigs from running away if the player
            --has the Authority Symbol in hand. Note that creatures following you will still back off.
            ChattyNode(self.inst, STRINGS.PIG_TALK_RUNAWAY_WILSON,
                RunAway(self.inst, "player", START_RUN_DIST, STOP_RUN_DIST, function(guy) return not GuyHasWilburnScepter(guy) end)),
            ChattyNode(self.inst, STRINGS.PIG_TALK_LOOKATWILSON,
                FaceEntity(self.inst, GetFaceTargetFn, KeepFaceTargetFn)),
            Wander(self.inst, GetNoLeaderHomePos, MAX_WANDER_DIST)
        },.5)
        
    
    local night = WhileNode( function() return not TheWorld.state.isday end, "IsNight",
        PriorityNode{
            ChattyNode(self.inst, STRINGS.PIG_TALK_RUN_FROM_SPIDER,
                RunAway(self.inst, "spider", 4, 8)),
            ChattyNode(self.inst, STRINGS.PIG_TALK_FIND_MEAT,
                DoAction(self.inst, FindFoodAction )),
            RunAway(self.inst, "player", START_RUN_DIST, STOP_RUN_DIST, function(target) return ShouldRunAway(self.inst, target) end ),
            -- added the IfNode for the bell that calls pigmen out of their homes.
            -- this prevents them from going back home instantly after they get out.
            IfNode(function() return self.inst.cangobackhome end, "allowed to go home",
                ChattyNode(self.inst, STRINGS.PIG_TALK_GO_HOME,
                    DoAction(self.inst, GoHomeAction, "go home", true ))),
            ChattyNode(self.inst, STRINGS.PIG_TALK_FIND_LIGHT,
                FindLight(self.inst)),
            ChattyNode(self.inst, STRINGS.PIG_TALK_PANIC,
                Panic(self.inst)),
        },1)
    
    
    local root = 
        PriorityNode(
        {
            WhileNode( function() return self.inst.components.hauntable and self.inst.components.hauntable.panic end, "PanicHaunted", 
                ChattyNode(self.inst, STRINGS.PIG_TALK_PANICHAUNT,
                    Panic(self.inst))),
            WhileNode(function() return self.inst.components.health.takingfiredamage end, "OnFire",
				ChattyNode(self.inst, STRINGS.PIG_TALK_PANICFIRE,
					Panic(self.inst))),
            ChattyNode(self.inst, STRINGS.PIG_TALK_FIGHT,
                WhileNode( function() return self.inst.components.combat.target == nil or not self.inst.components.combat:InCooldown() end, "AttackMomentarily",
                    ChaseAndAttack(self.inst, MAX_CHASE_TIME, MAX_CHASE_DIST) )),
            ChattyNode(self.inst, STRINGS.PIG_TALK_RESCUE,
                WhileNode( function() return GetLeader(self.inst) and GetLeader(self.inst).components.pinnable and GetLeader(self.inst).components.pinnable:IsStuck() end, "Leader Phlegmed",
                    DoAction(self.inst, RescueLeaderAction, "Rescue Leader", true) )),
            ChattyNode(self.inst, STRINGS.PIG_TALK_FIGHT,
                WhileNode( function() return self.inst.components.combat.target and self.inst.components.combat:InCooldown() end, "Dodge",
                    RunAway(self.inst, function() return self.inst.components.combat.target end, RUN_AWAY_DIST, STOP_RUN_AWAY_DIST) )),
            RunAway(self.inst, function(guy) return guy:HasTag("pig") and guy.components.combat and guy.components.combat.target == self.inst end, RUN_AWAY_DIST, STOP_RUN_AWAY_DIST ),
            ChattyNode(self.inst, STRINGS.PIG_TALK_ATTEMPT_TRADE,
                FaceEntity(self.inst, GetTraderFn, KeepTraderFn)),            
            day,
            night
        }, .5)
    
    self.bt = BT(self.inst, root)
    
end

return PigBrain

 

 

 

 

Link to comment
Share on other sites

@simpliCaty The error you're getting is because when it becomes dusk and your pigmen followers start to panic that there's no light, their brains start to try and look for light and the "radius" number (or their "SEE_LIGHT_DIST" variable) isn't getting passed through, therefore it's read as nil and you get the error.

First what you're going to want to do is add this local function in somewhere with the other local functions in the mod's pigbrain.lua file:

local function SafeLightDist(inst, target)
    return (target:HasTag("player") or target:HasTag("playerlight")
            or (target.inventoryitem and target.inventoryitem:GetGrandOwner() and target.inventoryitem:GetGrandOwner():HasTag("player")))
        and 4
        or target.Light:GetCalculatedRadius() / 3
end

Then search the script for the "FindLight(self.inst))," line and add replace it with this:

--Replace

FindLight(self.inst)),

--With

FindLight(self.inst, SEE_LIGHT_DIST, SafeLightDist)),

and see if that fixes your issue.

Hope this helped :) 

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