Page 1 of 2

unit_auto_com_group.lua

Posted: 06 Jul 2009, 19:15
by manolo_
hey,

i asked BD if he makes a lil widget for me (when game starts, my boi should be grouped to group 1)

but i got an error while its loading

[ 0] Error in DrawScreen(): [string "C:\games\Spring\LuaUI\Widgets\gui_comm_ends..."]:107: Incorrect arguments to gl.Text(msg, x, y [,size] [,"options"]
[ 0] Removed widget: CommanderEnds2


Code: Select all

 function widget:GetInfo()
  return {
    name      = "Auto Group com",
    desc      = "Has automagic inside!.",
    author    = "BD",
    date      = "Yesterday",
    license   = "GNU GPL, v2 or later",
    layer     = 0,
    enabled   = true  --  loaded by default?
  }
end



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


function widget:Initialize()
end

function widget:Shutdown()
end

function widget:GameFrame(t)
	
local allUnits = GetAllUnits()
	for _, 
unitID in pairs(allUnits) do
		
local unitDefID = GetUnitDefID(unitID)
		
if (unitDefID and UnitDefs[unitDefID].isCommander) then
		  
Spring.SetUnitGroup( unitDefID, 1 )
		end
	end
 	
widgetHandler:RemoveWidget()
end


Re: unit_auto_com_group.lua

Posted: 06 Jul 2009, 19:43
by very_bad_soldier
manolo_ wrote: [ 0] Error in DrawScreen(): [string "C:\games\Spring\LuaUI\Widgets\gui_comm_ends..."]:107: Incorrect arguments to gl.Text(msg, x, y [,size] [,"options"]
[ 0] Removed widget: CommanderEnds2
Read harder :mrgreen:

Re: unit_auto_com_group.lua

Posted: 06 Jul 2009, 22:00
by manolo_

Code: Select all

[      0] Error in GameFrame(): [string "C:\games\Spring\LuaUI\Widgets\unit_auto_com..."]:27: attempt to call global 'GetAllUnits' (a nil value)
[      0] Removed widget: Auto Group com
sorry, :P

Re: unit_auto_com_group.lua

Posted: 06 Jul 2009, 22:28
by very_bad_soldier
Easiest is to put "Spring." before those functions calls:

Code: Select all

function widget:GameFrame(t)
	local allUnits = Spring.GetAllUnits()
	for _, unitID in pairs(allUnits) do
		local unitDefID = Spring.GetUnitDefID(unitID)
		if (unitDefID and UnitDefs[unitDefID].isCommander) then
			Spring.SetUnitGroup( unitDefID, 1 )
		end
	end
	widgetHandler:RemoveWidget()
end

Re: unit_auto_com_group.lua

Posted: 06 Jul 2009, 22:33
by Argh
Fixed. Lots and lots of basic mistakes here. Variables weren't localized, and it was running every frame, lol.

Code: Select all

function widget:GetInfo()
  return {
    name      = "Auto Group com",
    desc      = "Has automagic inside!.",
    author    = "BD",
    date      = "Yesterday",
    license   = "GNU GPL, v2 or later",
    layer     = 0,
    enabled   = true  --  loaded by default?
  }
end



--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local GetAllUnits = Spring.GetAllUnits
local GetUnitDefID = Spring.GetUnitDefID
local SetUnitGroup = Spring.SetUnitGroup

function widget:GameFrame(t)
  if t == 1 then   
  local allUnits = GetAllUnits()
    for _, unitID in pairs(allUnits) do
       local unitDefID = GetUnitDefID(unitID)
       if (unitDefID and UnitDefs[unitDefID].isCommander) then
         SetUnitGroup( unitDefID, 1 )
       end
    end
  end
 if t > 1 then 
  widgetHandler:RemoveWidget()
 end
end
   

Re: unit_auto_com_group.lua

Posted: 06 Jul 2009, 22:44
by jK
Argh wrote:and it was running every frame, lol.
seems you don't know what widgetHandler:RemoveWidget() does ...

Re: unit_auto_com_group.lua

Posted: 06 Jul 2009, 22:47
by Argh
Well, I guess there's that. Fixed. Yay, gotta love Monday ;)

Re: unit_auto_com_group.lua

Posted: 07 Jul 2009, 08:38
by manolo_
big thx, so who wants to make my nuke-widget :D :D

Re: unit_auto_com_group.lua

Posted: 07 Jul 2009, 10:01
by Niobium
manolo_ wrote:big thx, so who wants to make my nuke-widget :D :D
What does it do?

Re: unit_auto_com_group.lua

Posted: 07 Jul 2009, 10:12
by Pendrokar
Auto nuke biggest density of units?

Re: unit_auto_com_group.lua

Posted: 07 Jul 2009, 10:21
by Google_Frog
Make it nuke the highest cost of units taking into account last known unit position, antinukes, terrain, AOE damage falloff and a complex economic model.

Re: unit_auto_com_group.lua

Posted: 07 Jul 2009, 10:53
by Pendrokar
Too easy. Not worth the effort. :P

Re: unit_auto_com_group.lua

Posted: 07 Jul 2009, 11:01
by manolo_

Re: unit_auto_com_group.lua

Posted: 08 Jul 2009, 17:49
by manolo_
still dont working in xta, maybe coz com has different name (?)

Re: unit_auto_com_group.lua

Posted: 09 Jul 2009, 04:02
by troycheek
Line 27 or so is currently:

SetUnitGroup( unitDefID, 1 )

Replace with:

SetUnitGroup( unitID, 1 )

and it works for me in XTA. Apparently, the information at wiki/Lua_UnsyncedCtrl is incorrect as pertains to Spring.SetUnitGroup as it says to use unitDefID instead of unitID. (Edit: Was incorrect. Has been fixed now.)

In addition to that, since you only have one unit (the commander) at the start of the game, you could remove the isCommander check and just let it add any unit it finds to the group.

Re: unit_auto_com_group.lua

Posted: 09 Jul 2009, 09:30
by manolo_
thx to all for your help works fine now

Re: unit_auto_com_group.lua

Posted: 12 Jul 2009, 20:53
by manolo_
bug:

when your a spectator/in team games, it sets all coms to the definied group

Re: unit_auto_com_group.lua

Posted: 12 Jul 2009, 22:11
by troycheek
Quick fix - adding the following to the end of what you've got should take care of the spectating problem by simply removing the widget when you're a spectator. (I'm assuming you won't need anything automatically grouped while spectating.)

Code: Select all

function widget:Initialize()
	if Spring.GetSpectatingState() then
		widgetHandler:RemoveWidget()
		return
	end
end
Not sure about the team problem. Perhaps someone else can help.

Re: unit_auto_com_group.lua

Posted: 13 Jul 2009, 15:10
by manolo_
couldnt it just detect the unit that is at the startpoint? and could it autpgroup if upgrade my boi?

Re: unit_auto_com_group.lua

Posted: 13 Jul 2009, 17:33
by Pendrokar
GetAllUnits() - Get all units in LOS... => Not only your team, all teams...

Add another condition to

Code: Select all

 if (unitDefID and UnitDefs[unitDefID].isCommander) then
(Spring.GetUnitTeam(unitID) == Spring.GetMyTeamID)