Q: LuaCob and returns from Cob

Q: LuaCob and returns from Cob

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

Moderator: Moderators

Post Reply
Archangel of Death
Posts: 854
Joined: 28 Jan 2005, 18:15

Q: LuaCob and returns from Cob

Post by Archangel of Death »

The stuff on LuaCob found here led me to believe it was possible to get the return value from a cob script called from lua. Am I correct in thinking this?

I am using 75b2 for this, mainly because it means I don't have to worry about the possibility something was changed. Spring.CallCOBScript is supposed to return nil, or "number returnValue", which I assume is the number returned by return at the end of the cob function. I have confirmed that if the unit does not have the cob function that the call attempt was made on, the value is nil, as should be expected. But if it does have it, then the value is always 0, whether I returned 0, 1, or 10 in the cob.

If I'm just doing it wrong, please enlighten. If the engine is just doing it wrong... if you want a look at my lua and boss's pm me an email address and I'll send them to you.

And yes I know there are a few other ways to get that data back to Lua, but this would be the easiest and fastest way.
User avatar
quantum
Posts: 590
Joined: 19 Sep 2006, 22:48

Re: Q: LuaCob and returns from Cob

Post by quantum »

It is indeed possible, and easy once you know the trick. Here is an example. Just look at the synced part.

Code: Select all

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


function gadget:GetInfo()
  return {
    name      = "Supression Indicators",
    desc      = "Visually indicates supression levels",
    author    = "quantum",
    date      = "June 29, 2007",
    license   = "GNU GPL, v2 or later",
    layer     = 0,
    enabled   = true  --  loaded by default?
  }
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local Spring = Spring
local gl = gl
--------------------------------------------------------------------------------
--  COMMON
--------------------------------------------------------------------------------
if (gadgetHandler:IsSyncedCode()) then
--------------------------------------------------------------------------------
--  SYNCED
--------------------------------------------------------------------------------


local scriptIDs = {}


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


function gadget:UnitCreated(unitID)
  scriptIDs[unitID] = Spring.GetCOBScriptID(unitID, "luaFunction")
end


function gadget:UnitDestroyed(unitID)
  scriptIDs[unitID] = nil
end


function gadget:GameFrame(n)
  for unitID, funcID in pairs(scriptIDs) do
    local _, supression = Spring.CallCOBScript(unitID, funcID, 1, 1)
    SendToUnsynced("supressed", unitID, supression)
  end
end

--------------------------------------------------------------------------------
--  SYNCED
--------------------------------------------------------------------------------
else
--------------------------------------------------------------------------------
--  UNSYNCED
--------------------------------------------------------------------------------
local WhiteStr   = "\255\255\255\255"
local RedStr     = "\255\255\001\001"


local pinnedThreshold = 14
local fontSize = 20

local units = {}
local remove = {}
function gadget:DrawScreen(dt)
  local readTeam
  local spec, specFullView = Spring.GetSpectatingState()
  if (specFullView) then
    readTeam = Script.ALL_ACCESS_TEAM
  else
    readTeam = Spring.GetLocalTeamID()
  end
  CallAsTeam({ ['read'] = readTeam }, function()
      local n = Spring.GetGameFrame()
      for unitID, supression in pairs(units) do
        -- if (not Spring.GetUnitViewPosition(unitID)) then
          -- break
        -- end
        local wx, wy, wz = Spring.GetUnitPosition(unitID)
        if (not wx) then
          remove[unitID] = true
          break
        end
        local sx, sy, sz = Spring.WorldToScreenCoords(wx, wy, wz)
        if (supression >= pinnedThreshold) then
          gl.Text(RedStr..supression, sx, sy, 20, "c")
        elseif (supression ~= 0) then
          gl.Text(WhiteStr..supression, sx, sy, 10, "c")
        -- else
          -- gl.Text(WhiteStr.."x", sx, sy, 10, "c")
        end
      end
    end
  )
  for unitID in pairs(remove) do
    units[unitID] = nil
  end
end

function RecvFromSynced(...)
  if (arg[2] == 'supressed') then
    units[arg[3]] = arg[4]
  end
end
--------------------------------------------------------------------------------
--  UNSYNCED
--------------------------------------------------------------------------------
end
--------------------------------------------------------------------------------
--  COMMON
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

The relevant part in the bos is:

Code: Select all

//lets Lua suppression notification see what fear is at
luaFunction(arg1)
{
 arg1 = fear;
}
Archangel of Death
Posts: 854
Joined: 28 Jan 2005, 18:15

Re: Q: LuaCob and returns from Cob

Post by Archangel of Death »

Aha, thanks. And now I actually understand how the returns work too!
User avatar
KDR_11k
Game Developer
Posts: 8293
Joined: 25 Jun 2006, 08:44

Re: Q: LuaCob and returns from Cob

Post by KDR_11k »

I think you can get a regular return with CallCOBScriptCB.
Post Reply

Return to “Lua Scripts”