Page 1 of 1

Lua can't give order during first instant

Posted: 09 Aug 2007, 02:35
by zwzsg
Try the following \LuaUI\Widgets\*.LUA script:

Code: Select all

function widget:GetInfo()
  return {
    name      = "Force Repeat ON",
    desc      = "Turn repeat ON on all units during their creation",
    author    = "zwzsg",
    date      = "August 8, 2007",
    license   = "Free",
    layer     = 0,
    enabled   = true  --  loaded by default
  }
end


function widget:UnitCreated(unitID, unitDefID, unitTeam)
 local ud = UnitDefs[unitDefID]
 if(unitTeam == Spring.GetMyTeamID()) then
   Spring.GiveOrderToUnit(unitID, CMD.REPEAT, { 1 }, {})
   Spring.Echo("Repeat order issued on " .. ud.name)
 end
end
It is supposed to turn "repeat" ON on every unit upon their creation. However, you'll notice that, for the commander, while the command is actually issued (you see the message), it remains "repeat" OFF. For any other unit it works fine.

I guess that lua is not allowed to give order during the first instant of the game. Why? Can be fixed? How do I workaround it?

I'm trying to fix Kernel Panic for 75b2. And amongst other thing, I need the autospam script to work again, and it'd be much nicer if that included the kernel.

Posted: 09 Aug 2007, 02:41
by AF
Either add the unit to an array and one very update, set repeat on and empty the array, or, switch to unitfinished instead of unitcalled.

Posted: 09 Aug 2007, 02:42
by zwzsg
I tried with UnitFinished, same problem apply.

Posted: 09 Aug 2007, 03:53
by Andrej

Code: Select all

------------------------------------------------------------------------------------
function widget:GetInfo() 
  return { 
    name      = "Force Repeat ON", 
    desc      = "Turn repeat ON on all units during their creation", 
    author    = "zwzsg", 
    date      = "August 8, 2007", 
    license   = "Free", 
    layer     = 0, 
    enabled   = true  --  loaded by default 
  } 
end 


function widget:UnitCreated(unitID, unitDefID, unitTeam) 
 local ud = UnitDefs[unitDefID] 
 if(unitTeam == Spring.GetMyTeamID()) then 
   Spring.GiveOrderToUnit(unitID, CMD.REPEAT, { 1 }, {}) 
   Spring.Echo("Repeat order issued on " .. ud.name) 
 end 
end



function widget:DrawScreen()

ut111 = Spring.GetTeamUnits( Spring.GetMyTeamID() )

 if ( table.getn(ut111) > 0 ) then

	ut11 = Spring.GetTeamUnits( Spring.GetMyTeamID() )

	 for k,v in pairs(ut11) do
	    Spring.GiveOrderToUnit(v, CMD.REPEAT, { 1 }, {}) 
	 end

	widget.widgetHandler.RemoveCallIn(widget.widget, "DrawScreen")

 end

end
------------------------------------------------------------------------------------
tldr commander spawn doesnt count as unit created and orders before the game started but after the map loaded dont count apparently O_o

this will basically spaem getteamunits until there are some ( = com spawned) then give repeat order and remove drawscreen from callin list or something seems to work when i try T_T[/code]

Posted: 09 Aug 2007, 05:25
by zwzsg
That worked, thanks Andrej.

Posted: 09 Aug 2007, 10:36
by AF
Why are you using DrawScreen? Wouldnt it be better (and more appropriate) to use Update()?

Also IIRC, Update doesnt get called untill the game starts, whereas drawscreen could be called while people are picking starting positions

This way you wouldnt have to repeatedly check untill the commander spawns.

Use the method AIs use. In Update() check fi the current game frame is 30 and then handle the commander then. That way you onyl check once, and you dont cause slowdown.

Posted: 10 Aug 2007, 00:24
by zwzsg
I just checked, and both DrawScreen and Update keep getting called during the time "Waiting for connections. Press return to start" is shown. And both are called as often (about 1600 times in the first 10s (including 2s of loading)).

Posted: 10 Aug 2007, 00:31
by jK
perhaps it does, but it still is the wrong place for such things

btw GameFrame() is only called if the game is running, but it is only accessable in gadgets ;o