Access variables from different directories.

How to access variables on lua from a different directory?


e.g:

plugin file

stuff file
my_file.lua​

morestuff file
the_file_i_want_to_access.lua

​---------------
How would I use a
include("");
to access “the_file_i_want_to_access.lua” from “my_file.lua”

??? help

What specifically do you want to access?

If you were to have a file called A and a file called B in the same addon directory such as :
ADDON NAME\
lua\
autorun\
B.lua
client
server\
A.lua

In A you can do something like “include(“B.lua”)” then use a variable from it.

EXAMPLE

B :




people = {
    "Bob",
    "Josh",
    "Kailey",
}



A :




AddCSLuaFile("B.lua")

include("B.lua")

print(people)



You should be able to print “people” fine in file “A” as long as file “B” gets called first

[editline]16th August 2017[/editline]

If you did:
[Lua]

local people = {
“Bob”,
“Josh”,
“Kailey”,
}

[/Lua]
In file “B” then it wouldn’t work because “local” means it only exists in that file

[editline]16th August 2017[/editline]

I wanted to access a variable from the lua file, I’ve removed local from the variable and used the include function.

It’s still printing the error in the console:
Couldn’t include file ‘cl_data’ (File not found) (@gamemodes/cwhl2rp/plugins/betterviewdata/plugin/entities/entities/cw_terminal/init.lua (line 7))

any ideas?

I want to access a lua file and varaible from:
(@gamemodes/cwhl2rp/plugins/betterviewdata/plugin/entities/entities/cw_terminal/init.lua (line 7))
here

and the lua i’m running is in:
(@gamemodes/cwhl2rp/plugins/betterviewdata/plugin/entities/entities/cw_terminal/init.lua (line 7))
here

You can always use file.Find, to define the exact location, and render it in gmod (by this I mean, it figures out that file exists, then it allows you to include it).

[editline]17th August 2017[/editline]

Example;


local files, directories = file.Find( "/cwhl2rp/plugins/betterviewdata/plugin/entities/entities/cw_terminal/init.lua", "GMOD" )

AddCSLuaFile( files )
include( files )

Now if this doesn’t work, it usually means the files arnt being added (and you put the file in gamemode/game/plugin. Try putting it in gamemode/game/gamemode/plugin, as the “LUA” files are getting rendered as LUA and don’t need to be manually included.

This is my code:
local files, directories = file.Find( “/cwhl2rp/plugins/betterviewdata/plugin/derma/cl_data.lua”, “GMOD” );

AddCSLuaFile(“cl_data”);
AddCSLuaFile(“cl_init.lua”);
AddCSLuaFile(“shared.lua”);
include(“shared.lua”);
include(“cl_data”);

This is the variable:
Clockwork.player:Notify(player, "Loyalty Points: " … loyaltyPoints … "
");
Clockwork.player:Notify(player, "Verdict Points: " … penaltyPoints … "
");

It’s not printing I’m so confused.

btw loyaltypoints and penaltypoints variable are from the cl_data.lua. the one im trying to access


local files, directories = file.Find( "/cwhl2rp/plugins/betterviewdata/plugin/derma/*.lua", "GMOD" );

for k,v in pairs( files ) do
    AddCSLuaFile(v);
    include(v);
end
include("shared.lua");

That will include and add all files in the location, but where are you running this code? Because well if its not in a lua section like gamemodes/game/gamemode/init.lua its not going to run, and makesure you put AddCSLuaFile() at the top of the file your running so it becomes a lua file.

I’m not sure, but i believe Notify only work serversided. If your code is intended to be used clientside make sure you addcsluafile those strings and do networking for your messages

If your strings/vars etc are not local you can easily call them from another lua file.

[ERROR] gamemodes/cwhl2rp/plugins/betterviewdata/plugin/entities/entities/cw_terminal/init.lua:12: bad argument #1 to ‘pairs’ (table expected, got nil)

  1. pairs - [C]:-1
  2. unknown - gamemodes/cwhl2rp/plugins/betterviewdata/plugin/entities/entities/cw_terminal/init.lua:12
    3. include - [C]:-1
    4. IncludeEntities - gamemodes/clockwork/framework/libraries/sh_plugin.lua:600
    5. IncludeExtras - gamemodes/clockwork/framework/libraries/sh_plugin.lua:697
    6. __Register - gamemodes/clockwork/framework/libraries/sh_plugin.lua:321
    7. Register - [string “CloudAuthX”]:1645
    8. Register - gamemodes/clockwork/framework/libraries/sh_plugin.lua:119
    9. Include - gamemodes/clockwork/framework/libraries/sh_plugin.lua:475
    10. IncludePlugins - gamemodes/clockwork/framework/libraries/sh_plugin.lua:682
    11. __Register - gamemodes/clockwork/framework/libraries/sh_plugin.lua:352
    12. Register - [string “CloudAuthX”]:1645
    13. Register -

All we can see from that is that a function that was supposed to get a table was given nil instead.
We don’t have those add-ons, we don’t have your code, we don’t know what you did you make that error.
You know more about it then us and that error tells you what went wrong and where to look if you actually read it. It tells you the exact line of code to look and every line of code from the start that caused the error