using callback functions

currently i am playing around with a bit of code and i would like to know how to use a callback function

mainly these

to for example return number players but not all of the rest of it, and possibly only for a single server ip

i have never even herd of “callbacks” and doing a bit of research has only let me to confusion, so any explanation on this matter would be appreciated

A callback is essentially just a function that gets called when another function finishes a task or is called.

Example, concommand.Add( _name, _callback );, hook.Add( _name, _category, _callback );, http.Fetch( _url, _callback );, tmysql.query( _query, _onsuccess, _on_fail_or_flags ); etc…

Here’s another forum post on the matter ( with a bit of information that I haven’t turned into a “tutorial” just yet ): https://dl.dropboxusercontent.com/u/26074909/tutoring/forum_posts/callbacks.lua.html - I hope it helps.

As for your exact question: http://wiki.garrysmod.com/page/Structures/ServerQueryData is telling you that is the table structure ( keys and expected values ) which you use to input here: http://wiki.garrysmod.com/page/serverlist/Query

The ones with default you don’t need to use and you can set the table up however you want… You can use { } in one line, in the function or set it up before. I’ll set it up before with a few commented out lines ( defaults )…


// Since this will query the server-list, we want to stop it after a few because this is just a test...
local _servers_to_query = 10;
local _counter = 0;


local _data = {
	// Default values aren't needed unless you want to change them, but if you're using this in gmod the chances are the user won't want to exit the game to 
	// join a server ( although it could be used to manage multiple game-servers in a community but there are better options than querying all servers )
	-- GameDir	= "garrysmod";
	-- AppID	= 4000;

	// starting non default..
	Type		= "internet";
	

	// This is called for each server found, meaning this'll be called a lot
	// The arguments are listed in order from top to bottom of the list provided on the page and provides descriptions of what each is for...
	Callback = function( _ping, _name, _desc, _map, _players, _maxplayers, _bots, _passworded, _last_played, _ip, _gamemode, _workshopid  )
		print( "Found Server: " .. _name .. " [ " .. _map .. " ] with " .. _players .. " of " .. _maxplayers .. " and " .. _bots .. " bots!" );

		_counter = _counter + 1;
		// If we have reached the number of servers to query, end it...
		if  ( _counter >= _servers_to_query ) then return; false; end
	end;

	// ...
	Finished = function( )
		print( "This is called once the function has finished running" );
	end;
};


Note that this can be used to create a vgui server-list by using the callback inside of a PANEL:Init function to create new vgui elements and add them to the parent-panel. Callbacks are quite useful.