modoption value to cob

modoption value to cob

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

Moderator: Moderators

Post Reply
User avatar
bobthedinosaur
Blood & Steel Developer
Posts: 2702
Joined: 25 Aug 2004, 13:31

modoption value to cob

Post by bobthedinosaur »

I was wondering if some one could show an example of an easy way to signal mod option values to unit type's cobs (yes i am using cob, b/c something can't be done in lua with he same effect). Using something like a custom parameter for the units that receive is fine.
User avatar
lurker
Posts: 3842
Joined: 08 Jan 2007, 06:13

Re: modoption value to cob

Post by lurker »

Option 1

Code: Select all

lua_getXmodoption() {return 0;}
lua_getXunitoption() {return 0;}
moo(){
   call-script lua_getmodoption();
   get(LUA1);
}


local modOptions = Spring.GetModOptions()
function _G.getXmodoption(unitID, unitDefID, teamID)
   return modOptions.potatofarmer
end
function _G.getXunitoption()
   return UnitDefs[unitDefID].customParams.cheese
end



Option 2 has been deleted before posting because Spring is apparently missing the Set functions for cob variables. Too bad, because it made for really concise bos code.
User avatar
bobthedinosaur
Blood & Steel Developer
Posts: 2702
Joined: 25 Aug 2004, 13:31

Re: modoption value to cob

Post by bobthedinosaur »

why did you add a get(LUA1) in that part of the cob? what does that do?

if the mod option is a boolean, couldn't that be much more simplified (in the cob section)?
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Re: modoption value to cob

Post by FLOZi »

b/c something can't be done in lua with he same effect)
Such as?
lurker wrote: Option 2 has been deleted before posting because Spring is apparently missing the Set functions for cob variables. Too bad, because it made for really concise bos code.
Umm, Spring.GetUnitCOBValue, Spring.SetUnitCOBValue (and also Spring.UnitScript.GetUnitValue / Spring.UnitScript.SetUnitValue in LUS)

or do you mean to set a variable value within bos directly?

Anyway, I'd do this with Spring.CallCOBScript.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Re: modoption value to cob

Post by FLOZi »

Lua (Probably needs more robust checking of modoptions):

Code: Select all

local modOptions =  Spring.GetModOptions()
local myParticularModOption = tonumber(modOptions.my_particular_mod_option)

function gadget:UnitCreated(unitID, unitDefID)
  local scriptID = Spring.GetCOBScriptID(unitID, "PassModOption")
  -- alternatively use some unitdef customParam
  if scriptID then
    Spring.CallCOBScript(unitID, "PassModOption", 0, myParticularModOption)
  end
end
Bos:

Code: Select all

static-var modOptionValue;
PassModOption(valueFromLua) {
  modOptionValue = valueFromLua;
}
User avatar
lurker
Posts: 3842
Joined: 08 Jan 2007, 06:13

Re: modoption value to cob

Post by lurker »

Here, I'll show you what I mean, flozi.

Code: Select all

#define XMODOPTION 4096 + 0
moo(){
    get(XMODOPTION);
}


function gadget:Initialize()
   local modOptions = Spring.GetModOptions()
   local XMODOPTION = 4096 + 0
   Spring.SetCOBGlobalVar(XMODOPTION, modOptions.potatofarmer)
end
There are special cob storage values per unit, per team, and global. The global ones start at 4096. The problem is that SetCOBGlobalVar, despite being documented, does not exist. Only GetCOBGlobalVar exists. You could hack in a way to set it via a unit but by then you've lost the simplicity of the code.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Re: modoption value to cob

Post by FLOZi »

Ah I see, never had a need for them.
User avatar
bobthedinosaur
Blood & Steel Developer
Posts: 2702
Joined: 25 Aug 2004, 13:31

Re: modoption value to cob

Post by bobthedinosaur »

this is what i have, but unfortunatly it doesnt apply to units that are created in the first few frames (ie: commander like units).

gadget:
http://sl.pastebin.com/ezBfHiSz

cob:
http://sl.pastebin.com/RjbfXxKR
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Re: modoption value to cob

Post by FLOZi »

Bob, change line 488 of game_roundcontrol.lua to

Code: Select all

function gadget:GameStart()
And all will be well.

(Although I'd also suggest moving AnalyzeMetalMap() to gadget:GamePreLoad())
User avatar
bobthedinosaur
Blood & Steel Developer
Posts: 2702
Joined: 25 Aug 2004, 13:31

Re: modoption value to cob

Post by bobthedinosaur »

Sounds good to me. I'll mention it to car.

But I actually got it working last night. I added a 2nd function in the commandermode gadget that does the same thing in the first 0 to 3 frames, which seems to catch it. Which this looks like works better.
Post Reply

Return to “Lua Scripts”