Global Table?

While coding a new jail system I’m storing coordinates for jail positions in a global table defined in gamemodes/AbyssRP/gamemode/jailpos.lua

[lua]
if string.lower(tostring(game.GetMap())) == “rp_downtown_v2” then

JailPoses = {
Vector(-1901.9425 193.9865 -195.9688),
Vector(-1997.0541 197.7970 -195.9688),
Vector(-2099.8928 196.5997 -195.9688),
Vector(-2199.5940 191.3833 -195.9688)
}

JailSupport = true

elseif string.lower(tostring(game.GetMap())) == “rp_abysscity” then

/*
JailPoses = {
“Enter Vector here”,
“Enter Vector here”,
“Enter Vector here”,
“Enter Vector here”,
“Enter Vector here”,
“Enter Vector here”
}
*/

JailSupport = true

else

JailSupport = false

end
[/lua]

But upon calling the tables or even JailSupport it just returns nil, and trying to run PrintTable errors too. How can I make these tables global so they can be called from anywhere?

As long as you haven’t got

[lua]
local JailPoses
local JailSupport
[/lua]

anywhere, you should be fine, are you definitely including the file?

Well that file is in gamemodes/AbyssRP/gamemode called jailpos.lua and I’m attempting to call the table from gamemodes/AbyssRP/entities/weapons/arrest_stick/shared.lua and addons/Evolve/lua/ev_plugins/sh_jail2.lua

Code extracts:

sh_jail2.lua
[lua]
if JailSupport then
pl.JailNum = math.random(1,table.Count(JailPoses))
pl.JailPos = JailPoses[pl.JailNum]
pl:SetPos( pl.JailPos )
else
pl:SetPos( evolve.jailPos )
end
[/lua]

arrest_stick/shared.lua
[lua]
if JailSupport then
i = math.random(1,table.Count(JailPoses))
trace.Entity:SetPos(JailPoses*)
else
trace.Entity:SetPos(evolve.jailPos)
end
[/lua]

Edit:

And including it where?

Do you have include( “jailpos.lua” ) in your gamemodes init.lua file?

Yup

Code extract:
[lua]

AbyssInt = {}
AddCSLuaFile( “cl_init.lua” )
AddCSLuaFile( “shared.lua” )
AddCSLuaFile( “cl_modules/cl_usermessages.lua” )
include( ‘shared.lua’ )
include( ‘sv_modules/sv_usermessages.lua’ )
include( ‘sv_modules/sv_salary.lua’ )
include( ‘sv_moneystuff.lua’ )
include( ‘jailpos.lua’ )
include( ‘sv_resources.lua’ )
[/lua]

What happens if you remove the map check, so we can rule out that it’s not the problem.

Okay:



] rcon lua_run PrintTable(JailPoses)
> PrintTable ( JailPoses )...
[@lua\includes\util.lua:35] bad argument #1 to 'pairs' (table expected, got nil)
] rcon lua_run print(JailSupport)
> print ( JailSupport )...
nil


jailpos.lua is now
[lua]
JailPoses = {
Vector(-1901.9425 193.9865 -195.9688),
Vector(-1997.0541 197.7970 -195.9688),
Vector(-2099.8928 196.5997 -195.9688),
Vector(-2199.5940 191.3833 -195.9688)
}

JailSupport = true
[/lua]

OH I JUST NOTICED IT.

You need commas in your vectors, eg:

[lua]
Vector(-1901.9425 193.9865 -195.9688)
– should be:
Vector(-1901.9425, 193.9865, -195.9688)
[/lua]

That would have prevented the file from loading and therefore JailPoses and JailSupport would be nil.

Oh! I’ll test that now, thanks!

[editline]25th December 2011[/editline]

Brilliant! It work’s perfectly, thanks!