Change a unit's attributes during game

Change a unit's attributes during game

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

Moderator: Moderators

Post Reply
super_g
Posts: 13
Joined: 12 Jan 2012, 04:10

Change a unit's attributes during game

Post 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.
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Change a unit's attributes during game

Post 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.
super_g
Posts: 13
Joined: 12 Jan 2012, 04:10

Re: Change a unit's attributes during game

Post 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.
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Change a unit's attributes during game

Post by knorke »

do you try this in a gadget or unit script?
because elsewhere it will not work.
Google_Frog
Moderator
Posts: 2464
Joined: 12 Oct 2007, 09:24

Re: Change a unit's attributes during game

Post 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.
super_g
Posts: 13
Joined: 12 Jan 2012, 04:10

Re: Change a unit's attributes during game

Post 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.
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: Change a unit's attributes during game

Post 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
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Change a unit's attributes during game

Post 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.
super_g
Posts: 13
Joined: 12 Jan 2012, 04:10

Re: Change a unit's attributes during game

Post 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?
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6241
Joined: 29 Apr 2005, 01:14

Re: Change a unit's attributes during game

Post by FLOZi »

Is it an aircraft?
super_g
Posts: 13
Joined: 12 Jan 2012, 04:10

Re: Change a unit's attributes during game

Post by super_g »

No, it is a ground unit as far as I know. The unit walks along the ground anyway.
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Change a unit's attributes during game

Post by knorke »

what mod and what is the unitname?
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: Change a unit's attributes during game

Post by smoth »

FLOZi wrote:Is it an aircraft?
no, go fish.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6241
Joined: 29 Apr 2005, 01:14

Re: Change a unit's attributes during game

Post by FLOZi »

My guess is that it is a mobile unit without a valid movementClass then
Post Reply

Return to “Lua Scripts”