yodosha Posted January 27, 2013 Posted January 27, 2013 Hi, I'm trying to make a simple script in Lua which incorporates the "number function Unit.getNumber(Unit self)" function, but whether I try to call in the form: "someunittype:getNumber()" or "Unit.getNumber(someunittype)" I get the same error: Common/LuaClass.lua 21st line: attempt to index local 'self' (a number value) Now, this error message doesn't say much to me especially since it is in a file which is a mystery what it's doing. (for me that is) I feel it's something extremely basic I'm missing here, but what is it? Cheers
Speed Posted January 27, 2013 Posted January 27, 2013 (edited) Hi, I'm trying to make a simple script in Lua which incorporates the "number function Unit.getNumber(Unit self)" function, but whether I try to call in the form: "someunittype:getNumber()" or "Unit.getNumber(someunittype)" I get the same error: Common/LuaClass.lua 21st line: attempt to index local 'self' (a number value) Now, this error message doesn't say much to me especially since it is in a file which is a mystery what it's doing. (for me that is) I feel it's something extremely basic I'm missing here, but what is it? Cheers Unit.getNumber must be called on a object of the Unit class (object, in this term, means the OOP definition of the word "object"). Each in-game unit has a Unit class object associated with it. One way (but not the ONLY way!) to get this Unit object is with Unit.getByName, where you specify the name you gave the unit in the mission editor. So, for example, do local playerUnit = Unit.getByName('Hawg 1-1') if playerUnit then local unitNum = playerUnit:getNumber() --<The rest of your code follows...> end end Unit.getNumber is one of the less-used Lua functions; oh, I can certainly think of examples that make it necessary to use, but in the maybe... 20,000? lines of Lua I have written for DCS since 2010, I can't remember a single time I ever used it, aside from testing it to make sure it really works correctly. Oh you might notice that, instead of using Unit.getNumber, I'm doing playerUnit:getNumber(). For most intents and purposes, playerUnit:getNumber() is identical Unit.getNumber(playerUnit), as you seem to already understand. It's just that playerUnit:getNumber() is easier to write (also, it demonstrates that you understand OOP programming and are using it as it is intended to be used). To understand the minor difference that does exist between Unit.getNumber(playerUnit) and playerUnit:getNumber(), you'd have to understand how Lua metatables work (particularly, __index). Also note that, since Unit.getByName does NOT accept a self variable of type Unit, it CANNOT be used like this <something>:getByName()- that doesn't even make sense. It must ALWAYS be used as Unit.getByName(string name). Edited January 27, 2013 by Speed Intelligent discourse can only begin with the honest admission of your own fallibility. Member of the Virtual Tactical Air Group: http://vtacticalairgroup.com/ Lua scripts and mods: MIssion Scripting Tools (Mist): http://forums.eagle.ru/showthread.php?t=98616 Slmod version 7.0 for DCS: World: http://forums.eagle.ru/showthread.php?t=80979 Now includes remote server administration tools for kicking, banning, loading missions, etc.
Druid_ Posted January 27, 2013 Posted January 27, 2013 (edited) Self is not the name of the unit! It's the object name, you have to use the function Unit.getByName() to get it. Local unit = Unit.getByName('name of unit in ME') Local unitnum = Unit.getNumber(unit) Print(unitnum) -- Will show the number of the unit in its group as set in the ME You will need to check that unitnum ~= nil else an error will be created (this will happen if the unit it is destroyed). EDIT: sniped by Speed. Edited January 27, 2013 by Druid_ i7-7700K : 16Gb DDR4 2800 Mhz : Asus Mobo : 2TB HDD : Intel 520 SSD 240gb : RTX 2080ti: Win10 64pro : Dx10 : TrackiR4 : TM Warthog : ASUS ROG SWIFT PG348Q
Recommended Posts