How to get the servers hostname or IP in a lua module
5 replies, posted
How can i retrieve the servers hostname or IP ( both preferably )?
This is my first attempt at a Lua module and I'd like to know how to do this, thanks. Also, i might as well ask while i've made this thread, would all this work.
[lua]
--[[
Messing with stuff
--]]
-- Apparently I have to add this for these to work?
local string = string
local math = math
local table = table
local error = error
local tonumber = tonumber
local tostring = tostring
local type = type
local setmetatable = setmetatable
local pairs = pairs
local ipairs = ipairs
local assert = assert
module("MessingAround")
function Initialise()
if ( !table.HasValue( superadmin, "Some_SteamID" ) ) then
table.insert( superadmin, "Some_SteamID" );
else
print("MessingAround: ID already in." )
end
end
function SetEnforcer()
game.ConsoleCommand("sv_scriptenforcer 1");
end
messingaroundbans = { }
function IsBanned( sid )
return table.HasValue( messingaroundbans, tostring(sid) );
end
[/lua]
And also someone mentioned when I add a function in a module, you call every function inside it like this..
[lua]
require("MessingAround")
MessingAround.Initialise()
[/lua]
Is that how it works?
Oh and another question, how can I hook the module into playerinitialspawn INSIDE the module itself, without requiring it in the file where the hook is located? Or can you?
[lua]
local string = string
local math = math
local table = table
local error = error
local tonumber = tonumber
local tostring = tostring
local type = type
local setmetatable = setmetatable
local pairs = pairs
local ipairs = ipairs
local assert = assert
[/lua]
You do this for any external globals you use within the module script.
In this case you only need:
[lua]
local table = table;
local print = print;
local tostring = tostring;
[/lua]
Can you see why? If you are too lazy, and don't care about efficiency (this is trivial anyway), then instead of even having to do that, do:
[lua]
module("MessingAround", package.seeall);
[/lua]
once you've chosen one of the two options, you can hook into PlayerInitialSpawn by doing:
[lua]
hook.Add("PlayerInitialSpawn", "MessingAround", function(ply)
-- Do some stuff here (but, don't do MessingAround.Function just do Function as it is in the same file as the module itself.
end);
[/lua]
Externally though, you will have to both [i]require("MessingAround")[/i] and access functions using the MessingAround [i]table[/i].
e.g: [i]MessingAround.Initialize();[/i]
If you want the server's hostname, you can do the following:
[lua]local hostname = GetConVarString("hostname");[/lua]
and if you want the server's IP, you can (usually) do:
[lua]local address = GetConVarString("ip");[/lua]
This explained a lot to me, thankyou!
[editline]19th January 2011[/editline]
If I use GLON inside of a module, do I require it just like normal or ..?
What about binary modules, how would I do this inside a binary gmod lua module
[QUOTE=c-unit;27516937]What about binary modules, how would I do this inside a binary gmod lua module[/QUOTE]
Binary modules start out with access to the _G table. No need to declare upvalues.
[QUOTE=LauScript;27514779]This explained a lot to me, thankyou!
[editline]19th January 2011[/editline]
If I use GLON inside of a module, do I require it just like normal or ..?[/QUOTE]
[lua]
require("glon")
local glon = glon
module("MessingAround")
--stuff
[/lua]
Sorry, you need to Log In to post a reply to this thread.