Page 1 of 1

Having trouble getting UnitDefs to work

Posted: 08 Sep 2012, 01:17
by yanom
I just recently started hacking on some lua gadgets, and now I've got this problem. Here in gadget:UnitFinished we have:

Code: Select all

function gadget:UnitFinished(unitID, unitDefID, teamID)
    if (is_miner_type (unitDefID) == true) then 
and the is_miner_type function is:

Code: Select all

 function is_miner_type (unitDefID)
    Spring.Echo("is_miner_type called!")
    
    local def = UnitDefs[unitDefID]
    --Spring.Echo(def)
    local custom = def.customParams
    if(custom) then
        --Spring.Echo(m)
        --Spring.Echo(UnitDefs[unitDefID])
        return custom.is_miner
    end
    
    --[[
    if (unitDefID == nil) then return false end
    local unitDef = UnitDefs[unitDefID]
    if (unitDef == nil) then return false end
    local unitname = unitDef.name
    for _,v in pairs(miner_name) do
        if v == unitname then return true end
    end ]]
    
    return false
end
I can't for the life of me get is_miner_type to work. It always returns false. Putting some debug code in, I found that it's unable to load the UnitDef: Spring.Echo(def) always displays TABLE: , an empty table.

Re: Having trouble getting UnitDefs to work

Posted: 08 Sep 2012, 01:39
by jK
You are very likely trying to use UnitFinished in unsynced context of the gadget, it does not automatically recv that, you need to pass it from synced to unsynced.
The topic was answered already on http://answers.springlobby.info/questions/

Re: Having trouble getting UnitDefs to work

Posted: 08 Sep 2012, 04:05
by yanom
jK wrote:You are very likely trying to use UnitFinished in unsynced context of the gadget,
nope. It's in the synced bit. Also, if I ask it to print the whole UnitDefs table, it freezes the game for a minute, then spits out a lot of stats and figures. So it can access that table, it's just not pulling up the data it should.

Re: Having trouble getting UnitDefs to work

Posted: 10 Sep 2012, 02:27
by yanom
*bump* anyone?

Re: Having trouble getting UnitDefs to work

Posted: 10 Sep 2012, 16:32
by Google_Frog
I found that it's unable to load the UnitDef: Spring.Echo(def) always displays TABLE: , an empty table.
That does not imply an empty table. Print def with pairs to see what the values are.

Re: Having trouble getting UnitDefs to work

Posted: 11 Sep 2012, 02:08
by yanom
Fixed!

drawing inspiration from stuff that was already in the code, i made this function (gets executed on gadget startup):

Code: Select all

function make_miner_name_table () 
    for id,unitDef in pairs(UnitDefs) do
        local cp = UnitDefs[id].customParams
        if (cp) then
            if (cp.is_miner) then
                local minersname = unitDef.name
                if (debug) then Spring.Echo ("tp_mining.lua: found miner" .. minersname) end
                table.insert (miner_name, minersname)
            end
        end
    end
end
and then is_miner_type() is this code:

Code: Select all

function is_miner_type (unitDefID)
     
    
    if (unitDefID == nil) then return false end
    local unitDef = UnitDefs[unitDefID]
    if (unitDef == nil) then return false end
    local unitname = unitDef.name
    for _,v in pairs(miner_name) do
        if v == unitname then return true end
    end
    
    return false
end
that code was already in there before I started hacking, but miner_name was a manually defined table, and I wanted the lua script to draw information from customParams, which it now does.