simple networking game tutorial ? takers

Monkey Forums/Monkey Beginners/simple networking game tutorial ? takers

navyRod(Posted 2014) [#1]
First of all I would like to say thank you to all the supporters of the Forum.

As a new beginner to monkey my ability to be a provider vice a taker of
knowledge here is obviously limited. I truly hope to reverse that with time.

I understand my abilities and unfortunately am no 'Einstein' or even close to it.
(should have used a reference to some great coder there) . I do enjoy learning
new things and passing on helpful advice that I have learned.

All product to be successful needs to have support and I have seen some awesome
examples in the past ( and still see some now ), but it seems to me that community support to some extent has dwindled off in the last year or so.

There are great examples of that support with:
Learn Monkey with this friendly beginners guide by James Boyd (pdf).
View the Monkey Pixels tutorial by Robbert Prins (pdf).
Follow the Path of the Monkey by Patrick Strandell.
Check out invaderJim's great new YouTube tutorials.

Also module support
Diddy
fantom Engine
and all the others.

And also awesome new third party product support like JungleIde and IginitionX

I am wondering over the past year why hasn't there been new YouTube videos
that have taken Jim's series to the next level?

Please don't get me wrong there have been some and don't have enough time
to list them all but I would like to encourage the community to give itself a shot
in the arm and make this a thriving community.

I wish I had discovered monkey back a year or two ago and hopefully would have
been by now more a giver than taker of knowledge. And so I apologize for
all my annoying I am sure to some beginner questions.

I would hope to see some new tutorials that help new users take this product and run
with it.

One thing I would like to see is a fully explained simple networked game example
that allows for multiple players to play ( say 6 or so) . My thoughts on this is
when you have friends or family over with different devices , Android/Windows/IOS
etc. Example

1. Simple shooter game.
2. Yatzee or similar dice game
3. Trivia game
4. Hang Man
.... the list goes on

Is there anyone out there that would take this on or can a community project
be created ?

Thanks
Rod.


navyRod(Posted 2014) [#2]
What do you think Rod... ?


navyRod(Posted 2014) [#3]
Gee I am pretty inexperienced but give me some time will see what I can figure out


navyRod(Posted 2014) [#4]
Thanks Rod -- will look forward to what you can come up with ..


TeaBoy(Posted 2014) [#5]
I wish I had discovered monkey back a year or two ago and hopefully would have
been by now more a giver than taker of knowledge. And so I apologize for
all my annoying I am sure to some beginner questions.


Keep spreading that you'll give yourself a reputation ;)


erebel55(Posted 2014) [#6]
I'm confused are you talking to yourself in this thread navyRod?
I would also love to see a tutorial on networking :)


navyRod(Posted 2014) [#7]
Yep -- was looking to get some response to the post ... so generated some myself


navyRod(Posted 2014) [#8]
Okay, I am going to give my self a challenge to create initially a very simple network game
Well to at least some of those on here that could probably knock this out in an hour,
but simple to me is a totally different ball game. Thinking it will take me ?

well that's left to be determined :) but I am sure you will not be seeing it tomorrow ( next week ? month ? year ?)

Concept:
1. Simple movable cannon with rotating turret along one edge of the play field
( if my playfield is a square and I still count right then I think I should be able to have up to 4 networked players)
... note to self above -- that is under assumption that I only have 1 player per edge

2. Goal: Shoot the other moveable cannon :)

Ok , -- made it through project design phase, now moving on to implementation ... more to follow.....


Reaper(Posted 2014) [#9]
Sounds like an interesting discussion you got going there Rod ;)
My code as it stands is way too messy to postup, and its a mix of benmc`s network code
& invader jims game mashed together ina way that seems to work ...

Been tidying it up and clearing out stuff , eventually Ill have it as a basic framework thats game with network in and Ill stick that up here
but Im no tutorial writer, will be interested to see what youve come up with too :)


navyRod(Posted 2014) [#10]
Thanks Reaper and erebe55 l :)


consty(Posted 2014) [#11]
Here is an example: C:\MonkeyX77a\bananas\mak\echoserver_tcp
I have not tried to make any network game yet, but this tutorial says it all.


navyRod(Posted 2014) [#12]
const -- -- thanks

I did see these in the banana fold and the echoserver_udp I am sure is the one I want to use for network game.

I run it and all's I get is the hello world print from the create method -- do not see any of the text from the methods making it through
actually getting the socket setup correctly and sending anything .

I do see the code files but I don't see actually any tutorial on how to use this module that goes along with it ?


navyRod(Posted 2014) [#13]
@ Reaper -- thanks -- hope to see what you get out as well.

Are you setting this up for more than two players ?

I am still trying to determine if I have more than 2 in the mix does each one need to connect to the host using a different port or do they all use the same port.


Pharmhaus(Posted 2014) [#14]
I made you a quick mock-up of what it could look like.
It doesn't send/receive data but shows you how to do the connection stuff.



hope that helps!


navyRod(Posted 2014) [#15]
Thanks.. ran that and it worked.

There was a post by ben that has a good beginning example .

It draws a colored circle based on mouse location and I did get this to work when trying between devices (laptop,windows8phone, android tablet, android phone) in different configurations lap to phone .. tablet to phone etc. ( I attach it in code section below)

What I am trying to figure out is what it is going to take to expand this code to allow more connections thinking up to 6 or so as it seems to be coded in this example just as a server and one client . ( so this is a great start )




navyRod(Posted 2014) [#16]
Thanks.. ran that and it worked.

Mark does have an echo server example out that draws a colored circle based on mouse locations as a beginning example.
It draws a colored circle based on mouse location and I did get this to work when trying between devices (laptop,windows8phone, android tablet, android phone) in different configurations lap to phone .. tablet to phone etc. ( I attach it in code section below)

What I am trying to figure out is what it is going to take to expand this code to allow more connections thinking up to 6 or so as it seems to be coded in this example just as a server and one client . ( so this is a great start )




navyRod(Posted 2014) [#17]
Thanks.. ran that and it worked.

Mark does have an echo server example out that draws a colored circle based on mouse locations as a beginning example.
It draws a colored circle based on mouse location and I did get this to work when trying between devices (laptop,windows8phone, android tablet, android phone) in different configurations lap to phone .. tablet to phone etc. ( I attach it in code section below)

What I am trying to figure out is what it is going to take to expand this code to allow more connections thinking up to 6 or so as it seems to be coded in this example just as a server and one client . ( so this is a great start )




Reaper(Posted 2014) [#18]
Heya navy, the code of benmc`s supoported 4 clients for the falling game over tcp no problems, udp would obviously be better tho but not tried yet.
It just needed an extra _socket.AcceptAsync( Self) at the end OnAcceptComplete:Void Method.



As I recall once Id added that it worked for multiple clients.
Here are the 2 OnReceiveComplete methods to show you how it switched state etc.
A bit messy but I was cobling it together and learning :)

1st the Clients receiving code.



And the Servers


Obviously theres a certain amount in the onUpdate() section so Ive included it below as a (bad?) example


As you can see I used serverStr and clientStr to pass data between the 2 and the 9999 was a flag I used to switch to 8888 / 7777 just as a trigger to switch gamestates.

The whole game ended up with a ferw extra states - a STATE_SYNC and a STATE_WAIT where it made sure all clients were ready after the main menu and after the death screen when the Server hit return.


Reaper(Posted 2014) [#19]
erm how do I make those code boxes scrolly..... I just used [ code and [/code ???


Reaper(Posted 2014) [#20]
damn u navy u distracted me, came here to find link for jentos`s Ide, thats pretty sweet, a few bugs atm but I find it very nice so far..... off to dload it and get on with what I meant to hehe


Raph(Posted 2014) [#21]
Use codebox instead of code.


navyRod(Posted 2014) [#22]
Awesome news that this can support 4 or (more?)

Will definitely be looking this over. Your posts are very much appreciated.


Reaper(Posted 2014) [#23]
Tested it with 4, according to the code it should work for 10 , at least thats what the array is declared as being able to allow.


navyRod(Posted 2014) [#24]
Nice -- you going to be posting your game eventually

Plan on dissecting what you got here and initially see if I can incorporate it into ben's example for my understanding

It appears that your using a keyboard routine to enter the IP address and port # that is going to be used vice hard coding it.

Is that correct ?

I am assuming that the clients have to do this as well ?

Getting a few networking examples out here I think would be a nice addition to the community effort so appreciate what you have done.


Reaper(Posted 2014) [#25]
Its is benmcs netcode exactly, just with that 1 extra missing line.

Correct the keyboard routine is lifted from \bananas\mak\keyboardtest and is used for entering in the IP. could do with some tweaks for the purpose ie only entering numbers
and auto inserting dots.... Only the client needs the IP. for now the port is fixed and it is all that the server needs.

Too much to dissect, where to start ;p

As said the netcode is the same, and to incorporate it as you can see I replaced the screen clicks with key presses. Also for android compatibnility I placed 4 buttons on the 4 corners of the screen for host(F1) join(F2) start(return) and quit(esc) F12 was just an experiment and forgot to remove it. Also as you can see in the various different case STATE sections of OnUpdate() it is essentialy just the 1player stuff with added in server checks, where its checking all players are dead during the game, or checking that all the players are ready (connected and having acknowledged the connection by returning relevant info.)

most of the code gets placed in the 2 onreceive methods which is essentialy the same, I just changed how the data was stored when it was split to allow for any amount of info being sent ie this bit -

The basic process from start to finish is the host presses f1 to initiate a connection, anyone can then join if they have the correct IP. only the server needs to have Dmz or port forwarding enabled on their router. Any client can now press F2 and connect. The server during this time is broadcasting the number of players total and the onclientno so when the next client connects it sets its local clientNo variable to onClient , (I also added clientNo as a field of the player class.)

Once the server hits return it sets the gamestate to STATE_SYNC, Seed() = Millisecs and seedForThisGame to Seed(). It then does the standard Reset() and sets serverStr to
readycount(1 initialy ie the server) + 8888 (just a random value I did as a marker/test. should be replaced with a single digit idealy.) + numberofplayers(total number of clients including host) once the client has received this it acknowledges by setting it6s clientStr to its clientno+the SeedForThisGame value.

As soon as the server sees that the seed value broadcast by the client matches its own it sets clientsready[broadcast client no] to be 1 . once all clients are ready the gamestate switches to STATE_GAME. - Note the server then continues to broadcast this info durting the game (seed+8888+readycount) because otherwise the clients dont have a chance to receive the updated readycount value and launch themselves.

Once its in the STATE_GAME section its basicly the same but a little more info is passed back and forth. basicly the clients send their clientNo, position, score, isDead and flip.
The server receives this info and puts each players info into its playerlist and then broadcasts the position of all 4 players(or total no playing) the client receives info and like the server puts the info in its playerlist for the other clients.

Collision death etc are all handled clientside, the server just waits until all players isDead flag is true before changing gamestate, again with a simple digit change, this time to 7777.
It continues broadcasting readycount and numplayers and attaches each players score. the clients likewise send their score once the server hits return / esc the broadcast message is changed to 8888 or 9999 depending if its going back to the main menu or to the game then it goes to STATE_WAIT which is essentialy the same as STATE_SYNC, and from there once all have acknowledged it then goes back to STATE_MENU / STATE_GAME.

Hope this helps and yeah Ill post fullcode of the network framework, stripped outof the game but still with a complete gameloop as a simpler example once Ive tidied it up alot and Im happy with it, this lot is very much a work in progress.

Also as of yet Ive not looked at doing it with UDP but might have to switch to that as with TCPIP you get a certain amount of lag in info being transferred, noticable by how the network players move too many pixels and a bit jerkily.


Reaper(Posted 2014) [#26]
wow thats a wall of text, hate explaining shit ;p hope it helps tho....


navyRod(Posted 2014) [#27]
Nice explanation. I can see from this that network code is not exactly easy and appreciate what you have done .

and ben for having the base to start with ..