Re: 2 network questions

@post 12 I can walk you through it. It's not hard.
The example in the network chapter has 2 while loops in each void, so I split it into 2 different voids between the while loops.
//hosting with IP
void create()
{
show_game_window("Game Server");
tts_voice voice;
if(host.setup_server(6500, 1, 100)==false)
{
speak("Error. The server could not be set up.");
exit();
}
speak("You are now hosting.");
network_event event;
while(true)
{
if(key_pressed(KEY_ESCAPE))
mainmenu();
event=host.request();
if(event.type==event_connect)
{
p.play_stationary("sounds/message.wav",false);
alert("peer joined.", "Peer number " + event.peer_id + " connected from " + host.get_peer_address(event.peer_id) + ".");
//determine that this is player 1
player=1;
//determines who's turn it is
first__turn=true;
second_turn=false;
//the menu for player 1 after connect success
show_game_window("player1");
player1_menu();
}
}
}
Here is the void for the person who will enter your IP
void join()
{
show_game_window("Game Client");
tts_voice voice;
uint server_id=0;  // This is used to store the ID of the remote peer that will be our server.
if(host.setup_client(1, 1)==false)
{
speak("Error. The client could not be set up.");
exit();
}
//speak("Client started.");
speak("Please enter the IP address or host name that you wish to connect to.");
string address=input_box("Address", "Please enter the host name or address to connect to.");
if(address=="")
{
speak("Exiting.");
exit();
}
speak("Connecting, please wait.");
host.connect(address, 6500);
network_event event;

// In this loop, we just wait for either a connect or disconnect event as this is always the first thing that we'll get.

while(true)
{
if(key_pressed(KEY_ESCAPE))
mainmenu();
event=host.request();
if(event.type==event_connect)
{
p.play_stationary("sounds/message.wav",false);
alert("connected", "The connection to " + host.get_peer_address(event.peer_id) + " succeeded!");
server_id=event.peer_id;
//determine player
player=2;
//determine who's turn it is
first_turn=true;
second_turn=false;
show_game_window("player2");
//the menu for player2 after connection successful
player2_menu();
}
if(event.type==event_disconnect)
{
voice.speak_interrupt_wait("The connection to " + host.get_peer_address(event.peer_id) + " failed. Exiting.");
exit();
}
if(key_down(KEY_LMENU) and key_pressed(KEY_F4))
{
host.destroy();
voice.speak_interrupt_wait("Exiting.");
exit();
}
wait(5);
}
}
So, that's how you set up the connection. Now we can start sending and receiving messages. Make sure you write "network host;" at the top of your script. I will post a few examples here.
//send
void send_player1()
{
while(true)
{
//checks for received messages
receive_player1();
if(key_pressed(KEY_SPACE))
{
alert("sending", "You are sending a message.");
host.send_reliable(0, "send1_player1", 0);
//switch turns
first_turn=false;
second_turn=true;
}
if(key_pressed(KEY_RETURN))
{
p.play_stationary("sounds/send.ogg",false);
alert("sending", "You are sending another message.");
host.send_reliable(0, "send2_player1", 0);
//switch turns
first_turn=false;
second_turn=true;
}
}
}
void send_player2()
{
while(true)
{
//check for received messages
receive_player2();
if(key_pressed(KEY_SPACE))
{
alert("sending", "You are sending a message.");
host.send_reliable(0, "send1_player2", 0);
//switch turn
first_turn=true;
second_turn=false;
}
if(key_pressed(KEY_RETURN))
{
p.play_stationary("sounds/send.ogg",false);
alert("sending", "You are sending another message.");
host.send_reliable(0, "send2_player2", 0);
//switch turn
first_turn=true;
second_turn=false;
}
}
}
//receive
void receive_player1()
{
tts_voice voice;
network_event event;
event=host.request();
if(event.type==event_disconnect)
{
host.destroy();
alert("disconnected", "The server died. Exiting.");
exit();
}
if(event.type==event_receive)
{
wait(50);
if(event.message=="send1_player2")
{
alert("receive", "You received a message.");
//switch turn
first_turn=true;
second_turn=false;
}
if(event.message=="send2_player2")
{
p.play_stationary("sounds/receive.ogg",false);
alert("receive", "You received another message.");
//switch turn
first_turn=true;
second_turn=false;
}
void receive_player2()
{
tts_voice voice;
network_event event;
event=host.request();
if(event.type==event_disconnect)
{
host.destroy();
alert("disconnected", "The server died. Exiting.");
exit();
}
if(event.type==event_receive)
{
wait(50);
if(event.message=="send11_player1")
{
alert("receive", "You received a message.");
//switch turn
first_turn=false;
second_turn=true;
}
if(event.message=="send2_player1")
{
p.play_stationary("sounds/receive.ogg",false);
alert("receive", "You received another message.");
//switch turn
first_turn=false;
second_turn=true;
}
I hope that helps you with your projects.

-- 
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Aprone via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Zarvox via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Aprone via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Zarvox via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : ivan_soto via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Zarvox via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Zarvox via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : rory-games via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Zarvox via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Zarvox via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Zarvox via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Zarvox via Audiogames-reflector

Reply via email to