unitid problems cmd.patrol

unitid problems cmd.patrol

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

Moderators: Moderators, Moderators

Post Reply
Flash
Posts: 32
Joined: 12 Dec 2010, 18:03

unitid problems cmd.patrol

Post by Flash »

Hi all,
im tryin to create a widget to place patrol and reclaim commands to mousebutton 4 and 5.
ingame this error occurs---> Error in mousepress () ... ...() bad unitid in parameter giveordertounit


function widget:GetInfo()
return {
name = "Mouse Reclaim/Patrol",
desc = "Use mousebuttons 4 and 5 for Reclaim/Patrol",
author = "Flash",
version = "v1.0",
date = "Dez, 2010",
license = "GNU GPL, v3 or later",
layer = 200,
enabled = true,
}
end

local spGetMyPlayerID = Spring.GetMyPlayerID
local spGetPlayerControlledUnit = Spring.GetPlayerControlledUnit
local GetPlayerControlledUnit = spGetPlayerControlledUnit
local GetMyPlayerID = spGetMyPlayerID


local function GetUnitID()
local unitID = GetPlayerControlledUnit(GetMyPlayerID())
return unitID
end

function widget:MousePress(mx, my, button)
local alt,ctrl,meta,shift = Spring.GetModKeyState()

-- Spring.Echo("Button pressed: " .. button)
if (button == 4) then
Spring.GiveOrderToUnit(GetUnitID(), CMD.PATROL);
return true
elseif (button == 5) then
Spring.GiveOrderToUnit(GetUnitID(), CMD.RECLAIM);
return true
end
return false
end


can anyone help me?
0 x

User avatar
aegis
Posts: 2456
Joined: 11 Jul 2007, 17:47

Re: unitid problems cmd.patrol

Post by aegis »

isn't GetPlayerControlledUnit for FPS'd units or something?
you could do GiveOrderToUnitArray() and use GetSelectedUnits()
0 x

User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6115
Joined: 29 Apr 2005, 01:14

Re: unitid problems cmd.patrol

Post by FLOZi »

GetPlayerControlledUnit only returns a unitID if a player is direct-controlling it (c-button FPS mode).

Also CMD.RECLAIM is CmdType CMDTYPE.ICON_UNIT_FEATURE_OR_AREA and CMD.PATROL is CmdType CMD.ICON_MAP, so when issuing the orders, you need to pass some parameters...

(HINT: you'll probably want to use

Code: Select all

Spring.TraceScreenRay 
 ( number mouseX, number mouseY [, boolean onlyCoords [, boolean useMinimap] ] )
   -> nil |
   string "unit",    number unitID    |
   string "feature", number featureID |
   string "ground",  { [1] = number x, [2] = number y, [3] = number z }
0 x

User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: unitid problems cmd.patrol

Post by knorke »

you basically want the extra mousebuttons to work as "P" on the keyboard? ie click mouse 4, cursor changes to patrol.

because GiveOrderToUnit wants more parameters for ie a patrol command. (x y z - location)
you probally do not want GiveOrderToUnit but something like ChangeActiveCommand <- no what its actually named or if exits.
0 x

User avatar
aegis
Posts: 2456
Joined: 11 Jul 2007, 17:47

Re: unitid problems cmd.patrol

Post by aegis »

good point, knorke

Spring.SetActiveCommand
( string action [, string actionExtra ] ) -> nil | boolean
( number cmdIndex [, number button = 1
[, boolean leftClick, boolean rightClick,
boolean alt, boolean ctrl, boolean meta, boolean shift]] ) -> nil | boolean

-- so maybe this?
Spring.SetActiveCommand(CMD.RECLAIM)
Spring.SetActiveCommand(CMD.PATROL)
0 x

Flash
Posts: 32
Joined: 12 Dec 2010, 18:03

Re: unitid problems cmd.patrol

Post by Flash »

ok thx guys. ill try my best :)
ill give you feedback asap.
0 x

Flash
Posts: 32
Joined: 12 Dec 2010, 18:03

Re: unitid problems cmd.patrol

Post by Flash »

Spring.SetActiveCommand("Patrol") works thx!


function widget:GetInfo()
return {
name = "Mouse Reclaim/Patrol",
desc = "Use mousebuttons 4 and 5 for Reclaim/Patrol",
author = "Flash",
version = "v1.0",
date = "Dez, 2010",
license = "GNU GPL, v3 or later",
layer = 200,
enabled = true,
}
end



function widget:MousePress(mx, my, button)
local alt,ctrl,meta,shift = Spring.GetModKeyState()

-- Spring.Echo("Button pressed: " .. button)
if (button == 4) then
Spring.SetActiveCommand("Patrol");
return true
elseif (button == 5) then
Spring.SetActiveCommand("Reclaim");
return true
end
return false
end


would it be possible for units who cant reclaim to set command "Attack" or sth? (i.e. bombers, berthas, nukes...)
0 x

User avatar
aegis
Posts: 2456
Joined: 11 Jul 2007, 17:47

Re: unitid problems cmd.patrol

Post by aegis »

if SetActiveCommand only sets the command if the selected units are capable of doing the command, then you can just GetActiveCommand afterwards and see if it was successfully set.

if this works and it wasn't set, you can try the next command (so try reclaim, if reclaim isn't selected, then try attack)

of course, this will only work if spring cares about SetActiveCommand applying to your selected units.
0 x

Flash
Posts: 32
Joined: 12 Dec 2010, 18:03

Re: unitid problems cmd.patrol

Post by Flash »

Spring.SetActiveCommand("Reclaim");
local zahl1, zahl2, zahl3, aCommand = Spring.GetActiveCommand()
if(aCommand == "Reclaim") then

else
Spring.SetActiveCommand("Attack");


works. thx aegis

dl link: http://www.springfiles.com/show_file.php?id=2881
0 x

User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6115
Joined: 29 Apr 2005, 01:14

Re: unitid problems cmd.patrol

Post by FLOZi »

Some tips; You can use the _ operator to discard return values you don't need:

Code: Select all

local _, _, _, aCommand = Spring.GetActiveCommand()
And if you want to be even neater, the select keyword:

Code: Select all

local aCommand = select(4, Spring.GetActiveCommand())
:-)
0 x

Post Reply

Return to “Lua Scripts”