Is this normally?
Code:local itemDef = global.ItemManager.FindItemDefinition("rifle.bolt")With itemID as argument I get the same error.Code:[Oxide] 2:20 PM [Error] Failed to run a 1.00 timer File: test.lua Line: 154 attempt to call field 'FindItemDefinition' (a table value): at NLua.Lua.ThrowExceptionFromError (Int32 oldTop) [0x00000] in <filename unknown>:0 at NLua.Lua.CallFunction (System.Object function, System.Object[] args, System.Type[] returnTypes) [0x00000] in <filename unknown>:0 at NLua.LuaFunction.Call (System.Object[] args, System.Type[] returnTypes) [0x00000] in <filename unknown>:0 at NLua.Method.LuaDelegate.CallFunction (System.Object[] args, System.Object[] inArgs, System.Int32[] outArgs) [0x00000] in <filename unknown>:0 at LuaGeneratedClass1.CallFunction () [0x00000] in <filename unknown>:0 at Oxide.Core.Libraries.Timer+TimerInstance.Update () [0x00000] in <filename unknown>:0
Solved Can't get item definition
Discussion in 'Rust Development' started by Chilli, Oct 10, 2015.
-
in LUA it is
Code:global.ItemManager:FindItemDefinition("rifle.bolt")
-
-
There are 2 FindItemDefinition methods, 1 that takes an itemid argument and the other one a shortname. When a method has one or more overloaded methods, the lua bindings generate an array for that method containing all theses methods which means that the way you call them changes.
So you would grab the FindItemDefinition method by using
Code:... local FindItemDefinition = global.ItemManager.FindItemDefinition.methodarray[0] ...
Code:... local FindItemDefinition = global.ItemManager.FindItemDefinition.methodarray[0] print(FindItemDefinition) ...
Code:... local FindItemDefinition = global.ItemManager.FindItemDefinition.methodarray[1] ... function PLUGIN:FindItemDef(shortname) local arr = util.TableToArray( { shortname } ) local item = FindItemDefinition:Invoke( nil, arr ) return item end ...
-
Thank you. I had never met methodarray[] in the lua. This from C#?
As I understand in this case methodarray[0] = FindItemDefinition(Int32) and methodarray[1] = FindItemDefinition(string).
Indexing in lua should begin with 1 or not? Also, methodarray[0] doesn't work with nuber value. -
I don't think its from there.
The index should start with 0. -
Lua however doesn't use types like C# does and doesn't distinguish between function abc(letter, letter) and abc(number, letter). Because of this an array is created in the engine which stores all the available overloaded methods, which you can then invoke using the example I gave before on how to use the FindItemDefinition method. -
I know what is the function overloading. I just never interpreted overloaded function as an array.
I wanted to know source of the methodarray[] field/property.
And one question still actual – why it doesn't work?
Code:local data = datafile.GetDataTable("test") local itemDef = global.ItemManager.FindItemDefinition.methodarray[0]:Invoke(nil, util.TableToArray({data.itemID})) -- itemID is number value
-
Code:local arr = util.TableToArray( { itemid } ) util.ConvertAndSetOnArray( arr, 0, itemid, System.Int32._type ) local ItemDef = FindItemDefinitionId:Invoke( nil, arr )
-
Usually using number value instead Int32 (float, double, etc.) is possible. Now I will do this "explicit conversion" in similar situations.
Thanks for explaining.