Say I want to pass an array from one lua file to another, for example:
I want to pass 'item' from the code below:
[lua]
item ={}
item.milk =
{
class = "drink_milk",
model = "models/props_junk/garbage_milkcarton002a.mdl",
cost = 1,
}
[/lua]
[lua]
local items = item
for k, v in pairs(items) do
local icon = vgui.Create("SpawnIcon", MainMenu)
icon:SetModel(v.itemModel)
icon.DoClick = function(icon)
RunConsoleCommand("spawn_entity", v.itemClass)
end
end
[/lua]
[lua]include("items_var_file.lua")[/lua]Goes into the file that needs to use the variables. The keyword 'local' prevents variables from transferring across files in this manner.
I had actually tried that but I had a few errors that were blocking one file from being read.
Thanks a lot anyway.
[QUOTE=joyenusi;35759847]I had actually tried that but I had a few errors that were blocking one file from being read.
Thanks a lot anyway.[/QUOTE]
Are you doing this in GLua (Garry's Mod version of Lua) or vanilla Lua?
If you're doing this in vanilla Lua, you'll have a problem because, IIRC, vanilla Lua doesn't have 'include'. You'd need to use
[lua]require "items_var_file.lua"[/lua]
then.
I'm using GLua, I got it working, thanks though.
I had already tried the include, it just didn't work because I forgot a '}' on one of my tables.
Sorry, you need to Log In to post a reply to this thread.