Author Topic: Node with SGDK2  (Read 2807 times)

eric22222

  • Fanatic
  • ***
  • Posts: 174
    • View Profile
    • Eric Online
    • Email
Node with SGDK2
« on: 2015-03-19, 09:26:28 PM »
Hey all, I've been tinkering with networked games through sgdk, and I think what I've stumbled through will help anyone else out who might want to take a stab at it.

Node.js is a fun program that makes it pretty easy to run a server in javascript. This makes it good for plugging in to sgdk's HTML5 projects since they're based on javascript. The tricky thing with networked games is that the server and the client have a communication delay between them, so you can't very well run every frame on the server and send the results back to the client. It's much smoother to have both the client and server run the same game code, and synchronize periodically. I managed to get Node to accept sgdk's generated javascript as a module, but it doesn't like running it. Node runs through the console, which means there's no document or window object to reference. So here's how you can do it!

Set up your node directory like this:

NodeFolder (folder)
- myServerCode.js
- node_modules (folder)
- - sgdk (folder)
- - - index.js
- - - sgdk-lib (folder)
- - - - sgdk.js
- - - - sgdk-extend.js

myServerCode.js is your Node application. Do whatever you want here, and add these lines:

Code: ("myServerCode.js addition") [Select]
var sgdk = require('sgdk');
sgdk.startGame();

index.js is what ties all the files in the sgdk module back to Node:

Code: ("index.js") [Select]
var fs = require('fs');
filedata1 = fs.readFileSync('./node_modules/sgdk/sgdk-lib/sgdk-extend.js','utf8');
eval(filedata1);
filedata2 = fs.readFileSync('./node_modules/sgdk/sgdk-lib/sgdk.js','utf8');
eval(filedata2);
exports.startGame = startGame;
// This tells Node that the function startGame is to be exported and made accessible to the server code

sgdk.js is just the generated javascript file exported from the HTML5 project. I had to replace
Code: ("sgdk.js original") [Select]
mainLoop.interval = setInterval("pulse()", mainLoop.milliseconds);with
Code: ("sgdk.js edit") [Select]
mainLoop.interval = setInterval(pulse, mainLoop.milliseconds);
Finally, add sgdk-extend.js to dummy out the drawing functions in a mocked document/window:

Code: ("sgdk2-extend.js") [Select]
var document = new mocument();
document.elements = new Object();
document.elements['gameView'] = new mockGameView();

function mocument() {}
mocument.prototype.write = function(x) { console.log(x); }
mocument.prototype.getElementById = function(x) { return document.elements[x]; }
mocument.prototype.createElement = function(x) {}
mocument.prototype.querySelector = function(x) { return null; }

var window = new Object();
window.innerHeight = 768;
window.innerWidth = 1024;

function mockGameView() {}
mockGameView.prototype.getContext = function(x) { return new mockCanvas(); }

function mockCanvas() {}
mockCanvas.prototype.fillRect = function(x,y,w,h) {}
mockCanvas.prototype.strokeRect = function(x,y,w,h) {}
mockCanvas.prototype.save = function() {}
mockCanvas.prototype.beginPath = function() {}
mockCanvas.prototype.rect = function(x,y,w,h) {}
mockCanvas.prototype.clip = function() {}
mockCanvas.prototype.restore = function() {}
mockCanvas.prototype.drawImage = function(i,x,y,w,h,x2,y2,cw,ch) {}

And voila, sgdk in a console!


(This is pretty much just a single sprite with initial velocity heading for a dead end, so no player input from the console)

You can add other exported functions so that you can perform game logic based on network communication. For instance, have the player send its position to the server every so often, and respond with the location of all other sprites. Hope this helps!

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2736
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Node with SGDK2
« Reply #1 on: 2015-03-22, 11:29:28 AM »
Cool. Coincidentally, I've just been reading about how Javascript is so prevalent now that Unity 5 implemented the (mindblowing) ability to compile C# to C++, then compile that to Javascript in order to run Unity 5 games in a browser with no plugin.

Do you have any specific plans to develop an SGDK2 networked game or fully functional networked demo using Node.js?

Also, it may be interesting to try using some of the functions used for saving and loading games in communicating game state between client and server.
« Last Edit: 2015-03-22, 11:33:54 AM by bluemonkmn »

eric22222

  • Fanatic
  • ***
  • Posts: 174
    • View Profile
    • Eric Online
    • Email
Re: Node with SGDK2
« Reply #2 on: 2015-03-22, 09:49:22 PM »
Right now I'm pretty much flying by the seat of my pants, but I am working on a networked browser game with SGDK2 and Node.js. I've got a couple of friends helping me test, and the general development cycle is that we try out what I've got every Tuesday, then brainstorm what I should put in next.

I'll see if I can't capture some gifs or something next time we're testing, or maybe I can find a way to host the html file and have a semi-public demo (single-file export is a HUGE help when sharing with friends, so thanks a ton for that feature!).

I did use a bit of the serialization code from the save functions, but I've trimmed it down to pretty much just stringifying part of the sprite list to save on bandwidth. Still figuring out how to do this without making it unplayable for players with high ping.

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2736
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Node with SGDK2
« Reply #3 on: 2015-03-23, 06:52:27 AM »
Is the trimmed down serialization complicating things at all as compared to a full serialization of the sprite list or even the whole map? One tenet of optimization is to not optimize prematurely. Keep in mind that even if you only have 1 Mbps (which most or all people I know exceed these days), that amounts to ~125KB of data that can be sent per second, and that's over a relatively slow connection. Have you evaluated how big a simple serialization of the whole map is? I'm curious what challenges you face specific to the limited serialization that uniquely affects players with high ping.

Which single-file export are you referring to, SGKD2 file or HTML file? I assumed HTML, but then realized you might also be referring to the SGDK2 format.
« Last Edit: 2015-03-23, 07:05:20 AM by bluemonkmn »

eric22222

  • Fanatic
  • ***
  • Posts: 174
    • View Profile
    • Eric Online
    • Email
Re: Node with SGDK2
« Reply #4 on: 2015-03-24, 05:11:44 PM »
I really just poked through the save code and found it was just stringifying things, so I just decided to pack up my stuff in the same way. In addition to sprite data, I also have chat messages and such to pass around. I may end up using the whole save/load set eventually. It's nice and separate from everything else, so it won't be a big change once I do rethink what data needs to be sent. I hadn't checked until just now, but a serialization of my dummy map with 20 sprites running around comes to 25 kb. Granted, I've got about 30 Mbps down, but only about 5 up, which amounts to 25 sends per second. It'll take a lot of trial and error to determine how frequently each client will need to download the game, and I'm still not sure how many players the game should support.

The html file is what I've been passing around. I don't have any hosting set up just yet, so I've just been emailing the html file to my friends. I found out that you can do limited free hosting through Google Drive, but it only serves up the page through https, and I haven't quite figured out self-signed certificates just yet.

emmalopez

  • Visitor
  • *
  • Posts: 1
    • View Profile
Re: Node with SGDK2
« Reply #5 on: 2015-03-30, 03:31:50 AM »
Even I am currently dealing with the HTML files only. For this I use my Google drive.
« Last Edit: 2015-04-03, 12:46:18 AM by emmalopez »

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2736
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Node with SGDK2
« Reply #6 on: 2015-03-30, 05:27:24 AM »
Glad to see SGDK2 is still seeing some activity (perhaps thanks to the HTML support). Hopefully the discrepancy between the desktop function set and the HTML function set isn't causing too much trouble. thanks for joining us and commending, emmalopez. I would be interested to hear more about your project in the Projects section, or another post in this General section!