Lua can't give order during first instant

Lua can't give order during first instant

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

Moderator: Moderators

Post Reply
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Lua can't give order during first instant

Post 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.
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post 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.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Post by zwzsg »

I tried with UnitFinished, same problem apply.
Andrej
Posts: 176
Joined: 13 Aug 2006, 18:55

Post 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]
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Post by zwzsg »

That worked, thanks Andrej.
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post 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.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

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

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

Return to “Lua Scripts”