I’ve been messing around with it for a few days, and have got to a level where I know what I’m doing, so here’s what’s what.
Binding
I use binding to describe binding c functions to the scripting world. The binding is similar to basic Lua. You define proxy c functions, then you assign them as named globals in the scripting enviroment. Lua has a few helper scripts to ease in binding, which is great if you’re in a rush, but usually ends up in a mess of undebuggable bloat. I always preferred to just play it straight, albeit with a few macros. Here’s some example code, bear in mind that I’ve made a few wrapper classes, so this isn’t straight v8, anything with v8plus in the name is mine.
static v8::Persistent<v8::FunctionTemplate> World_ft;
V8_PLUS_LIBRARY_START( World )
static v8PlusFunction( CreateEntity )
{
v8This( CWorld );
V8_ARG_COUNT( 1 );
V8_ARG_STRING( vStrName, 0 );
IEntity* ent = self->CreateEntity( vStrName.c_str() );
if (!ent)
{
return v8::Undefined();
}
return ent->GetScriptObject();
}
void Construct( v8TemplateHandle global_template )
{
v8::HandleScope hs;
World_ft = v8::Persistent<v8::FunctionTemplate>::New( v8::FunctionTemplate::New() );
World_ft->SetClassName( v8String( "World" ) );
v8::Local<v8::ObjectTemplate> instance_t = World_ft->InstanceTemplate();
instance_t->SetInternalFieldCount(1);
instance_t->Set( "CreateEntity", v8::FunctionTemplate::New(CreateEntity) );
}
V8_PLUS_LIBRARY_END( World )
namespace v8pWorld
{
v8::Local<v8::Object> CreateObject( void )
{
v8::Local<v8::Object> objReturn = World_ft->GetFunction()->NewInstance();
return objReturn;
}
}
CreateEntity is a member entity, and Construct creates the template.
In v8 everything happens in context. A context is a world where all the variables and objects live. If you don’t have a context you can’t make real objects and variables.. but you can make object templates. Object templates can be used by any of the context’s to quickly create an object.
In the above code we created an object template for a world object. Later on, when a world class is created, it creates the script enviroment and throws itself out as a global called world, using the object template above.
m_v8World.Set( v8pWorld::CreateObject() ); // Make a new world object from the template m_v8World.Get()->SetInternalField( 0, v8::External::New(this) ); // Set the internal pointer to this class m_v8Plus->SetGlobal( "world", m_v8World.Get() ); // and set it as a global
Calling
Calling is pretty simple and straight forward..
v8::Handle<v8::Value> arg[1] = { v8::Number::New(1) };
v8::Handle<v8::Value> returnval = functionobject->Call( context->Global(), 1, arg );
And obviously gets even simpler once you’ve wrapped the shit out of it
v8LocalValue vObjectProtoType = v8p->CallFunction( "EntityFactory", "GetPrototype", v8String( GetClassName().c_str() ) );
You can store the functions as objects and call whenever you want.
Error Catching
You can catch errors by having a v8::TryCatch in scope.
v8::TryCatch try_catch;
v8::Handle<v8::Script> script = v8::Script::Compile( v8String( strValue.c_str() ), v8String( strFileName.c_str() ) );
if ( script.IsEmpty() )
{
ReportException( try_catch );
return false;
}
The try_catch variable can then give you line number, source code, even the column in the code where it all went wrong.
Syntax
It’s javascript. Big woop.
World = {}
World.Initialize = function( )
{
Msg( "Initialize\n" );
Msg( "World: " , world, "\n" );
for (var i = 0; i < 10000; i ++ )
{
var ent = world.CreateEntity( "TestEntity" );
ent.SetPos( Vector( 1, i * 2, 1 ) );
ent.Spawn();
}
}
World.Shutdown = function( )
{
Msg( "ShutDown\n" );
}
World.OnPlayerJoin = function( player )
{
Msg( "Player Join: ", player, "\n" );
}
World.OnPlayerLeave = function( player )
{
Msg( "Player Leave: ", player, "\n" );
}
People say Lua syntax is fine. No it isn’t. If you’re basing that on GMod’s Lua implementation you’re totally wrong. I hacked the syntax in GMod to include things like c++ style comments, here’s Lua comments..
-- OK single line comments aren't so bad --[[ Oh what the fuck is this Lua you idiot, why are you trying to be different!?!?! THIS IS NOT BETTER OR SIMPLER THAN C STYLE COMMENTS --]]
Speed
Although it’s probably the most important thing when selecting a scripting language, I’ve got to admit, it’s not the most important thing to me. As long as it runs at a reasonable speed I’m happy. And it does.
I think the important thing to bear in mind when scripting in a game engine is that you’re prototyping in script. A lot of the modules you make should be transferred back into the engine.. and if you’re scripting in a nice modular manner then that should be a piece of piss to do.
From what I’ve heard, v8 is faster than standard Lua. But is slower than Lua JIT. But these things almost always depend on the benchmark, and don’t always apply to your application. If your app doesn’t append a character to a string 50,000 times in a for loop, then that specific benchmark is probably useless to you.
Summary
I’m happy where it’s going. I’m happy with the Javascript .prototype stuff. I appreciate that v8 is in the very early stages of development, and I’m happy that it is being actively developed. If anything changes in the situation I’ll let ya’ll know.
Sounds like you made the right choice!
How hard would V8 be to implement in a game/mod like the Source Engine?
Also, personally I’d rather have JS over Lua for Garry’s Mod, but if it’s slower, well then.
The syntax is basically the same as gamemonkey? (Sorry never seen/used javascript before).
This is pretty exciting, because I actually ‘like’ Javascript. I hated Lua.
You are aware that in Lua, you don’t need to put a comment on the closing line, right?
–[[
SOME CODE HERE
]]
And that’s it. It works by making the entire block a string, and then commenting the string out.
Also, how the hell is this simpler:
v8LocalValue vObjectProtoType = v8p->CallFunction( “EntityFactory”, “GetPrototype”, v8String( GetClassName().c_str() ) );
Seriously.
local res = func( arg, arg, arg );
Lua syntax is fine if you aren’t concerned about it not being like C++/C.
Regardless, glad it’s shaping up for you.
Google v8 king’d :P
JavaScriptCore
make more blog posts about your action-packed life please.
@5
My example is an example of BINDING C FUNCTIONS TO JS. That code is C++, not Javascript.
Garry: The king of cherry picking the work of others for his own profit.
If speed is your concern, just use Java (or heck even .NET). Java’s JIT will always be faster than any JavaScript JIT, and apparently it is possible to use it for scripting from what I’ve heard. Not to mention it gets rid of the crap JavaScript throws at you (like “31″+7=”317″, but “31″-7=24).
Mind posting v8Plus? Seems useful.
bahahahaha, yeah, you hacked it in, and that was stupid.
O thank god that the first parts of code are C++.
You scared the crap out of me, since I thought I knew JavaScript pretty well by now.
I’ve started to learn Visual C++ but that still looks crazy to me. Yet the JS code looks very nice, YAY! Wait… why am I saying yay!? I don’t even know if we’ll ever even touch this engine.
Yeah, Lua syntax is evil: It makes me code if () then end in PHP,JavaScript and Java where I haven’t to and these languages make me script if() {} in Lua.
Horrible.
I just wish V8 would run in GMod10 but that will probably always only a wish (why to change?).
abc123: Java? Since when does Java have a compiler that can be embedded in own applications?
@5: You’re comparing apples to oranges.
You compared a Lua comment to C code to bind v8 classes to C functions.
The actual stuff you’d write is under “Syntax”, and that stuff seems simple enough to me.
@15
Learn plain C/C++ then. You can use stuff like FLTK or wxWidgets to get nice GUI’s.
sexy syntax
Cochrane: That’s what the Stencyl guys are doing with their stuff.
http://blogs.stencyl.com/team/
IT TOLD ME TO SMELL YOUR BALLS, GARRY.
Why don’t you just fork Lua?
@19 Yeah, I have a steep learning curve so learning C++ souldn’t take too long. I learned VB in a month(most of it). Though, I wanna know C++ so I can code better stuff.(game engine)
Garry, do you remember exactly one year ago today when you posted about email capsules? I received an email from myself 1 year later, and I just wanted to say thanks. Here’s your reference post:
http://www.garry.tv/?p=324
Have a good one!
Js in gmod please, it would open up modding it for so many people.
Garry, wouldn’t it be possible for V8 and LUA to coexist in gmod?
Mark me too, I just got mine as well
Lua is easier.
LuaManager.RegisterFunction( “WriteStatus”, this, type.GetMethod( “WriteStatus” ) );
:D
At any rate since when does the characters used for commenting make any difference to anything really? SHIT GUYS LETS NOT USE RUBY BECAUSE IT HAS # COMMENTS!
If you have problems switching between the various semantics of different languages maybe you shouldn’t be using them in the first place.
I’m not really up to JavaScript specially but just in case you added :: and ->, please get rid of them and use anything JCC (http://java.sun.com/docs/codeconv/) suggests. V8 makes JavaScript a VM language, don’t try to mix non-VM code specific syntax in there.
…that is of course if :: and -> aren’t part of JavaScript (which I think they are not). Also argh, no new line before { !
I’m impressed; V8 just gained itself another user.
The Lua implementation is just too dirty to stand.
Why should he add ‘::’ and ‘->’?! ‘::’ would be a bit understandable, though, but just why?
And what’s “non-VM code”/”non-VM code specific syntax”? The syntax is based upon the language, not on the way it is executed.
Also, what’s wrong with a new line before ‘{‘?
I’ve been programming in standard Lua for the past 6 months and its not all at different from GMod Lua. Garry added a few C operators (!=, ||, &&), the C comments (which I don’t use any more, –[[ ]] is only 2 extra character and syntax highlighting likes it better), broke the package system for nested modules, and added a bazillion C bindings.
peon (#29):
1) That’s C++ (not JavaScript)
2) You linked to the code conventions to Java (which is not JavaScript)
3) I don’t think you have any clue what the fuck you’re talking about.
It there a way to do namespaces in V8? If not you should add it in.
Ex:
Engine.Particles.NewParticleEmitter(blah blah arguments)
Particles Pull from engines namespace, etc.
It would be nice. :D
No, smell MY balls; you dirty, dirty man.
but the it the but it are too many {‘s my brayn hurt
@36
What? You mean you don’t understand those brackets?
They do like closing parts of code into a group – to make it more readable there are tabs/spaces.
I wish someone could give a straight answer.
33:
1) OK, then it’s irrelevant.
2) Wow, you noticed? JCC stands for JAVA coding conventions which are what I use when I’m working – I get paid for coding Java.
3) That’s great, I think you’re a purple clown with seven legs and a hairy nose.
Line change before } is just completely useless and doesn’t make the code easier to read at all. Matter of preference, may take some time to get used to if you’re not doing it but after that it’ll be more compact and everything’s fine.
YOU NEED MOAR BLOGPOSTS
@39
2) Well then, Java is still not JavaScript.
3) OMG HE’S NOT USING MY PREFERRED CODING-STILE, KILL IT WITH FIRE!! Seriously.. the new line does not hurt.
PLZ! could there be download link for garrys mod 9 on this site?
LOL
It’s his blog, you know? Not his website or summin.
Also, GMod 10 is worth it’s money for sure!
Else… GOOGLE douchebag.
Garry, are you going to make a new game?
and javascript rocks
I have tested v8 embeded into c++. It is amazing easy. check the test_api.cpp file. It is so easy.
It is totally fine to use v8 javascript as in game script.
For 3,000,000 javascript calls, the time used is about 1 seconds. There is no performance issue anymore.
i will try to use GWT compiler to compile java code to javascript. and then run it in v8 engine.
I think it is useful…
javascript is much more powerful than Lua. javascript has very big community and libraries like xml parsers etc…almost unlimited libraries in javascript.
i like this site so more time i visit here!This will give people a starting point to get to know each other before you have … Don't let budget stand in the way of getting people together
“31″+”7″ = “317″ makes perfect sense. You're concating 2 string ffs. “31″-7=24 makes perfect sense too if you know how JavaScript works (kinda like all programming languages have rules). Just sounds like horribad programmer shooting off his own foot.