Hello,
I'm trying to implement a pretty simple concept: when the game starts it calls JoinNetwork() then spends two seconds looking for a host; if it doesn't find one it then declares itself the host with HostNetwork(). The problem I'm having is that this ends up with two clients on the network - so I suppose what is happening is the first attempt to join actually succeeds when that local host is created. Looking at the example code for JoinNetwork
https://www.appgamekit.com/documentation/Reference/Multiplayer/JoinNetwork.htm, I figured what I was missing was a CloseNetwork() command, which that code calls after a timeout. But trying this locked up my code. Wondering if I was doing something else wrong, I tried the example code, but it locks up too.
The snippet from my tier 2 code:
// find a host or become one.
_networkId = agk::JoinNetwork("tanks", "client");
float t = agk::Timer();
bool connected = false;
do
{
agk::PrintC("Searching for host...");
connected = (agk::GetNetworkNumClients(_networkId) > 0);
switch (connected)
{
case true:
agk::Print("found.");
// found a host, so we're a client.
_netState = iAmClient;
_theServerId = agk::GetNetworkServerID(_networkId);
break;
case false:
// no host found, but give it two seconds...
if ((agk::Timer() - t > 2) && !connected)
{
agk::Print("not found, so hosting.");
agk::CloseNetwork(_networkId);
agk::Sync();
// ...and then become the host.
_networkId = agk::HostNetwork("tanks", "host", 1041);
_netState = iAmHost;
connected = true;
}
break;
}
agk::Sync();
} while (!connected);
// now we're connected, get our client ID.
_myClientId = agk::GetNetworkMyClientID(_networkId);
So what to do? This seems like an easy way to emulate something closer to peer-to-peer. Any help much appreciated!