View topic - Change a unit's attributes during game



All times are UTC + 1 hour


Post new topic Reply to topic  [ 14 posts ] 
Author Message
PostPosted: 08 Feb 2012, 20:16 

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


Top
 Offline Profile  
 
PostPosted: 08 Feb 2012, 20:29 
Moderator
User avatar

Joined: 22 Feb 2006, 01:02
Location: cheap kitchen
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.


Top
 Offline Profile  
 
PostPosted: 08 Feb 2012, 21:38 

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


Top
 Offline Profile  
 
PostPosted: 08 Feb 2012, 21:56 
Moderator
User avatar

Joined: 22 Feb 2006, 01:02
Location: cheap kitchen
do you try this in a gadget or unit script?
because elsewhere it will not work.


Top
 Offline Profile  
 
PostPosted: 09 Feb 2012, 09:23 
Moderator

Joined: 12 Oct 2007, 08:24
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.


Top
 Offline Profile  
 
PostPosted: 09 Feb 2012, 22:09 

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


Top
 Offline Profile  
 
PostPosted: 09 Feb 2012, 22:28 
Moderator

Joined: 05 Aug 2009, 19:42
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:
    Spring.SendLuaRulesMsg("scenedit|addUnit|" .. unitDef .. "|" ..
        x .. "|" .. y .. "|" .. z .. "|" .. playerId)


--gadget code
Code:
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:
        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


Top
 Offline Profile  
 
PostPosted: 10 Feb 2012, 01:40 
Moderator
User avatar

Joined: 22 Feb 2006, 01:02
Location: cheap kitchen
Quote:
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.


Top
 Offline Profile  
 
PostPosted: 10 Feb 2012, 05:53 

Joined: 12 Jan 2012, 04:10
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?


Top
 Offline Profile  
 
PostPosted: 10 Feb 2012, 08:23 
Moderator
User avatar

Joined: 29 Apr 2005, 00:14
Location: #moddev - join it!
Is it an aircraft?


Top
 Online Profile  
 
PostPosted: 10 Feb 2012, 14:52 

Joined: 12 Jan 2012, 04:10
No, it is a ground unit as far as I know. The unit walks along the ground anyway.


Top
 Offline Profile  
 
PostPosted: 10 Feb 2012, 15:34 
Moderator
User avatar

Joined: 22 Feb 2006, 01:02
Location: cheap kitchen
what mod and what is the unitname?


Top
 Offline Profile  
 
PostPosted: 10 Feb 2012, 16:45 
Content Developer
User avatar

Joined: 13 Jan 2005, 00:46
Location: ModalitÃ
FLOZi wrote:
Is it an aircraft?

no, go fish.


Top
 Offline Profile  
 
PostPosted: 10 Feb 2012, 20:27 
Moderator
User avatar

Joined: 29 Apr 2005, 00:14
Location: #moddev - join it!
My guess is that it is a mobile unit without a valid movementClass then


Top
 Online Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group

Site layout created by Roflcopter et al.