Sign in to follow this  
studentTeacher

Javascript/C++ Combo Questions (Networking/Tools)

Recommended Posts

studentTeacher    1077

Hello,

 

Currently, I've been looking forward to making a wide range of related tools for my game engine (written in C++, scripted in Javascript). I've even looked at going to the point of having a browser-based game, but for now I want to keep the engine working as-is. Recently the tools I'm looking to develop require HTTP requests from a browser to be handled by a webserver embedded into the engine, allowing for developers to do a variety of things: snoop, change variables, check game flow, etc. I'm already planning on using Javascript as a scripting language, especially with SpiderMonkey's IonMonkey doing JIT compiling and such.

 

I've had a hard time getting myself getting this webserver working in C++ -- and frankly, JavaScript seems to have so many better solutions to getting a lightweight webserver to work (e.g. Node.js and Socket.io) that I find myself wondering if I should just implement this portion in javascript. I can combine this "scripted" portion (regardless of how important this tool is, and how embedded it will be with the engine's data and core, the engine is still C++ in its overall core I guess) with the engine and some browser tools. Before I take this step, I wanted to get some advice, or see if I'm missing something in the bigger picture of things. 

 

Does this sound like a good idea? Am I missing something, or totally off course? I'm looking for suggestions, questions, and corrections. Please help me out!

 

P.S. I'm not sure if this is the right forum to place it in; I was between different forums and decided to place it here for general-purposes sake....

 

Best,

ST

Edited by studentTeacher

Share this post


Link to post
Share on other sites
SmkViper    5396
Off the top of my head - if javascript gets you the results you want faster and you can afford the speed loss that comes from an interpreted language and the correctness loss that comes from a non-typed language, then hey, go for it.

You can always switch back to C++ if you find that javascript isn't going to fit your needs or is too slow or whatever.

That being said, it would probably be useful for you to at least figure out why you can't get the webserver working in C++. Even if you don't end up using it, the debugging experience and learning process will be useful to you.

Share this post


Link to post
Share on other sites
studentTeacher    1077


That being said, it would probably be useful for you to at least figure out why you can't get the webserver working in C++. Even if you don't end up using it, the debugging experience and learning process will be useful to you.

 

I think it's more so that I can get it working in C++, but the time and effort I'd put into that, for something that I find more verbose and difficult to use anyway, isn't worth it. Using Javascript (which I've done for a chat program that uses the browser as the client) was simple, barely a few lines of code, and was concise and easy to read and understand. I just don't see why I should take the time to finish figuring out the C++ version when it won't be as nice or as clean as the Javascript version.

 

Now with  that  being said, I just wanted to check my sanity at this point. Do scripting languages get used in this way (as I will use this as a scripting language later anyways)? Or is this bad practice that I change later? The javascript portion is going to end up with access to all Game Objects and assets and will be able to modify/change/snoop on them all in the browser....

 

As I see it, I will use Javascript to make my game. When it's not performant enough, I can fall back on C++ to do the job for me. So my thoughts here are that Javascript is used for anything it can be used for, and C++ is ONLY used when necessary, like pathfinding or AI behaviors, or the RenderingEngine portion of my engine. Previously I thought I'd use C++ for the Engine, and Javascript for the game. The issue I'm having is networking, webserver stuff, and all that jazz is SO much easier in Javascript for me that parts of the engine are moving over to Javascript -- and in theory, I'm unsure if this is a safe move or not.

 

In the end I get that it boils down to me and what I want, and what I need to meet my expectations, I just wanted to see where I'm at and where theory and general practice usually leans :)

Share this post


Link to post
Share on other sites
TheChubu    9454


Or is this bad practice that I change later?
If you do it in JS and then change it to C++, it isn't "bad practice", that just means requirements weren't what you thought they'd be. Its fine, it happens, and the next time you have to implement something related, you'll know the right answer.

Share this post


Link to post
Share on other sites
studentTeacher    1077

I guess my last question would be this then: Does anyone know of any good C++ alternatives to things like Node.js that would be easy enough to embed in my game engine and still just as functional as Node.js? Maybe I've been looking up the wrong side of the tree or something, so i think it's best to ask.

Share this post


Link to post
Share on other sites
SmkViper    5396

As I see it, I will use Javascript to make my game. When it's not performant enough, I can fall back on C++ to do the job for me. So my thoughts here are that Javascript is used for anything it can be used for, and C++ is ONLY used when necessary, like pathfinding or AI behaviors, or the RenderingEngine portion of my engine. Previously I thought I'd use C++ for the Engine, and Javascript for the game. The issue I'm having is networking, webserver stuff, and all that jazz is SO much easier in Javascript for me that parts of the engine are moving over to Javascript -- and in theory, I'm unsure if this is a safe move or not.
 
In the end I get that it boils down to me and what I want, and what I need to meet my expectations, I just wanted to see where I'm at and where theory and general practice usually leans smile.png


Well, if it's fast enough in javascript, then leave it in javascript. smile.png

Scripting languages, in general, are used where fast iteration is preferred over processing speed. This usually means that a lot of core functionality is written in C++, with the scripting language being used to wire things together or provide customization.

Example:

I could make a trap in C++ that fires a shotgun at the player when they step on a pressure pad. However, if I want the pressure pad to turn on a light - or a trap that only fires on enemies, I have to make a new trap in C++, and that has compile time costs.

On the other hand, I could make a pressure pad in C++ that fires off some sort of "triggered" event to a scripting system. Then the script can fire a shotgun, or turn on a light, or whatever without touching a line of C++ code. In this example, the thing that needs to be fast (collision detection) is in C++, but the thing that can be slow (what do I do when the player hits me?) is done in script so I can more easily swap it out or change it later.

Share this post


Link to post
Share on other sites
Krohm    5031


Recently the tools I'm looking to develop require HTTP requests from a browser to be handled by a webserver embedded into the engine ... I've had a hard time getting myself getting this webserver working in C++ -- and frankly, JavaScript seems to have so many better solutions to getting a lightweight webserver to work... Am I missing something, or totally off course?
Yes. Having a webserver in your game to serve XMLHTTPRequests?

No way. Look for websockets.

 

The feature you're trying to implement is extremely advanced and comes sure at a toll in terms of effort. Be sure to get a positive gain. I wouldn't do anything like this at engine level, maybe for the scripting system. Maybe.

Share this post


Link to post
Share on other sites
studentTeacher    1077


Yes. Having a webserver in your game to serve XMLHTTPRequests?
No way. Look for websockets.
 
The feature you're trying to implement is extremely advanced and comes sure at a toll in terms of effort. Be sure to get a positive gain. I wouldn't do anything like this at engine level, maybe for the scripting system. Maybe.

 

I've been looking at websockets -- though that was a keyword that escaped me for a while. What I've decided to do is run my engine as a shared library alongside the V8 engine (embedding C++ functionality from my engine into my javascript code), and use Node.js w/Express and Socket.io for my webserver. This ends up being powerful and simple to do, while also letting me get my scripting language out of it, which is what I wanted. I can already pass data form the engine to an HTML page! It only took about a couple days to implement it :)

 

Now to add structure to the engine (C++), and pretty up the browser portion, and I'm done. Then I just need to create requests and responses as I come up with them. Luckily all requests can be handled in C++, so that makes my life a lot easier to pack into the engine. I'm thinking in a couple weeks I can have functionality done, and I can work on form and style over time.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this