unit_auto_com_group.lua

unit_auto_com_group.lua

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

Moderator: Moderators

User avatar
manolo_
Posts: 1370
Joined: 01 Jul 2008, 00:08

unit_auto_com_group.lua

Post 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

User avatar
very_bad_soldier
Posts: 1397
Joined: 20 Feb 2007, 01:10

Re: unit_auto_com_group.lua

Post 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:
User avatar
manolo_
Posts: 1370
Joined: 01 Jul 2008, 00:08

Re: unit_auto_com_group.lua

Post 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
User avatar
very_bad_soldier
Posts: 1397
Joined: 20 Feb 2007, 01:10

Re: unit_auto_com_group.lua

Post 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
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: unit_auto_com_group.lua

Post 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
   
Last edited by Argh on 06 Jul 2009, 22:59, edited 2 times in total.
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Re: unit_auto_com_group.lua

Post by jK »

Argh wrote:and it was running every frame, lol.
seems you don't know what widgetHandler:RemoveWidget() does ...
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: unit_auto_com_group.lua

Post by Argh »

Well, I guess there's that. Fixed. Yay, gotta love Monday ;)
User avatar
manolo_
Posts: 1370
Joined: 01 Jul 2008, 00:08

Re: unit_auto_com_group.lua

Post by manolo_ »

big thx, so who wants to make my nuke-widget :D :D
User avatar
Niobium
Posts: 456
Joined: 07 Dec 2008, 02:35

Re: unit_auto_com_group.lua

Post by Niobium »

manolo_ wrote:big thx, so who wants to make my nuke-widget :D :D
What does it do?
User avatar
Pendrokar
Posts: 658
Joined: 30 May 2007, 10:45

Re: unit_auto_com_group.lua

Post by Pendrokar »

Auto nuke biggest density of units?
Google_Frog
Moderator
Posts: 2464
Joined: 12 Oct 2007, 09:24

Re: unit_auto_com_group.lua

Post 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.
User avatar
Pendrokar
Posts: 658
Joined: 30 May 2007, 10:45

Re: unit_auto_com_group.lua

Post by Pendrokar »

Too easy. Not worth the effort. :P
User avatar
manolo_
Posts: 1370
Joined: 01 Jul 2008, 00:08

Re: unit_auto_com_group.lua

Post by manolo_ »

User avatar
manolo_
Posts: 1370
Joined: 01 Jul 2008, 00:08

Re: unit_auto_com_group.lua

Post by manolo_ »

still dont working in xta, maybe coz com has different name (?)
User avatar
troycheek
Posts: 80
Joined: 22 May 2009, 19:13

Re: unit_auto_com_group.lua

Post 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.
Last edited by troycheek on 10 Jul 2009, 20:09, edited 1 time in total.
User avatar
manolo_
Posts: 1370
Joined: 01 Jul 2008, 00:08

Re: unit_auto_com_group.lua

Post by manolo_ »

thx to all for your help works fine now
User avatar
manolo_
Posts: 1370
Joined: 01 Jul 2008, 00:08

Re: unit_auto_com_group.lua

Post by manolo_ »

bug:

when your a spectator/in team games, it sets all coms to the definied group
User avatar
troycheek
Posts: 80
Joined: 22 May 2009, 19:13

Re: unit_auto_com_group.lua

Post 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.
User avatar
manolo_
Posts: 1370
Joined: 01 Jul 2008, 00:08

Re: unit_auto_com_group.lua

Post by manolo_ »

couldnt it just detect the unit that is at the startpoint? and could it autpgroup if upgrade my boi?
User avatar
Pendrokar
Posts: 658
Joined: 30 May 2007, 10:45

Re: unit_auto_com_group.lua

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

Return to “Lua Scripts”