Jump to content

[HELP] Attacker is nil, even though it's assigned

Recommended Posts

Hi, I am trying to make a book that, when read, summons rocks around the player. The player then gets an action called "obliterate" that launches all of the rocks at the selected target.


The problem is that when I use the following component for the character:

function ThrowMT:Throw(target, attacker)	if target ~= nil then		local pt = Vector3(attacker.Transform:GetWorldPosition())		local ents = TheSim:FindEntities(pt.x, 0, pt.z, 7, nil, { "smashable", "INLIMBO", "playerghost" })			for i, v in ipairs(ents) do				if v ~= nil and v.prefab == "rocks" then					attacker.SoundEmitter:PlaySound("dontstarve/common/stone_drop")										local function onattack(inst, attacker, target, skipsanity)						if target.components.combat then							target.components.combat:GetAttacked(attacker, 5, weapon)						end					end										v:AddComponent("projectile")					v.components.projectile.onhit = onattack					v.components.projectile:Throw(attacker, target, attacker)														end			end				return true	else 		return false	endend

and invoke it like so:

AddAction("THROWMT", "Obliterate", function(act)	local target = act.target	if act.doer ~= nil and target ~= nil and target.components.combat then		local success = act.doer.components.throwmt.Throw(act.doer, target)		if success == true then			act.doer:RemoveComponent("throwmt")			return true		else			return false		end	else		return false	endend)GLOBAL.ACTIONS.THROWMT.distance = 40AddStategraphActionHandler("wilson", ActionHandler(GLOBAL.ACTIONS.THROWMT, "doshortaction"))     AddComponentAction("SCENE", "combat", function(inst, doer, actions, right)	if doer.components.throwmt then		table.insert(actions, GLOBAL.ACTIONS.THROWMT)	endend)

I get the following error message:

[00:01:03]: [string "../mods/Suika 1/scripts/components/throwmt...."]:14: attempt to index local 'attacker' (a nil value)LUA ERROR stack traceback:../mods/Suika 1/scripts/components/throwmt.lua:14 in (field) Throw (Lua) <11-38>

I don't understand why though. Line 14 is the first reference to the variable "attacker," but "attacker" is equal to act.doer, which is required to be a real value for the action to fire in the first place. Does anyone know what is going on?


Link to comment
Share on other sites

function ThrowMT:Throw(attacker, target)equalsfunction ThrowMT.Throw(ThrowMT, attacker, target)

where you reference ThrowMT as self.


Therefore, when you do

local success = act.doer.components.throwmt.Throw(act.doer, target)

you get

self = act.doerattacker = act.targettarget = nil

Assuming you mistyped the code too, the attacker would be the nil parameter.

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

  • Create New...