I'm making a small networked game, still learning networking so I might be missing out on something.
I am using Visual Studio Community 2017.
I am using Allegro 5 for inputs and graphics.
I am using SDL_Net for networking.
- The game is supposed to launch, works fine.
- Every game tick, function
HandleEvents(...)
gets called in my code.
- The game connects to the server if a mouse click was registered between 0-10 px on the screen, both x and y.
that worked fine, except that the client connected multiple times, so I set a condition so that it will only try and connect 1 client. I did this using a condition:
1 2 3 4
|
if(connected) {
std::cout << "Already connected.." << std::endl;
} else
...
|
Here's the actual code, so you can see what I have in the function.
function in MenuState.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
void MenuState::HandleEvents(Input* input, ClientSocket* cs) {
if (input->mouseX >= 0 && input->mouseX <= 10 && input->mouseY >= 0 && input->mouseY <= 10) {
if (input->mouseB == MouseLDown) {
input->set_mouse_state(MouseNDown);
if (connected == true) {
std::cout << "You're already connected." << std::endl;
}
else if(connected == false) {
switch (cs->connectToServer()) {
case Packets::SERVER_NOT_RESOLVED:
break;
case Packets::SERVER_RESOLVED:
break;
case Packets::SERVER_DOWN:
break;
case Packets::SERVER_NOT_FULL:
{
connected = true;
std::cout << "Connected to server" << std::endl;
receivedMessage = "";
// Check if we've received a message
receivedMessage = cs->checkForIncomingMessages();
// If so then...
if (receivedMessage != "")
{
// Display the message and then blank it...
cs->displayMessage(receivedMessage);
// ...and then re-display the prompt along with any typed-but-not-yet-sent input
//cs->displayPrompt();
}
// Get and deal with input from the user in a non-blocking manner
cs->getUserInput();
}
break;
case Packets::SERVER_FULL:
break;
case Packets::SERVER_NO_RESPONSE:
break;
}
}
}
}
}
|
As you can see, there is clearly a condition check. The condition check works if I remove all networked code in
case Packets::SERVER_NOT_FULL: