multiplayer -- what?

BlitzMax Forums/BlitzMax Programming/multiplayer -- what?

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

i've read every topic on gnet that the search brought up, and every thing in the code archives.

alot of the examples and code snippets wouldn't work, probably because of version differences, and i don't know where to start to correct them.

i've written a VERY CRUDE test project (just to test things out and try and get an understanding of using gnet), thats gone under many revisions based off the code examples that did work.

my final code is loosely based on the code that WendellM wrote.

it seems to connect, but doesn't send and/or receive any information.
i've spent the last 3 days constantly going over all the gnet posts and examples and recoding, but for the life of me can't figure out what to do or how to do it correctly.

in this test program each player is player 1 on their own computer and the other connected player is player 2.
i need to:
1. know when player 2 connects and add 1 to the numplayers var on both computers.

2. be able to send p1x,p1y coords to be recieved by the other player and put them into p2x,p2y.

as always any help would be greatly appreciated.


Import BRL.GNet
AppTitle="multitest"
Graphics 640,480
SeedRnd MilliSecs()

'gfx
Global character = LoadImage("character.png")

'var
Global p1x=Rand(0,575),p1y=Rand(0,416)
Global p2x,p2y
Global oldx,oldy
Global numplayers = 1
Global multiOn = 0
Global myStatus$ = "not connected"
Global connected = 0
Global host:TGNetHost = CreateGNetHost()
Global port = 12345
Global ip$ = "127.0.0.1"
Global timeout_ms = 10000 
Global hosting

Global localObj:TGNetObject = CreateGNetObject:TGNetObject( host )
Global remoteObj:TGNetObject
Global objList:TList = New TList 

Const SLOT_CONNECT = 0
Const SLOT_X = 1
Const SLOT_Y = 2

While Not AppTerminate()
Cls
	If multiOn = 0 Or multiOn = 2
		If numplayers = 2
		    'player2
		    SetColor 255,0,0
		    DrawImage character,p2x,p2y
		EndIf
		
		'player1
		SetColor 255,255,255
		DrawImage character,p1x,p1y		
	EndIf	
		
	Keys()		
	
	If connected = 1
		SendInfo()	
		GNetSync host
		ReceiveInfo()
	EndIf	
			
	If multiOn = 1
		DrawText "[H]ost or [Join]",200,200		
	EndIf
	
SetColor 255,255,255

'debug info
DrawText "p1x: "+p1x,10,50
DrawText "p1y: "+p1y,10,60
DrawText "oldx: "+oldx,10,70
DrawText "oldy: "+oldy,10,80
DrawText "p2x: "+p2x,10,100
DrawText "p2y: "+p2y,10,110

'connection status
DrawText "Players: "+numplayers,10,10
DrawText "Status: "+myStatus$,10,450
Flip
Wend
End

Function ConnectTo()
	CloseGNetHost(host)
	host:TGNetHost=CreateGNetHost()
	If hosting = 1
		success = GNetListen( host, port )
		If Not success
			connected = 0
			myStatus$ = "GNetListen failed"			
			multiOn = 0
		Else
			connected = 1			
			myStatus$ = "connected -- HOST"
			SetGNetInt(localObj, SLOT_CONNECT,1)
			SendInfo()
			multiOn = 2			
		EndIf
	Else
		success = GNetConnect( host, ip$, port, 10000 )
		If Not success
			connected = 0
			myStatus$ = "GNetConnect failed"
			multiOn = 0
		Else
			connected = 1
			myStatus$ = "connected -- CLIENT"
			SetGNetInt(localObj, SLOT_CONNECT,1)
			SendInfo()
			multiOn = 2
		EndIf
	EndIf		
EndFunction

Function Keys()
	If multiOn = 0  Or multiOn = 2
		If KeyDown(key_Left)
			p1x:-10
			If p1x <0 Then p1x = 0
		EndIf
		If KeyDown(key_Right)
			p1x:+10
			If p1x > 575 Then p1x = 575
		EndIf		
		If KeyDown(key_Up)
			p1y:-10
			If p1y <0 Then p1y = 0
		EndIf
		If KeyDown(key_down)
			p1y:+10
			If p1y > 416 Then p1y = 416
		EndIf
		
		If multiOn = 0
			If KeyDown(key_M)
				multiOn = 1
			EndIf
		EndIf
	EndIf
	
	If multiOn = 1
		If KeyDown(key_H)
			hosting = 1
			ConnectTo()
		EndIf
		If KeyDown(key_J)
			hosting = 2
			EnterIP()
		EndIf
	EndIf
	
	If KeyDown(key_Escape) Then End
EndFunction

Function EnterIP()
Local done = 0

	Repeat
	Cls
		DrawText"Enter IP:> "+ip$+"*",200,200
		
		c=GetChar()
		If c
			If c = 8
				If Len(ip$)>0 Then ip$=Left$(ip$,Len(ip$)-1)
			EndIf
			Select Chr(c)
				Case 1
					ip$ = ip$ +Chr(c)
				Case 2
					ip$ = ip$ +Chr(c)
				Case 3
					ip$ = ip$ +Chr(c)
				Case 4
					ip$ = ip$ +Chr(c)
				Case 5
					ip$ = ip$ +Chr(c)
				Case 6
					ip$ = ip$ +Chr(c)
				Case 7
					ip$ = ip$ +Chr(c)
				Case 8
					ip$ = ip$ +Chr(c)
				Case 9
					ip$ = ip$ +Chr(c)
				Case 0
					ip$ = ip$ +Chr(c)
				Case "."
					ip$ = ip$ +Chr(c)
			EndSelect
		EndIf
		
		If KeyHit(key_Enter)			
			done = 1
			Cls
			DrawText"attempting to connect to ip: "+ip$,200,200
		EndIf
	Flip
	Until done = 1
	
	ConnectTo()
EndFunction

Function SendInfo()
	If p1x <> oldx
		oldx = p1x
		SetGNetInt localObj,SLOT_X,p1x
	EndIf
	If p1y <> oldy
		oldy = p1y
		SetGNetInt localObj,SLOT_Y,p1y
	EndIf	
EndFunction

Function ReceiveInfo()
	objList = GNetObjects( host, GNET_MODIFIED )
	For remoteObj = EachIn objList
		Local c = GetGNetInt(remoteObj,SLOT_CONNECT)
		If c = 1 Then numplayers:+1
		
		p2x = GetGNetInt(remoteObj, SLOT_X)
		p2y = GetGNetInt(remoteObj, SLOT_Y)
	Next	
EndFunction