Jump to content


Photo

Multiplayer (oh Boy)


  • Please log in to reply
9 replies to this topic

#1 Kajio

Kajio

    GMC Member

  • GMC Member
  • 2520 posts

Posted 05 July 2009 - 06:33 AM

Well, I've decided to peek my head into the realm of multiplayer games. I've been starting off real slow and getting some basic tutorials and such, but it seems I've hit a problem already. I'm using 39DLL, as the industry standard, and I've just been trying to get an incredibly simple engine going, where all you do is click on the screen and your little circle jumps to that point. That is sent to the other player who then puts the little object representing you, in the correct place.

The only real code in the game is that of the obj_player. Here, I've commented it to what I think it's supposed to do:
//GAME START EVENT
dllinit(0,1,1)  //Kick off 39DLL

if show_message_ext("Host or join?","Host","Join","")=2  //Let the player pick to host or join a match
{
 hostip=get_string("Host IP","")  //Get the ip of the desired host
 global.otherplayer=tcpconnect(hostip,23033,1)  //Connect to the desired host
 if global.otherplayer=0 show_message("Unable to connect to server") //Bug check
 x=500  obj_otherplayer.x=80  //Switch places so you are at the joining position
 started=-1 //Remain idle
}
else
{
 started=0  //Remain idle, but check for incoming connections
 listen = tcplisten(23033, 2, 1); //Socket for monitering incoming connections
}

//STEP EVENT
if started=0  //If checking for connections
{
 sock = tcpaccept(listen, 1);  //Accept connections
 if(sock) //If there is an accepted connection
 {
  closesocket(listen); //Stop checking for new connections
  global.otherplayer = sock; //Assign the new connection a variable
  started=1 //Let the good times roll
 }
}

if started=1 //If the good times are rolling
{
 if mouse_check_button_pressed(mb_left) //Click
 {
  x=mouse_x //Move to
  y=mouse_y //the mouse
  clearbuffer(); //clean slate
  writebyte(0); //Message type
  writeshort(x); //x position
  writeshort(y); //y position
  sendmessage(global.otherplayer);  //Send to other player
 }

 while(1) //repeat until broken
 {
  size = receivemessage(global.otherplayer); //recieve a message
  if(size < 0)break; //if there was no message, stop
  if(size == 0) //if the message was blank
  {
   show_message("Other player left the game");  //the player disconnected
   game_restart(); //Try again
   break;
  }
  mid = readbyte();  //What type of message was it?
  switch(mid)
  {
   case 0: obj_otherplayer.x = readshort(); obj_otherplayer.y = readshort(); break;  //Move the other player to the right spot
  }
 }
}

So, I start up one copy of the game, and select host. It acts like it should, just staying idle waiting for an incoming connection, so I start up another copy of the game and select join. It asks for the IP, and I just put in my own (which has worked for all the examples/tutorials I've seen) and it waits (with that black screen) like it's trying to connect. It then shows the room like everything's good, but it won't let me click to move the player (host or client side).

It seems like the variable "started" is never changed to 1 (which now that I think of it, there's nothing to set it to 1 client-side... but it still ought to work on the host-side). It should be changed to 1 when the host receives the connection for the client, which it never seems to.

Anyone with any decent experience with 39DLL will probably find this pretty simple. I modeled this off of an example included with the DLL, so it's not exactly revolutionary.

If it'll help, you can get the exact file I have and try it out yourself: Download online.gmk

If you need any more information or whatever, just ask. I'd like to get into some multiplayer functionality, so this'll definitely help :)

Edited by Kajio, 06 July 2009 - 01:29 AM.

  • 0

#2 candc32

candc32

    GMC Member

  • GMC Member
  • 895 posts

Posted 05 July 2009 - 08:11 AM

add this to step of the player:
if started=-1
{
  if (tcpconnected(hostip))
  {
	game_end()
  }
  else
  {
	started=1
  }
}

  • 0

#3 Kajio

Kajio

    GMC Member

  • GMC Member
  • 2520 posts

Posted 05 July 2009 - 08:40 AM

Hey thanks, that seems to solve the problem client side, but the host side still doesn't work... I'll test around some, but if you know right off (or have a good idea) let me know. ;D

EDIT: But hey, doesn't that mean that it's not connected? Cuz unless I'm missing something, the game should end if it is connected... but maybe I am missing something, I'm pretty sleep deprived right now 0.0

Edited by Kajio, 05 July 2009 - 08:45 AM.

  • 0

#4 FoxInABox

FoxInABox

    GMC Member

  • GMC Member
  • 5400 posts
  • Version:GM:Studio

Posted 05 July 2009 - 01:05 PM

I moved the "repeat until broken" part out of the "if started=1" and instead it check for messages "if abs(started)==1"
when the player connect to host .. then host send a message to client to set itself to started=1 .. and it does it to itself aswell

Here you go:
Posted Image
  • 0

#5 Kajio

Kajio

    GMC Member

  • GMC Member
  • 2520 posts

Posted 06 July 2009 - 01:10 AM

Hmm... it still doesn't work for me. I seems like they never connect to each other... Am I missing some step that I need to do? I've set up a specific port for use, and my firewall doesn't block it (I should get a notice)...

I don't get any error messages, they just don't let me move like I should if started==1...

EDIT: And I have a router... that messes with connecting online doesn't it? But should it matter if I'm just connecting to myself? And I'm specifying a port to use...

Edited by Kajio, 06 July 2009 - 01:14 AM.

  • 0

#6 Mnementh

Mnementh

    15151

  • Retired Staff
  • 6261 posts
  • Version:GM:Studio

Posted 06 July 2009 - 01:16 AM

What IP address are you connecting to?
  • 0

#7 Kajio

Kajio

    GMC Member

  • GMC Member
  • 2520 posts

Posted 06 July 2009 - 01:22 AM

My own. The one that shows up when I go to http://www.whatismyip.org/ at least.

Edited by Kajio, 06 July 2009 - 01:26 AM.

  • 0

#8 Mnementh

Mnementh

    15151

  • Retired Staff
  • 6261 posts
  • Version:GM:Studio

Posted 06 July 2009 - 01:24 AM

Most internet providers (that I know of) won't send a message back to its sender. You have to use your internal address, or the loopback address (127.0.0.1).
  • 0

#9 Kajio

Kajio

    GMC Member

  • GMC Member
  • 2520 posts

Posted 06 July 2009 - 01:29 AM

Hey! Look at that! It works like a charm, thanks a lot. That's quite an annoying little mess up, but I'm all the wiser for it.

Thanks for the help everyone, it works now!
  • 0

#10 Mnementh

Mnementh

    15151

  • Retired Staff
  • 6261 posts
  • Version:GM:Studio

Posted 06 July 2009 - 01:31 AM

No problem, for my part. :blink:
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users