Help! (strange bug in my code)

Help! (strange bug in my code)

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
eyu100
Posts: 182
Joined: 05 Jul 2008, 04:10

Help! (strange bug in my code)

Post by eyu100 »

Code: Select all

function gadget:GetInfo()
   return {
      name = "Fail Gadget",
      desc = "Various ways to kill stuff",
      author = "eyu100 (Eric Yu)",
      date = "2010-07-10",
      license = "Public domain",
      layer = 1,
      enabled = true
   }
end

local function Killall()
   local units = Spring.GetAllUnits()
   for _,unit in pairs(units) do
      Spring.DestroyUnit(unit,true)
   end
end

local function Kill(cmd, msg, words, player)
   local teamToKill = msg[1]
   Spring.Echo(teamToKill)
   local unitsToKill = Spring.GetTeamUnits(teamToKill)
   for _,unit in pairs(unitsToKill) do
      Spring.DestroyUnit(unit,true)
   end
end

local function Suicide(cmd, msg, words, player)
   local _,_,_,teamToKill,_ = Spring.GetPlayerInfo(player)
   local unitsToKill = Spring.GetTeamUnits(teamToKill)
   for _,unit in pairs(unitsToKill) do
      Spring.DestroyUnit(unit,true)
   end
end

function gadget:Initialize()
   gadgetHandler:AddChatAction("killall",Killall,"Kills all units")
   gadgetHandler:AddChatAction("kill",Kill,"Kills stuff")
   gadgetHandler:AddChatAction("suicide",Suicide,"Kills self")
end

--[[
function gadget:randomNumber()
   local players = Spring.GetPlayerList()
   local playerInfo, i = {}, 1
   for index,playerID in pairs(players) do
      playerInfo[i] = Spring.GetPlayerInfo(playerID)
   end
end
--]]
/luarules killall works, /luarules kill [team] doesn't (prints "nil" as the team to kill), /luarules suicide works.

Oddly, killall and suicide run twice and throw the error "LuaRules::RunCallIn: error = 2, RecvFromSynced, [string "LuaRules/Gadgets/failgadget.lua"]:[line number]:attempt to call field 'DestroyUnit' (a nil value)" (note that the error is only thrown once), while kill [team] only runs once. Any explanations? In case this helps, I tested this gadget by adding it to an unpacked (.sdd) version of the BA V7.11 mod archive and used /luarules reload to quickly test my changes.
SkyStar
Engines Of War Developer
Posts: 23
Joined: 08 Jul 2009, 15:05

Re: Help! (strange bug in my code)

Post by SkyStar »

I'm not sure exactly what the problem is, but try calling destroy functions from gameframe instead.

so:

Code: Select all

function gadget:GetInfo()
   return {
      name = "Fail Gadget",
      desc = "Various ways to kill stuff",
      author = "eyu100 (Eric Yu)",
      date = "2010-07-10",
      license = "Public domain",
      layer = 1,
      enabled = true
   }
end

local killUnits = {}

function gadget:GameFrame(frame) 
   for unitID,_ in pairs(killUnits) do
   Spring.DestroyUnit(unitID,true)
   killUnits[unitID] = nil
   end 
end

local function Killall()
   local units = Spring.GetAllUnits()
   for _,unit in pairs(units) do
    killUnits[unit] = true
   end
end

local function Kill(cmd, msg, words, player)
   local teamToKill = msg[1]
   Spring.Echo(teamToKill)
   local unitsToKill = Spring.GetTeamUnits(teamToKill)
   for _,unit in pairs(unitsToKill) do
   killUnits[unit] = true
   end
end

local function Suicide(cmd, msg, words, player)
   local _,_,_,teamToKill,_ = Spring.GetPlayerInfo(player)
   local unitsToKill = Spring.GetTeamUnits(teamToKill)
   for _,unit in pairs(unitsToKill) do
   killUnits[unit] = true
   end
end

function gadget:Initialize()
   gadgetHandler:AddChatAction("killall",Killall,"Kills all units")
   gadgetHandler:AddChatAction("kill",Kill,"Kills stuff")
   gadgetHandler:AddChatAction("suicide",Suicide,"Kills self")
end

--[[
function gadget:randomNumber()
   local players = Spring.GetPlayerList()
   local playerInfo, i = {}, 1
   for index,playerID in pairs(players) do
      playerInfo[i] = Spring.GetPlayerInfo(playerID)
   end
end
--]]
eyu100
Posts: 182
Joined: 05 Jul 2008, 04:10

Re: Help! (strange bug in my code)

Post by eyu100 »

Didn't work =\
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Re: Help! (strange bug in my code)

Post by jK »

1. gadgets contain synced AND unsynced versions of the gadget in the same file, check other gadgets how they manage this
2. check all arguments a function gets that is added via AddChatAction
eyu100
Posts: 182
Joined: 05 Jul 2008, 04:10

Re: Help! (strange bug in my code)

Post by eyu100 »

Here is my new (not working at all, i.e. commands do nothing) code:

Code: Select all

function gadget:GetInfo()
   return {
      name = "Fail Gadget",
      desc = "Various ways to kill stuff",
      author = "eyu100 (Eric Yu)",
      date = "2010-07-10",
      license = "Public domain",
      layer = 1,
      enabled = true
   }
end

local killall = false
local kill = {false, ["team"]=nil}
local genocide = {false, ["unitType"]=nil}

local function Killall(cmd,msg,words,player)
   killall = true
end

local function Kill(cmd,msg,words,player)
   kill[0] = true
   kill["team"] = msg[1]
end

local function Genocide(cmd,msg,words,player)
   genocide[0] = true
   genocide["unitType"] = msg[1]
end

local function Suicide(cmd,msg,words,player)
   kill[0] = true
   kill["team"] = player
end

local function _killall()
   Spring.Echo("Killing all units...")
   local units = Spring.GetAllUnits()
   for _,unit in pairs(units) do
      Spring.DestroyUnit(unit,true)
   end
end

local function _kill(team)
   Spring.Echo("Selected team is:")
   Spring.Echo(team)
   local teamToKill = team
   local unitsToKill = Spring.GetTeamUnits(teamToKill)
   for _,unit in pairs(unitsToKill) do
      Spring.DestroyUnit(unit,true)
   end
end

local function _genocide(unitType)
   Spring.Echo("Genociding unit:")
   Spring.Echo(unitType)
   local units = Spring.GetAllUnits()
   for _,unit in pairs(units) do
      local def = UnitDefs[Spring.GetUnitDefID(unit)]
      if def["name"]==unitType then
	 Spring.DestroyUnit(unit,reclaimed=true)
      end
   end
end

function gadget:GameFrame()
   if killall then _killall() end
   if kill[1] then _kill(kill["team"]) end
   if genocide[1] then _genocide(genocide["unitType"]) end
end

function gadget:Initialize()
   gadgetHandler:AddChatAction("killall",Killall,"Kills all units")
   gadgetHandler:AddChatAction("kill",Kill,"Kills stuff")
   gadgetHandler:AddChatAction("genocide",Genocide,
			       "Kills all units of a particular type
			       and prevents more of them from being
			       built")
   gadgetHandler:AddChatAction("suicide",Suicide,"Kills self")
end

--[[
function gadget:randomNumber()
   local players = Spring.GetPlayerList()
   local playerInfo, i = {}, 1
   for index,playerID in pairs(players) do
      playerInfo[i] = Spring.GetPlayerInfo(playerID)
   end
end
--]]
SirMaverick
Posts: 834
Joined: 19 May 2009, 21:10

Re: Help! (strange bug in my code)

Post by SirMaverick »

jK wrote:1. gadgets contain synced AND unsynced versions of the gadget in the same file, check other gadgets how they manage this
2. check all arguments a function gets that is added via AddChatAction
eyu100, read that again.

Also read other peoples code that does similar things, e.g. http://trac.caspring.org/browser/trunk/ ... _debug.lua
eyu100
Posts: 182
Joined: 05 Jul 2008, 04:10

Re: Help! (strange bug in my code)

Post by eyu100 »

SirMaverick wrote:
jK wrote:1. gadgets contain synced AND unsynced versions of the gadget in the same file, check other gadgets how they manage this
2. check all arguments a function gets that is added via AddChatAction
eyu100, read that again.

Also read other peoples code that does similar things, e.g. http://trac.caspring.org/browser/trunk/ ... _debug.lua
I already figured out my problem; I was having syntax errors (I didn't know where to find the error messages :lol:).

EDIT: fixed smiley
Post Reply

Return to “Lua Scripts”