Blabber

Imagine yourself in a late night coding session, eyes pulsing – your hands tingling and your heart pumping away to the sound of the keystrokes. You are on the last stretch here, this is the last puzzle piece. This is what all those hours were for. Not even a whole minute passes, what felt like years all converging into a single experience right underneath your finger tips… As you hit F5 your console comes alive with output, the game starts up and you are presented with a blank screen and some debug spew in the standard output window. What feels like a mixture of adrenaline and, probably caffeine induced shaking you realise just exactly what it is you have achieved – almost nothing. There is no game play, no sounds, not even a single sprite to greet you with a silent nod of the head. All you have done is wired something into your creation, something exciting, something with huge potential but all you have done – is something you never have before. You have stepped in a direction that is new to you.  

If this is so little, then why do I feel like I just launched myself one thousand miles into the sky and I am now ( probably ) invincible? Could it be that something as simple as a change of tools has fired up the passion and fun I had while programming when I started? What makes programming so addictive ( yea I said it! ) for hungry minds like ours? I think I like new challenges. New grounds.

Change is as good as a holiday?

I have no idea. What I do know, is that some times in my programming cycle I find myself doing things with the same set of tools over and over. Maybe it is work, maybe it is a personal project but damned if I don’t get some fresh air. I need something invigorating again, I need that rush to kick me in the shin and remind me exactly what it is that drives me to learn more. Whatever my fancy, whatever I do – get some fresh air. Do something else. Something small and silly. Just because I never have. Break grounds, for myself. Uncover ground I never knew about. DIG AROUND. There is so much fun to be had, even if it leads to pointless, useless and horribly written mess – I will still feel like a million bucks – and whatever it is I come back to in the morning always feels a little more fun.

Irregular broadcasting

In 2009, I decided to do just that. I moved away from the things I knew well, things I had worked on, left the comfort zone for something new.

Is this an evangelism post for v8 vs lua vs whateverScript? No. Is this a conversion pitch to turn over to my favorite technology? No. 

This is a post I would have loved to stumble upon in 2009. This is the spearhead of a long and sometimes frustrating journey. What happened was the story above, re kindling of my favorite parts of learning something new – but it only happened in the beginning of 2010. This is a post that I hope someone somewhere, curious about v8 (in search of something new to learn) can find their way into the story above a lot sooner than I did.

I chose javascript intentionally

Is v8 faster than Lua meant nothing to my goals, I was sick of Lua. I had used it before, used AngelScript before, I had tried and tested and used them all in my games and in my code bases. I wanted something I had never done before! Something daunting for my curious mind. I chose v8 because I liked javascript, and I figured that it would be more accessible to my friends as many of them were web developers ( who isn’t right :p ), most developers somewhere in their programming career have faced javascript in a browser, and probably knew enough to not turn away at the sound of programming a game with me.

What else made me like it? 

While browser wars are an amusing internet culture (among many other fan boy groups), the benefits are obvious. Everyone wants to have the fastest VM, the fastest JIT compiler and the fastest user experience. For me, I like that. I like seeing a repository bursting with changes daily, most if not all of those changes aimed at performance. I like seeing websites and domains with graphs plotting DAILY performance hits on changes to the code of the different engines ( see www.tinyclouds.org) .

Node.js has spawned a sudden urgency in terms of documentation, usage examples, and easy to browse source code of the v8 coding practices. A lot of new developments are now using the v8 javascript engine. Modules, managers, plugins, extensions, you name it – there are now a thousand times more developers using the engine than before (don’t quote me on that, I am just expressing my appreciation ).

The actual v8 stuff in this post starts here

Since the v8 scene has started to liven up, the mailing list is often filled with the same beginner questions I had but there are now many knowledgeable users ready to help those who haven’t grasped it all yet. Here is my contribution to the community, a few tutorials with code, examples and implementations of the most commonly asked and misunderstood aspects when embedding v8 into a game or application.

The usual disclaimer 

I am no professional v8 developer, I am no c++ ninja, but what I can offer I will do my best. Please, if there are things you can suggest for performance, usage or clarity it would be great if you could post it in the comments or send me an email so it can be included for the other readers of the tutorials :)

Diving right in

I am not going to cover installing, compiling or configuring (there is plenty of that around – and you should learn the tools you want to use). What these tutorials will cover is split up over 4 simpler parts , each expanding on the previous one, aiming to cover the very first steps of throwing v8 into your application. The examples are written using vs2008 express, and if desired it would be trivial to make Mac or Linux project files too. The binaries included are for windows, but the code is heavily commented and hopefully clear and straight forward.

Note : Each heading is a link to the source of the example on github.

Tutorial 01 – A Command Line javascript environment. 

This one is the raw , simple basics. Load a file, execute the string, return the results. 

Tutorial 02 – Exposing global functions into the scripts

Standing on tutorial 01, this example shows how to make a global function accessible in scripts.

Tutorial 03 – Exposing objects into the context

Pushing predefined c++ objects into javascript is almost as important as being able to execute the code itself. 

Tutorial 04 – Exposing types from c++ into a javascript context

Something everybody seems to ask at some point. How do you expose native c++ classes in a chunky fashion.  

 

Conclusion

I hope this blabber is rewarded by the examples if anything, and that you don’t feel robbed D:

I am posting to learn, don’t hold back on code, performance, implementation details and general feedback.

 

Download the examples and source 

v8-tutorials on github