Loading...
  OR  Zero-K Name:    Password:   

LUA question: How to get attached unit instance

3 posts, 336 views
Post comment
Filter:    Player:  
sort
6 years ago
LUA newbie here.

I want to modify a unit when it is created based on some parameter like teamID or the number of units of that class already on the map.

I'm assuming this is done in function script.Create() for the unit class's LUA script because I assume that function is called every time a unit of that type is created. What I don't know is how to get the unit that the script is being called from. All the examples I have seen use UNITID as a parameter or something like that. Any idea of how to do this?
+0 / -0

6 years ago
Scripts have an implicit `unitID` global value that you can use to tell which unit is running the script. If going this way `script.Create` would indeed be the correct place to do stuff.

The other way would be to make a LuaRules gadget and use the UnitCreated callin (which receives the unit ID as the first parameter). Use this if your mechanic is not limited by design to a single unit type.
+0 / -0


6 years ago
Generally I think it is better to use a gadget instead of a unit script. I only like to use scripts for mechanics which are localized to the unit, and that barely interact with other systems.
+0 / -0