Solved Can't get item definition

Discussion in 'Rust Development' started by Chilli, Oct 10, 2015.

  1. Is this normally?
    Code:
    local itemDef = global.ItemManager.FindItemDefinition("rifle.bolt")
    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
    With itemID as argument I get the same error.
     
    Last edited by a moderator: Oct 10, 2015
  2. in LUA it is
    Code:
    global.ItemManager:FindItemDefinition("rifle.bolt")
     
  3. Already tried, same error.
     
  4. 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]
    ...
    But because there isn't really a way to tell which method you took you want to print it and see if it matches the one you want to use:
    Code:
    ...
    local FindItemDefinition = global.ItemManager.FindItemDefinition.methodarray[0]
    print(FindItemDefinition)
    ...
    As soon as you know the correct index to use, in this case index 1, you can call the method in your lua plugin:
    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
    ...
     
  5. 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.
     
  6. You don't need to do such in C#.
    I don't think its from there.
    The index should start with 0.
     
  7. Not what he's saying/asking ;)
    The Lua extension automatically generates Lua functions for all the public methods that are in the game's DLLs, that is why you can access some methods by just using global.BasePlayer:Find("Mughisi") and such. Some methods however have multiple variations, these are overloaded members, basically methods with the same name but with a different parameter list. Another example of this can be found in the Physics class in the UnityEngine namespace. You have multiple methods here that have a the same name, but a different parameter list.

    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.
     
  8. 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
     
  9. That's what I just explained :)
    The method you are trying to invoke requires a value of the type Int32, and a number in Lua isn't of that type, therefor you need to cast that number to the correct type. This is something you can do using the util method util.ConvertAndSetOnArray(array, index, value, target type).
    Code:
    local arr = util.TableToArray( { itemid } )
    util.ConvertAndSetOnArray( arr, 0, itemid, System.Int32._type )
    local ItemDef = FindItemDefinitionId:Invoke( nil, arr )
     
  10. Usually using number value instead Int32 (float, double, etc.) is possible. Now I will do this "explicit conversion" in similar situations.
    Thanks for explaining.