Having trouble getting UnitDefs to work

Having trouble getting UnitDefs to work

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

Moderator: Moderators

Post Reply
yanom
Posts: 323
Joined: 10 Jul 2009, 23:34

Having trouble getting UnitDefs to work

Post 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.
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Re: Having trouble getting UnitDefs to work

Post 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/
yanom
Posts: 323
Joined: 10 Jul 2009, 23:34

Re: Having trouble getting UnitDefs to work

Post 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.
yanom
Posts: 323
Joined: 10 Jul 2009, 23:34

Re: Having trouble getting UnitDefs to work

Post by yanom »

*bump* anyone?
Google_Frog
Moderator
Posts: 2464
Joined: 12 Oct 2007, 09:24

Re: Having trouble getting UnitDefs to work

Post 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.
yanom
Posts: 323
Joined: 10 Jul 2009, 23:34

Re: Having trouble getting UnitDefs to work

Post 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.
Post Reply

Return to “Lua Scripts”