Page 1 of 1

Change a unit's attributes during game

Posted: 08 Feb 2012, 20:16
by super_g
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.

Re: Change a unit's attributes during game

Posted: 08 Feb 2012, 20:29
by knorke
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.

Re: Change a unit's attributes during game

Posted: 08 Feb 2012, 21:38
by super_g
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.

Re: Change a unit's attributes during game

Posted: 08 Feb 2012, 21:56
by knorke
do you try this in a gadget or unit script?
because elsewhere it will not work.

Re: Change a unit's attributes during game

Posted: 09 Feb 2012, 09:23
by Google_Frog
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.

Re: Change a unit's attributes during game

Posted: 09 Feb 2012, 22:09
by super_g
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.

Re: Change a unit's attributes during game

Posted: 09 Feb 2012, 22:28
by gajop
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

Code: Select all

    Spring.SendLuaRulesMsg("scenedit|addUnit|" .. unitDef .. "|" .. 
        x .. "|" .. y .. "|" .. z .. "|" .. playerId)
--gadget code

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
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:

Code: Select all

        GG.Delay.DelayCall(Spring.CreateUnit, {par1, par2, par3, par4, 0, tonumber(par5)})
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

Posted: 10 Feb 2012, 01:40
by knorke
PS: gadgets may be able to edit unit's attributes, but not sure if they can do so for unitdefs
They can't. If you want that you have to manually loop through all units that you want to edit.

Re: Change a unit's attributes during game

Posted: 10 Feb 2012, 05:53
by super_g
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?

Re: Change a unit's attributes during game

Posted: 10 Feb 2012, 08:23
by FLOZi
Is it an aircraft?

Re: Change a unit's attributes during game

Posted: 10 Feb 2012, 14:52
by super_g
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

Posted: 10 Feb 2012, 15:34
by knorke
what mod and what is the unitname?

Re: Change a unit's attributes during game

Posted: 10 Feb 2012, 16:45
by smoth
FLOZi wrote:Is it an aircraft?
no, go fish.

Re: Change a unit's attributes during game

Posted: 10 Feb 2012, 20:27
by FLOZi
My guess is that it is a mobile unit without a valid movementClass then