MookVanguard Posted June 18, 2015 Share Posted June 18, 2015 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 endendand 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 More sharing options...
MookVanguard Posted June 18, 2015 Author Share Posted June 18, 2015 Just a head's up, I mistyped the original post. Throw(target, attacker) should be Throw(attacker, target). My character can't perform the action on anything, which means that the target is nil. Link to comment Share on other sites More sharing options...
DarkXero Posted June 19, 2015 Share Posted June 19, 2015 function ThrowMT:Throw(attacker, target)equalsfunction ThrowMT.Throw(ThrowMT, attacker, target)where you reference ThrowMT as self. Therefore, when you dolocal success = act.doer.components.throwmt.Throw(act.doer, target)you getself = act.doerattacker = act.targettarget = nilAssuming you mistyped the code too, the attacker would be the nil parameter. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now