Game Development Community

Quick Server Joining

by Rafael S. · in Torque Game Engine · 05/20/2008 (6:29 am) · 10 replies

Hey guys,
i got quite a special problem concerning server connections.
my game requires the user to be able to join a server by clicking only ONE button. the set-up will be a network of 2 computers only and the ip-addresses will be static, so i do know what ip to connect to.
i checked out some ressources and implemented a ressource which allows me to query a specific ip-address only.
but since i only get the info sent back to the gui-element (which i don't wanna use) and having it display the server (which i don't want also) it doesn't help much.

maybe i just missed something in the scripts, but this doesn't seem to be a quickie...

my dream-solution would scan for LAN-Servers and connect to the first one it finds with only 1/2 players. unfortunately this doesn't seem to be as easy as i thought it would...

every help would be greatly appreciated!

#1
05/20/2008 (6:39 am)
Use the TorqueScript "Connect(IPAdressOfTypeA.B.C.D:Port)" command.

Nicolas Buquet
www.buquet-net.com/cv/
#2
05/20/2008 (6:51 am)
The dream is also not far off... use what Nicholas said to join the first queried server with only 1/2 players!
#3
05/20/2008 (6:52 am)
Doh, ok, i almost feel like apologizing for the thread.. but thanks for the quick answer nicolas!

non-the-less i'm still looking for a way to get the ip-address of a server found by a query as a string.
this way i could still have it look for a server and then connect to the ip automatically.

i still feel stupid for not having found the connect() function tho..
thanx, this will work as a quick and dirty fix for the moment.

Edit:
Dave: yeah, that was my original plan. but when i was following the trail of the query-functions i eventually landed in the source code and didn't find a way to get the server's ip-address in form of a string. somehow the functions in c++ try to output directly to a gui-element. correct me if that's not true..
#4
05/20/2008 (9:08 am)
Ok, maybe i was exploring in the wrong direction, but i tried to understand the serverQuery.cc code.

my main problem (apart from not knowing c++ at all) was getting the right information out into the script. i found the right place to do that would be at the end of the "processPingsAndQueries(...)" function and tried to call a scripted function with the Con::executef() callback. unfortunately i don't understand how the Vector "gServerList" is managed.
i was able to call the script function but my attempts to pass information (i tried stuff like &gServerList.last() or &gServerList[0] and similar) failed miserably. when trying to access "gServerList" without a "&" (which i don't know what it really stands for) torque just crashed when i called the function.
maybe some c++ guru might wanna help me out.. i would really apreciate it!

and still.. if there is a simpler way to get the queried server list into a string so i could pick the server by script i'd be very, very interrested!
#5
05/20/2008 (9:24 am)
function JoinServerGui::update(%this)
{
   // Copy the servers into the server list.
   JS_queryStatus.setVisible(false);
   JS_serverList.clear();
   %sc = getServerCount();
   for (%i = 0; %i < %sc; %i++) {
      setServerInfo(%i);
      JS_serverList.addRow(%i,
         $ServerInfo::Name TAB
         $ServerInfo::Ping TAB
         $ServerInfo::PlayerCount @ "/" @ $ServerInfo::MaxPlayers TAB
         %i);  // ServerInfo index stored also
   }
   JS_serverList.sort(0);
   JS_serverList.setSelectedRow(0);
   JS_serverList.scrollVisible(0);

   JS_joinServer.setActive(JS_serverList.rowCount() > 0);
}

In this code, setServerInfo is used to have the engine set several constants which are used to populate the GUI. It's a bit unusual compared to the way other design patterns are used, but it's the magic in the function really.
#6
05/20/2008 (9:37 am)
Ok, thanx dave, now at least i understand where it's really triggering the update (haven't noticed that it's calling a hardcoded function there).
now i guess my goal would be to write a getServerInfo(%num) function, which i unfortunately have no idea how to do it.. =)
the other option i guess would be calling the setServerInfo() function for an invisible gui and then just read the ServerInfo::whatever variables...
a bit dirty but i guess that might work..
#7
05/20/2008 (10:01 am)
You probably don't need to write your own, just hook into the information there. Looking into the Join function, you can see:

//----------------------------------------
function JoinServerGui::join(%this)
{
   cancelServerQuery();
   %id = JS_serverList.getSelectedId();

   // The server info index is stored in the row along with the
   // rest of displayed info.
   %index = getField(JS_serverList.getRowTextById(%id),6);
   if (setServerInfo(%index)) {
      %conn = new GameConnection(ServerConnection);
      %conn.setConnectArgs($pref::Player::Name);
      %conn.setJoinPassword($Client::Password);
      %conn.connect($ServerInfo::Address);
   }
}

This again uses the setServerInfo function which uses the engine to set a bunch of constants, and one of the constants it sets is $ServerInfo::address. This is accessible from the update function as well of course, so simply grab the address after the previous setServerInfo and you have all the pieces you need.
#8
05/20/2008 (10:14 am)
Hmm..
well, i was trying to avoid this because i don't wanna bother using the JS_serverList GUI in the first place, but i guess i'll have to live with it.. thanx anyway!

what i don't get is: after starting the query, which will try to write info into the gui (which will be invisible in my case), but also writes the constants, how do i know it has finished querying if i start the server from a seperate function? do i just schedule it and hope to pick the right time?
#9
05/20/2008 (10:21 am)
At any time after the pings finish, you can call setserverinfo(index) to populate the constants with meaningful info. The code is ready and willing for you to scalpel and whittle, reuse, etc. You've got all the pieces now!
#10
05/20/2008 (10:30 am)
Yeah, i guess so.. guess i just have to live with the fact that it's not gonna work the way i wanted to do it. but the result will be the same, so i guess it's just the result that counts =)

thanx for your time and effort dave!