Lua tables in defination script[SOLVED]

  • #1, by red363Thursday, 15. June 2023, 04:58 11 months ago
    Hello!

    I use tables in my scripts. The question arose, is it possible and how to create a table in the definition script and refer to it in all these scripts? (In order not to duplicate the same table in different scripts)

    Does it have to be placed in a function so that it can be referenced or what?

    Thanks

    Forum Fan

    101 Posts


  • #2, by afrlmeThursday, 15. June 2023, 13:52 11 months ago
    Don't create it as local & you can access it in any script you like.

    t_example = {}

    instead of...

    local t_example = {}

    Just make sure you use an original name for the table as it can be accessed/updated from anywhere.

    Also, I would really recommend joining our discord server, if you aren't already on it as you will get a much faster response there.

    Imperator

    7278 Posts

  • #3, by red363Thursday, 15. June 2023, 16:04 11 months ago
    Cool, thanks. Ok.

    A follow-up question...

    After the tables were translated into global ones, the names of the objects stopped responding to the selected language. (in the tables, the names of the objects that they take from the tab of the object's properties, i.e. their name for each language).

    There was no such problem with local tables...
    What could be the problem?

    example:

    t_items_nam = {
    [1] = Objects["itm1"]:getTextStr(VObjectName),
    [2] = Objects["itm2"]:getTextStr(VObjectName),
    [3] = Objects["itm3"]:getTextStr(VObjectName),
    [4] = Objects["itm4"]:getTextStr(VObjectName),
    [5] = Objects["itm5"]:getTextStr(VObjectName),
    [6] = Objects["itm6"]:getTextStr(VObjectName),
    [7] = Objects["itm7"]:getTextStr(VObjectName),
    [8] = Objects["itm8"]:getTextStr(VObjectName),
    [9] = Objects["itm9"]:getTextStr(VObjectName),
    [10] = Objects["itm10"]:getTextStr(VObjectName),
    [11] = Objects["itm11"]:getTextStr(VObjectName),
    [12] = Objects["itm12"]:getTextStr(VObjectName)
    }

    Forum Fan

    101 Posts

  • #4, by afrlmeThursday, 15. June 2023, 16:32 11 months ago
    Hmm, not really sure why that would be. Do you have multiple objects in the project with the same names? Was there anything in the log about it? At a guess, I'd say it was either getting confused with which to access or it was trying to access them before the game has finished loading.

    Also, you don't need to include the index numbers in brackets when you write tables like that...

    t_items_nam = {
    
    
    
    Objects["itm1"],
    
    Objects["itm2"],
    
    Objects["itm3"],
    
    Objects["itm4"],
    
    Objects["itm5"],
    
    Objects["itm6"],
    
    Objects["itm7"],
    
    Objects["itm8"],
    
    Objects["itm9"],
    
    Objects["itm10"],
    
    Objects["itm11"],
    
    Objects["itm12"]
    
    
    
    }

    I would recommend querying the textStr function when you want to grab the text as that way it will be accessed when you need it instead of storing the data in the table at the beginning - just in case you are updating the strings belonging to the values.
    print( t_items_nam[1]:getTextStr(VObjectName) )

    You could also use iteration if you want to access & print out each of them - assuming all the values are actually named like you've named them in the table above...

    for i = 1, 12 do
    
     print( Objects["itm" .. i]:getTextStr(VObjectName) )
    
    end

    Imperator

    7278 Posts

  • #5, by red363Thursday, 15. June 2023, 17:48 11 months ago
    It's ok, I found the error, Thanks

    Forum Fan

    101 Posts

Write post