Page 1 of 1

[cleared] Strange table-behavior

Posted: 17 Aug 2010, 20:52
by camo
Hi thar!

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

Re: Strange table-behavior

Posted: 17 Aug 2010, 21:01
by jK
camo wrote:

Code: Select all

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: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
    end
  end
end
->
hastech[teamID]

versus
hastech[teamID][i[1]]

Re: [cleared] Strange table-behavior

Posted: 17 Aug 2010, 21:11
by camo
Wow, 1 point for Captain Obvious, 10 for you :-)


(And parts of a new researchsystem for the masses)

Re: [cleared] Strange table-behavior

Posted: 17 Aug 2010, 23:56
by Forboding Angel
Hey, I don't want to bust your bubble and I sure as hell don't want to discourage you, but I just wanted to make you aware of this: http://springrts.com/phpbb/viewtopic.ph ... hilit=tech

So that you don't end up accidentally reinventing the wheel. But like i said, I sure as hell don't want to discourage you or anything like that. Just more of an FYI that this already exists in some shape or form (But it may be totally irrelevant because what you're working on might be completely different).

Re: [cleared] Strange table-behavior

Posted: 18 Aug 2010, 12:36
by camo
I know, have been browsing the forums for quite a long time. They don't exactly fit my need. A combination of simple_tech and Multi_tech is close, but still no hit. In the end it might be a mix of these both and own stuff. When it's all done I'll publish it, so you see :shock:

Keep on busting btw, a concept can't exist of bubbles alone