News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

A simple MultiSync Example and explanation

Started by LemonWizard, August 06, 2010, 04:23:02 AM

Previous topic - Next topic

LemonWizard

Alright people's.. well after experimenting with multisync...thanks to the people who posted it..

I've been learning it a little. It's interesting.
I have come up with this!!!

It syncs two players together. The code isn't that elegant.
It does things raw. but it works.

Local host ip is usually 127.0.0.1
it turns out that multisync is useful...heh... and it works. FINALLY SOMETHING THAT WORKS FOR NETWORKED STUFF and it's documented
always a plus...


I just figured that this is going to be good since I can finally start making my projects two player.

Okay, here's what you need to do this with more than one person.

If you get hamachi it's easy. since firewalls seem to love to block playbasic multisync packets.

I don't know why -.- but it's hell...
anyways..

get hamachi here from the official site:: http://download.oldapps.com/Hamachi/HamachiSetup-1.0.1.5-en.exe

Then install it, install the basic not the trial.
Go to the create network if you are the host.

Create your network and set a password then join.

Right here is the most important part. Hamachi will give you a unique IP.
This is the IP your playbasic program needs to connect to.



here is the source for my header.. I just call it that. This is the client:


SETFPS 60
;client

#include "Multisync"

player1x=20
player1y=20
player2x=100
player2y=100





repeat

print "attempting to connect to server"
netconnect("5.32.213.168")



sync
cls rgb(0,0,0)


until netconnected()




The source code example I post here has to be changed, for the client Ip.
To the one you specify.

If your testing on your own computer you can open the client code, and then run the server code right beside it in another window from two seperate PB projects (Just hit the Run button)
and you'll be able to see it take effect right away.
Open the server code first and run it.
Then the client code
The server will wait until player joined before trying to send packets.

For local host, change the ip in the header to 127.0.0.1

And if your the one hosting, just open hamachi and run server.
And change the client code to match your hamachi Ip. You might want to compile these if you have the full version of pb.
All you do is change the ip. and then compile the client.
Make sure that all the include files are sent with the executable because it won't run without them. sorry. XD I think that's pb related.


Anyways..

Here's the client source code and the server source code for mine::


Client::



; PROJECT : Client
; AUTHOR  : owner
; CREATED : 8/5/2010
; EDITED  : 8/6/2010
; ---------------------------------------------------------------------

SETFPS 60
;client

#include "Multisync"

player1x=20
player1y=20
player2x=100
player2y=100





repeat

print "attempting to connect to server"
netconnect("Enter your HAMACHI IP here")



sync
cls rgb(0,0,0)


until netconnected()






main:


circle player1x, player1y, 20, 1
circle player2x, player2y, 20, 1

text player1x, player1y, "Player1"
text player2x, player2y, "Player2"


m=netgetmessage()

if m=true
player1x=netgetint()
player1y=netgetint()
endif

netputint(player2x)
netputint(player2y)
netsend(0)


if upkey() then player2y=player2y-1
if downkey() then player2y=player2y+1
if leftkey() then player2x=player2x-1
if rightkey() then player2x=player2x+1



sync
cls rgb(0,0,0)

goto main














Server:


; PROJECT : Server
; AUTHOR  : owner
; CREATED : 8/5/2010
; EDITED  : 8/6/2010
; ---------------------------------------------------------------------
SETFPS 60
;server

#include "Multisync"

player1x=20
player1y=20
player2x=100
player2y=100



nethost(2)

repeat

print "Connecting with client"

sync
cls rgb(0,0,0)


until netplayerjoined()






main:


circle player1x, player1y, 20, 1
circle player2x, player2y, 20, 1

text player1x, player1y, "Player1"
text player2x, player2y, "Player2"


m=netgetmessage()

if m=true
player2x=netgetint()
player2y=netgetint()
endif

netputint(player1x)
netputint(player1y)
netsend(1)

if upkey() then player1y=player1y-1
if downkey() then player1y=player1y+1
if leftkey() then player1x=player1x-1
if rightkey() then player1x=player1x+1

sync
cls rgb(0,0,0)

goto main







I'll package the whole thing if it's less confusing...



Cheers to anyone who wants to use this ^_^

I just hope that this will help.

If anyone wants help with this library they should learn it with me. maybe even do some tests.
I'm very reachable. I have msn and other things. Just ask. =p

But yeah to note.. this really only seems to work on localhost or through hamachi.

OH YEAH!!

It'll also work through the same router you just have to get the host ip through the cmd
if your using windows..heh. yeah. very tired.