Jump to content

Error in entity script


Recommended Posts

Hello,

I am basically finished with my mod, but will occasionally receive an error in the entityscript.lua with an attempt to index local 'v' (a number value) screenshotted below. The crash happens at seemingly random times as well.

I'm not sure what is causing this since I haven't touched the scripts in the game, but my character is able to craft pets so maybe that code is somehow interfering: 

 

character.lua

local function OnDespawn(inst)
if inst.components.leader and inst.components.leader.followers then
    --inst.components.sanity.penalty=inst.components.leader.numfollowers*5
	local shadowpet={}
	local i=0
	for k,v in pairs(inst.components.leader.followers) do
	  
		i=i+1
		if k:HasTag("shadowpet") then	--table.insert(shadowpet, k.prefab)--:GetSaveRecord())
			shadowpet[i] = k:GetSaveRecord()
			k:Remove()
	    end
	
    end
	inst.shadowpet = shadowpet
end
end


local function OnSave(inst, data)

if not inst.shadowpet and inst.components.leader.numfollowers > 0 then  
    local shadowpet={}
	local i=0
   for k,v in pairs(inst.components.leader.followers) do
	    --print(k)
		i=i+1
		if k:HasTag("shadowpet") then
	    --table.insert(shadowpet, k.prefab)--:GetSaveRecord())
		shadowpet[i] = k:GetSaveRecord()
		--print(k.prefab.." removed")
		--k:Remove()
	end
	end
	
	if shadowpet then 
	data.shadowpet = shadowpet 
	--inst:DoTaskInTime(0,function(inst) print("loadtaskintime") inst.OnLoad(inst,inst) inst.shadowpet=nil end)
	end
end

if inst.shadowpet then
   data.shadowpet = inst.shadowpet
   end
end


local function OnLoad(inst, data)    
	local shadowpet = data.shadowpet       
	if shadowpet then      
	local pos = Vector3(inst.Transform:GetWorldPosition())
	local radius = 10
    local offset = Vector3(radius,0,0)
		for k, v in pairs(shadowpet) do
			local unit = SpawnSaveRecord(v)
			inst.components.leader:AddFollower(unit)
			unit.persists = false
        end      
		end     
		
	if data ~= nil and data.wisteriadarkenergy ~= nil then
		inst.components.darkenergy:GainAmount(data.wisteriadarkenergy)
	end
end

Any ideas? Thank you!

Error.png

Edited by theDarkFey
Link to comment
Share on other sites

@CarlZalph I'm looking in my shadowpet prefab, and I don't think I have that line of code there. The code for the shadowpet is below: 

 

local assets=
 {
	    Asset("ANIM", "anim/shadow_insanity2_basic.zip"),
 }
    
local prefabs =
{
   
}

SetSharedLootTable("shadow_creature",
{
    { "nightmarefuel",  1.0 },
    { "nightmarefuel",  1.0 },
	{ "purplegem", .2},
})

local function NormalRetargetFn(inst)
    return FindEntity(inst, TUNING.SHADOWCREATURE_TARGET_DIST, function(guy)
            if not guy.LightWatcher or guy.LightWatcher:IsInLight() then
                return not guy.components.health:IsDead() and inst.components.combat:CanTarget(guy) and not 
                (inst.components.follower and inst.components.follower.leader ~= nil and guy:HasTag("abigail"))
 and not guy:HasTag("player")
            end
        end,
        {"monster", "_health", "player"} -- see entityreplica.lua
        )
end
	
local function NotifyBrainOfTarget(inst, target)
    if inst.brain and inst.brain.SetTarget then
        inst.brain:SetTarget(target)
    end
end

local function OnAttacked(inst, data)
    inst.components.combat:SetTarget(data.attacker)
end	

	
	
local function OnNewCombatTarget(inst, data)
    NotifyBrainOfTarget(inst, data.target)
end

local function OnStartFollowing(inst) 
	if not inst.components.companion then
		inst:AddTag("companion") 
	end
end


local function FollowPlayer(inst)
	local player = GetPlayer()
	if player and player.components.leader then
		player.components.leader:AddFollower(inst)
	end
end

local function onsave(inst, data)
    data.shadowpet = not inst:HasTag("companion") or nil
end

local function onload(inst, data)
    if not data then return  end   
end

local function onbuild(inst,builder)
	inst.builder.components.darkenergy:GainAmount(-40)
end

local function ondeath(inst)
	if inst.components.darkenergy ~= nil then
	inst.components.sanity:DoDelta(40)
end
end

-------------------------------------------------------------------------------------------------
 

local function fn()

   local sounds =
    {
        attack = "dontstarve/sanity/creature2/attack",
        attack_grunt = "dontstarve/sanity/creature2/attack_grunt",
        death = "dontstarve/sanity/creature2/die",
        idle = "dontstarve/sanity/creature2/idle",
        taunt = "dontstarve/sanity/creature2/taunt",
        appear = "dontstarve/sanity/creature2/appear",
        disappear = "dontstarve/sanity/creature2/dissappear",
    }
    
	local inst = CreateEntity()
    local trans = inst.entity:AddTransform()
    local anim = inst.entity:AddAnimState()
    local physics = inst.entity:AddPhysics()
    local sound = inst.entity:AddSoundEmitter()
	
	
    inst.Transform:SetFourFaced()
     
    inst.AnimState:SetBank("shadowcreature2")
    inst.AnimState:SetBuild("shadow_insanity2_basic")
    anim:PlayAnimation("idle_loop")
    anim:SetMultColour(1, 1, 1, 0.5)
     
    MakeCharacterPhysics(inst, 10, 1.5)
    
    inst.Physics:SetCollisionGroup(COLLISION.SANITY)
    inst.Physics:CollidesWith(COLLISION.SANITY)
 
    inst:AddComponent("locomotor") -- locomotor must be constructed before the stategraph
    inst.components.locomotor.walkspeed = (8)
    inst.sounds = sounds
    inst:SetStateGraph("SGshadowcreature")

        local brain = require "brains/shadowpet_brain"
		
        inst:SetBrain(brain)
        
		inst.persists = false
		    
        inst:AddComponent("health")
        inst.components.health:SetMaxHealth(300)
		
        inst:AddComponent("combat")
        inst.components.combat:SetDefaultDamage(40)
        inst.components.combat:SetAttackPeriod(TUNING.TERRORBEAK_ATTACK_PERIOD)
		inst.components.combat:SetRetargetFunction(3, NormalRetargetFn)
        		
		inst:AddComponent("lootdropper")
        inst.components.lootdropper:SetChanceLootTable('terrorbeak_pet')
			
		inst:AddComponent("follower")
			
		inst:AddComponent("inspectable")
		inst:AddComponent("knownlocations")
		inst:AddTag("companion")
		inst:AddTag("scarytoprey")
		inst:AddTag("notraptrigger")
		inst:AddTag("shadowpet")
		inst:AddTag("terrorbeak")
		

        inst:ListenForEvent("attacked", OnAttacked)
		inst:ListenForEvent("newcombattarget", OnNewCombatTarget)
		inst:ListenForEvent("startfollowing", OnStartFollowing)		
		
		local oldonbuilt = inst.OnBuilt
			inst.OnBuilt = function(inst,builder)
			inst:ListenForEvent("onbuilt", function(inst,builder)
			inst.builder.components.darkenergy:DoDelta(-40)
			inst.builder.components.health:DoDelta(-30)
			--inst.HUD.controls.status.darkenergybadge:PulseRed()
			end)
				
			if builder.components.darkenergy then	
				builder.components.darkenergy:DoDelta(-40)
				
				--inst.HUD.controls.status.darkenergybadge:PulseRed()
			end
			if builder.components.health then
				builder.components.health:DoDelta(-30)
			end
			oldonbuilt(inst,builder)
			end
			
		
				
		
		local player = GetPlayer()
			if player and player.components.leader then
			player.components.leader:AddFollower(inst)
		end
		
        inst.entity:SetPristine()

        if not TheWorld.ismastersim then
            return inst
        end
	   
	    	
		inst.OnSave = onsave
		inst.OnLoad = onload
		
	inst:ListenForEvent("stopfollowing",function(inst)
		if not inst.components.health:IsDead() then inst.components.health:Kill()
		end 
	end
	)	
		
	inst:ListenForEvent("death", function()
		--inst:DoTaskInTime(0, function()
		if player ~= nil then
		inst._playerlink = player
		end
		if inst._playerlink ~= nil then
		--inst._playerlink.components.leader = nil
		inst._playerlink.components.darkenergy:DoDelta(40)
		
		end
		
		end)
	
	
		
        return inst
	end
		

		
return Prefab("terrorbeakpet", fn, assets, nil)

 

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