WIP: Week 6 -- Node.js, Scripting, Networking

Published January 25, 2015
Advertisement
Hey all!

As a continuation of my last post, I decided to move onto creating some debugging/feature tools, as well as implementing a scripting language. This was basically a successful 2-for-1 special!! For me, networking and general client/server/browser work would be much simpler to do in Javascript, and should be performant enough. Though I have littler experience with Javascript, the experience I've had and the research I've done seems like it'll be a good choice. I also had chosen the language as my scripting language, since it's simple to learn and easily applicable to a variety of platforms and peoples. I've also been looking for excuses to use/learn the language tongue.png

This past week, I downloaded Node.js, which is backed by Google's V8 Javascript Engine. The code is definitely a bit convoluted, but seems very cautious and safe, and makes sense none the less! I delved right in, and got some simple examples running:

  1. Simple Chat Program, all Javascript. Uses browser for the clients.
  2. C++ to Javascript code, where I could use C++ objects and call C++ methods from my Javascript code. Easy enough!
  3. Combined the two above: Chat program, where messages got printed to the console and appended with the sender's information on the C++ side.

The code looked like the following:[code=:0]// C++// Point example.//...static v8::Persistent constructor;// Everything's static except the internal field of the class instance.static void init(v8::Handle exports) { v8::Local tpl = v8::FunctionTemplate::New(New); tpl->SetClassName(v8::String::NewSymbol("Point")); // This internal field is a pointer to the class instance. tpl->InstanceTemplate()->SetInternalFieldCount(1); // These methods need work, handling different method parameters is HARD. // These methods add to the "New" method a function pointer to a callback. // These method callbacks grab a "this" argument, the parameter arguments, // then grab the underlying object and call the same method, with the given // parameter arguments. I've hard-coded the arguments for now, but I need to // template-ize these methods and get it all working generically. addMethod(tpl, "getX", getX); addMethod(tpl, "getY", getY); addMethod(tpl, "setX", setX); addMethod(tpl, "setY", setY); constructor = v8::Persistent::New(tpl->getFunction()); exports->Set(v8::String::NewSymbol("Point"), constructor);} //...void InitAll(v8::Handle exports) { JSPoint::init(exports);}NODE_MODULE(addon, InitAll);// End C++ // Javascript var addon = require('./build/Release/addon.node'); var p1 = new addon.Point(129576, 165.567); var p2 = new addon.Point(124.9, 235.1423); console.log("P1: x=" + p1.getX() + ",y=" + p1.getY()); console.log("P2: x=" + p2.getX() + ",y=" + p2.getY());console.log(p1 instanceof addon.Point);// End Javascript
These examples were brief and simple, but they got the main parts I needed to figure out down pat. From here, the C++ objects and methods lay in the engine, and Javascript runs the game and networking logic. The last issue I need to solve is creating an extensible wrapper for classes that I can wrap for the Javascript code -- surprisingly more difficult than one might think sad.png I hope I can get there in the next week or so!

Best,
ST
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement