I've been working on a custom command for the "locator" unit in my game. What I'm trying to do right now is make it act as the move command. (Head to the coords of the map I click on.)
What I'm confused about is that whenever I call the command, this happens. The unit walks to the same exact area in the upper left area of the map after the game freezes for about 5 seconds.
What works is Spring.Echo() when I call the command.
My infolog after testing is here. Please disregard line 77-154 as they are due to empty files in the directory.
My gadget code is here. I've commented most of it, which hopefully helps understand what I'm trying to do.
I suspect the problem is where I've attempted to create unsynced code in the middle of the commandfallback function, but I'm not sure how else I'd be able to add that just for Spring.GetMouseState.
I'm not sure what to do at this point as I've attempted to fix the code in many different ways.
Any assistance is appreciated.
Issue With Ordering a Command
Moderator: Moderators
Re: Issue With Ordering a Command
One thing is in unsynced you do this to get mouse coordinates:
and then try to use it in synced:
but to get variables across the sync/unsync it needs some special stuff explained on a page that was taken down.
Maybe the unit tries to walk to upper left corner because x, z are both nil (since no special border crossing stuff used) which is maybe interpreted as 0:0 = upper left corner.
Good luck it does not need this anyway!
Custom commands with type = CMDTYPE.ICON_MAP work in two steps:
1) click button
2) click somewhere on map
The coordinates from 2) can be read from the command event.
So the mouse cursor stuff is not needed and you dont even have to worry if the player clicks outside the map because Spring will automagically block that.
If you put below code into your game it will not work because other gadgets or gadgethandler make all kind of errors, the lua does not like that and dies completly.
But if you put it into http://springrts.com/wiki/SpringTutorialGame it should work. Use /give simplehover to test.
Note that after /luarules reload the custom buttons disappear.
(ofc can readd them too)
Difference between CommandFallback & AllowCommand Idk.
Also try adding this:It changes the mouse cursor when your command is in "step 1" which makes it easier to see/understand.
Code: Select all
x,z = Spring.GetMouseState(mousex, mousey)
Code: Select all
Spring.GiveOrderToUnit(unitID, CMD.MOVE , {x,92,z },
Maybe the unit tries to walk to upper left corner because x, z are both nil (since no special border crossing stuff used) which is maybe interpreted as 0:0 = upper left corner.
Good luck it does not need this anyway!
Custom commands with type = CMDTYPE.ICON_MAP work in two steps:
1) click button
2) click somewhere on map
The coordinates from 2) can be read from the command event.
So the mouse cursor stuff is not needed and you dont even have to worry if the player clicks outside the map because Spring will automagically block that.
If you put below code into your game it will not work because other gadgets or gadgethandler make all kind of errors, the lua does not like that and dies completly.
But if you put it into http://springrts.com/wiki/SpringTutorialGame it should work. Use /give simplehover to test.

Code: Select all
if (not gadgetHandler:IsSyncedCode()) then return end
local LOCATOR = {
[UnitDefNames["simplehover"].id] = true,
}
local CMD_STRT_LOC = 34581
local locateCmd = {
id = CMD_STRT_LOC,
name = "Locate",
action = "locate",
type = CMDTYPE.ICON_MAP,
tooltip = "Commands the locator to locate.",
}
function gadget:UnitCreated(unitID, unitDefID, unitTeam, builderID)
if LOCATOR[unitDefID] then
Spring.InsertUnitCmdDesc(unitID, locateCmd) --you had a "500" there, dunno what that was supposed to do
end
end
function gadget:AllowCommand(unitID, unitDefID, unitTeam, cmdID, cmdParams, cmdOptions, cmdTag)
if cmdID == CMD_STRT_LOC then --is beloved our custom command?
if LOCATOR[unitDefID] then --is it a unit that should care about our custom command?
local x = cmdParams[1]
local y = cmdParams[2]
local z = cmdParams[3]
--see http://springrts.com/wiki/Lua_CMDs#CMDTYPE.ICON_MAP
Spring.GiveOrderToUnit(unitID, CMD.MOVE , {x,y,z }, {})
--Spring.SetUnitPosition (unitID, x,y,z) --uncomment for teleport!
end
end
return true --return false can be used to block commands but dont want to block anything, so always return true
end
(ofc can readd them too)
Difference between CommandFallback & AllowCommand Idk.
Also try adding this:
Code: Select all
function gadget:Initialize()
Spring.AssignMouseCursor ("Locate", "cursorattack")
end
Re: Issue With Ordering a Command
One question: Why don't you want to use the regular move command?
- Automatics
- Posts: 15
- Joined: 04 Aug 2011, 08:37
Re: Issue With Ordering a Command
Because it's not only supposed to be a move cmd.
So on line 76 here: http://pastebin.com/Jte6F8Rq , I'm trying to check if the unit given a command is a specified unit (locator), but it seems I'm not finding the correct unitID if I am finding one at all.
Anybody know what I'm doing wrong? I at least want to make sure I can make the Spring.Echo work if it's the correct unitID.
Thanks in advance.
So on line 76 here: http://pastebin.com/Jte6F8Rq , I'm trying to check if the unit given a command is a specified unit (locator), but it seems I'm not finding the correct unitID if I am finding one at all.
Anybody know what I'm doing wrong? I at least want to make sure I can make the Spring.Echo work if it's the correct unitID.
Thanks in advance.
Re: Issue With Ordering a Command
There is ~two ways to do it:I'm trying to check if the unit given a command is a specified unit (locator)
you can check if the adressed unit is the correct TYPE of unit:
if LOCATOR[unitDefID] then
That way I already posted and is in your version too. It does not work because you tried to add a 2nd way, but incomplete:
2nd way is to keep a list of all alive lolcators and check if the adressed unit is in this list.
check part is there:
if locatorList[unitID] == unitID
But there is nothing that adds lolcators to locatorList.
spoiler: http://pastebin.com/AULMZvvr
Also there is still all those other Lua errors, with that you can not be quite sure if your new gadget is broken or if the whole Lua thing has already crashed.