Change a unit's attributes during game
Moderator: Moderators
Change a unit's attributes during game
Is there any way to change a unit's canMove attribute while the game is running?
I can get the value of a unit's canMove through the unitDef, but I can not seem to be able to set it to a new value.
local movement = UnitDefs[unitID]["canMove"]
I am looking for a way to toggle a unit's ability to move on and off. Thanks.
I can get the value of a unit's canMove through the unitDef, but I can not seem to be able to set it to a new value.
local movement = UnitDefs[unitID]["canMove"]
I am looking for a way to toggle a unit's ability to move on and off. Thanks.
Re: Change a unit's attributes during game
UnitDefs table is read only.
save original speed & turnrate:
local mt = Spring.GetUnitMoveTypeData(unitID)
local originalspeed = mt.maxSpeed
local originalturnrate = mt.turnRate
make immobile:
Spring.MoveCtrl.SetGroundMoveTypeData(unitID, "maxSpeed", 0)
Spring.MoveCtrl.SetGroundMoveTypeData(unitID, "turnRate",0)
make mobile again:
Spring.MoveCtrl.SetGroundMoveTypeData(unitID, "maxSpeed", originalspeed)
Spring.MoveCtrl.SetGroundMoveTypeData(unitID, "turnRate",originalturnrate)
0 = in 0.82.7 this worked.
In 85.0 it gets you the message:
"Warning: Can not assign key "maxSpeed" to GroundMoveType"
(it will work though, so that warning is imo a bug)
Setting to 0.01 is almost the same as 0 and works without warning.
save original speed & turnrate:
local mt = Spring.GetUnitMoveTypeData(unitID)
local originalspeed = mt.maxSpeed
local originalturnrate = mt.turnRate
make immobile:
Spring.MoveCtrl.SetGroundMoveTypeData(unitID, "maxSpeed", 0)
Spring.MoveCtrl.SetGroundMoveTypeData(unitID, "turnRate",0)
make mobile again:
Spring.MoveCtrl.SetGroundMoveTypeData(unitID, "maxSpeed", originalspeed)
Spring.MoveCtrl.SetGroundMoveTypeData(unitID, "turnRate",originalturnrate)
0 = in 0.82.7 this worked.
In 85.0 it gets you the message:
"Warning: Can not assign key "maxSpeed" to GroundMoveType"
(it will work though, so that warning is imo a bug)
Setting to 0.01 is almost the same as 0 and works without warning.
Re: Change a unit's attributes during game
This is exactly what I need. I tried the following just to test it. I can get the movement values, but an error occurs when the set is called. The error states "attempted to index field 'MoveCtrl' (a nil value)"
for id,unitDef in ipairs(Spring.GetSelectedUnits()) do
local mt = Spring.GetUnitMoveTypeData(unitDef)
local originalspeed = mt.maxSpeed
local originalturnrate = mt.turnRate
Spring.Echo("maxSpeed: "..originalspeed)
Spring.MoveCtrl.SetGroundMoveTypeData(unitDef, "maxSpeed", 0.01)
Spring.MoveCtrl.SetGroundMoveTypeData(unitDef, "turnRate",0.01)
end
I tried to use both id and unitDef from the Spring.GetSelectedUnits() and only unitDef worked with the getter.
Thanks for you help.
for id,unitDef in ipairs(Spring.GetSelectedUnits()) do
local mt = Spring.GetUnitMoveTypeData(unitDef)
local originalspeed = mt.maxSpeed
local originalturnrate = mt.turnRate
Spring.Echo("maxSpeed: "..originalspeed)
Spring.MoveCtrl.SetGroundMoveTypeData(unitDef, "maxSpeed", 0.01)
Spring.MoveCtrl.SetGroundMoveTypeData(unitDef, "turnRate",0.01)
end
I tried to use both id and unitDef from the Spring.GetSelectedUnits() and only unitDef worked with the getter.
Thanks for you help.
Re: Change a unit's attributes during game
do you try this in a gadget or unit script?
because elsewhere it will not work.
because elsewhere it will not work.
-
- Moderator
- Posts: 2464
- Joined: 12 Oct 2007, 09:24
Re: Change a unit's attributes during game
Here is how to do it with a widget:
Spring.GiveOrderToUnit(unitID, CMD.STOP,{},{})
Spring.GiveOrderToUnit(unitID, CMD.MOVESTATE,{0},{})
That is the best you can do as a widget because they cannot change basic unit attributes.
Spring.GiveOrderToUnit(unitID, CMD.STOP,{},{})
Spring.GiveOrderToUnit(unitID, CMD.MOVESTATE,{0},{})
That is the best you can do as a widget because they cannot change basic unit attributes.
Re: Change a unit's attributes during game
Is there a way to make a gadget that modifies unit's attributes and then call the function from a widget?
I have been trying to call a function from a gadget using the following include in my widget.
VFS.Include("LuaRules\Gadgets\modifyUnitAttributes.lua",nil)
I have tried a couple variants, but the widget does not load with the include.
Thanks again.
I have been trying to call a function from a gadget using the following include in my widget.
VFS.Include("LuaRules\Gadgets\modifyUnitAttributes.lua",nil)
I have tried a couple variants, but the widget does not load with the include.
Thanks again.
Re: Change a unit's attributes during game
Even if it did work, you would still be in lua "widget space".
You want to have widget -> gadget communication (not sure if it can go both ways). You do that by sending messages, f.e :
--widget code
--gadget code
Now remember when you do stuff in your gadget, it may not be possible to do it in the RecvLuaMsg function (the Gods of Olymp disagree), so you may need to delay the call, f.e:
PS: gadgets may be able to edit unit's attributes, but not sure if they can do so for unitdefs
You want to have widget -> gadget communication (not sure if it can go both ways). You do that by sending messages, f.e :
--widget code
Code: Select all
Spring.SendLuaRulesMsg("scenedit|addUnit|" .. unitDef .. "|" ..
x .. "|" .. y .. "|" .. z .. "|" .. playerId)
Code: Select all
function gadget:RecvLuaMsg(msg, playerID)
pre = "scenedit"
--if (msg:find(pre,1,true)) then Spring.Echo ("its a loveNtrolls message") end
local data = explode( '|', msg)
if data[1] ~= pre then return end --make sure it's from your widget
-- rest of your code
end
Code: Select all
GG.Delay.DelayCall(Spring.CreateUnit, {par1, par2, par3, par4, 0, tonumber(par5)})
Re: Change a unit's attributes during game
They can't. If you want that you have to manually loop through all units that you want to edit.PS: gadgets may be able to edit unit's attributes, but not sure if they can do so for unitdefs
Re: Change a unit's attributes during game
Thank you for your help.
I am getting a "[string "LuaRules/Gadgets/modifyunitattributes.lua"]:107: Unit does not have a compatible MoveType" error in the gadget when I try to call:
Spring.MoveCtrl.SetGroundMoveTypeData(id, "maxSpeed", 0.01)
Is moveType need to be declared in the unit script? or could there be another reason why the unit would not have a compatible MoveType?
I am getting a "[string "LuaRules/Gadgets/modifyunitattributes.lua"]:107: Unit does not have a compatible MoveType" error in the gadget when I try to call:
Spring.MoveCtrl.SetGroundMoveTypeData(id, "maxSpeed", 0.01)
Is moveType need to be declared in the unit script? or could there be another reason why the unit would not have a compatible MoveType?
Re: Change a unit's attributes during game
Is it an aircraft?
Re: Change a unit's attributes during game
No, it is a ground unit as far as I know. The unit walks along the ground anyway.
Re: Change a unit's attributes during game
what mod and what is the unitname?
Re: Change a unit's attributes during game
no, go fish.FLOZi wrote:Is it an aircraft?
Re: Change a unit's attributes during game
My guess is that it is a mobile unit without a valid movementClass then