I'm currently modifying my researchscript. It all worked fine so far, but
function gadget:UnitCreated(unitID, unitDefID, teamID)
needed a small change.
In there I'm accessing a table, but it returns nil as the data. Amusingly it works perfectly fine in all the other functions and returns the right data.
Is it some dumb typo or once again a nonobvious behavior of lua?
camo
Code: Select all
function gadget:GetInfo()
return {
name = "RA-Research",
desc = "",
author = "camo",
date = "2010-08-07",
license = "ask",
layer = 0,
enabled = true
}
end
if (not gadgetHandler:IsSyncedCode()) then
return false
end
include('luarules/gadgets/const.lua')
local techs={
{39601,"601-tech","601","Commshiptech"},
{39602,"602-tech","602","Interceptortech"},
}
local hastech={}
function gadget:Initialize()
for _,team in ipairs(Spring.GetTeamList()) do
hastech[team]={}
for _,i in pairs(techs) do
hastech[team][i]=0
end
end
end
function gadget:AllowCommand(unitID, unitDefID, teamID,cmdID, cmdParams, cmdOptions)
if cmdID>=39601 and cmdID<=39602 then
hastech[teamID][cmdID]=1
print(teamID.." " ..cmdID.." now "..hastech[teamID][cmdID])
--TODO: Foreach
Spring.RemoveUnitCmdDesc(unitID,Spring.FindUnitCmdDesc(unitID,cmdID))
end
return true
end
function gadget:AllowUnitCreation(unitDefID, builderID, builderTeam, x, y, z)
if UnitDefs[unitDefID].customParams.needtech==nil then
return true
end
if hastech[builderTeam][tonumber(UnitDefs[unitDefID].customParams.needtech)]==1 then
return true
else
return false
end
print("ERR")
end
function gadget:UnitCreated(unitID, unitDefID, teamID)
if UnitDefNames["researchcenter"].id==unitDefID then
for _,i in pairs(techs) do
print(teamID)
print(hastech[teamID][i[1]]) --returns nil
print(hastech[teamID][39601]) --aswell
if hastech[teamID][i[1]]==0 then
Spring.InsertUnitCmdDesc(unitID, {
id = i[1],
type=CMDTYPE.ICON,
name=i[2],
action="t",
tooltip=i[4].."\nCost: "..i[3].." Metal",
})
end
end
end
end