A C-like scripting language?

Started by
56 comments, last by Rebooted 15 years, 10 months ago
Hi, I've been playing CoD4 for a while now, and been making some modifications. I've noticed that the scripting language they use, is very easy and familiar to work with, it has a C-like syntax. I would like to use something similair in my projects aswell, though I don't know how the language is called, if it's IW's (CoD4 makers) property and if it's free to use/downloadable. See this for an explaination of CoD4 scripting language Quake 3 has C files as 'scripting' language (AI logic), but I wonder how they do it. Is it compiled on the fly/when the game lauches/before hand? In other words, is it just hardcoded or like CoD4 approaches this matter? I've seen Lua, but I'm not familiar with the syntax/don't like it very much (problematic to run too for me). I've also tried Cint, which executes C on-the-fly (so C as a scipting language), but I'm unsure if it's wise to use it for games/projects...it rather looks like a learning language... Any thoughts? Thanks. This is an example of C-like script:
#include common_scripts\utility;
#include maps\mp\_utility;

	// This script automaticly plays a users specified oneshot effect on all prefabs that have the 
	// specified "script_struct" and "targetname" It also excepts angles from the "script_struct" 
	// but will set a default angle of ( 0, 0, 0 ) if none is defined.
	//
	// example of the syntax: 
	// global_FX( "targetname", "fxIDname", "fxFile", "delay"
	
main()
{
	
	randomStartDelay = randomfloatrange( -20, -15);
	
	//map_source/prefabs/misc_models/com_barrel_fire.map
	global_FX( "barrel_fireFX_origin", "global_barrel_fire", "fire/firelp_barrel_pm", randomStartDelay, "fire_barrel_small" );

	//map_source/prefabs/misc_models/ch_street_light_02_on.map
	//prefabs/misc_models/ch_street_wall_light_01_on.map
	global_FX( "ch_streetlight_02_FX_origin", "ch_streetlight_02_FX", "misc/lighthaze", randomStartDelay );

	//prefabs/misc_models/me_streetlight_on_scaleddown80.map
	global_FX( "me_streetlight_01_FX_origin", "me_streetlight_01_FX", "misc/lighthaze_bog_a", randomStartDelay );

	//prefabs\village_assault\misc\lamp_post.map
	global_FX( "ch_street_light_01_on", "lamp_glow_FX", "misc/light_glow_white", randomStartDelay );

	//prefabs\village_assault\misc\highway_lamp_post.map
	global_FX( "highway_lamp_post", "ch_streetlight_02_FX", "misc/lighthaze_villassault", randomStartDelay );
	
	//prefabs/misc_models/cs_cargoship_spotlight_on.map
	global_FX( "cs_cargoship_spotlight_on_FX_origin", "cs_cargoship_spotlight_on_FX", "misc/lighthaze", randomStartDelay );

	//prefabs/misc_models/me_dumpster_fire.map
	global_FX( "me_dumpster_fire_FX_origin", "me_dumpster_fire_FX", "fire/firelp_med_pm_nodistort", randomStartDelay, "fire_dumpster_medium" );
 
	//map_source/prefabs/misc_models/com_tires01_burning.map
	global_FX( "com_tires_burning01_FX_origin", "com_tires_burning01_FX", "fire/tire_fire_med", randomStartDelay );

	//map_source/prefabs/icbm/icbm_powerlinetower02.map
	global_FX( "icbm_powerlinetower_FX_origin", "icbm_powerlinetower_FX", "misc/power_tower_light_red_blink", randomStartDelay );

}

global_FX( targetname, fxName, fxFile, delay, soundalias )
{
	// script_structs
	ents = getstructarray(targetname,"targetname");
	if ( !isdefined( ents ) )
		return;
	if ( ents.size <= 0 )
		return;
	
	for ( i = 0 ; i < ents.size ; i++ )
		ents global_FX_create( fxName, fxFile, delay, soundalias );
}

global_FX_create( fxName, fxFile, delay, soundalias )
{
	if ( !isdefined( level._effect ) )
		level._effect = [];
	if ( !isdefined( level._effect[ fxName ] ) )
		level._effect[ fxName ]	= loadfx( fxFile );
	
	// default effect angles if they dont exist
	if ( !isdefined( self.angles ) )
		self.angles = ( 0, 0, 0 );
	
	ent = createOneshotEffect( fxName );
	ent.v[ "origin" ] = ( self.origin );
	ent.v[ "angles" ] = ( self.angles );
	ent.v[ "fxid" ] = fxName;
	ent.v[ "delay" ] = delay;
	if ( isdefined( soundalias ) )
	{
		ent.v[ "soundalias" ] = soundalias;
	}
}
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Advertisement
I tend to find the C syntax annoying, with all those parentheses, brackets and semicolons everywhere. Plus, I've found that many C-family languages follow in C's footsteps of preventing definition of literal arrays (though this is by no means
universal). The only C-family language I can manage is PHP, these days.

My ideal approach would have been, using ML-like syntax:

let delay = randomfloatrange (-20) (-15) initer (fun (t, fx, hz) -> global_FX(t, fx, hz, delay))[ "barrel_fireFX_origin",                "global_barrel_fire",           "fire/firelp_barrel_pm";  "ch_streetlight_02_FX_origin",         "ch_streetlight_02_FX",         "misc/lighthaze";  "me_streetlight_01_FX_origin",         "me_streetlight_01_FX",         "misc/lighthaze_bog_a";  "ch_street_light_01_on",               "lamp_glow_FX",                 "misc/light_glow_white";  "highway_lamp_post",                   "ch_streetlight_02_FX",         "misc/lighthaze_villassault";  "cs_cargoship_spotlight_on_FX_origin", "cs_cargoship_spotlight_on_FX", "misc/lighthaze";  "me_dumpster_fire_FX_origin",          "me_dumpster_fire_FX",          "fire/firelp_med_pm_nodistort";  "com_tires_burning01_FX_origin",       "com_tires_burning01_FX",       "fire/tire_fire_med";  "icbm_powerlinetower_FX_origin",       "icbm_powerlinetower_FX",       "misc/power_tower_light_red_blink" ]
This may sound dumb, but: if you want C, use C. No, seriously :-)

There's tinycc, it lets you compile C from memory to memory, link with and run in the address space of your running application. So basically, you can write C code which works like a script.

Obviously, running binary code in the programs address space is not without potential issues, but this isn't necessarily much better using a script language.

You might also have a look at Angelscript, which isn't precisely like C, but rather similar to C++. It's not bug-free, but nevertheless very usable, actively developed, reasonably fast, and very easy to set up and get running.
Quote:Original post by ToohrVyk
I tend to find the C syntax annoying, with all those parentheses, brackets and semicolons everywhere. Plus, I've found that many C-family languages follow in C's footsteps of preventing definition of literal arrays (though this is by no means
universal). The only C-family language I can manage is PHP, these days.

My ideal approach would have been, using ML-like syntax:

let delay = randomfloatrange (-20) (-15) initer (fun (t, fx, hz) -> global_FX(t, fx, hz, delay))[ "barrel_fireFX_origin",                "global_barrel_fire",           "fire/firelp_barrel_pm";  "ch_streetlight_02_FX_origin",         "ch_streetlight_02_FX",         "misc/lighthaze";  "me_streetlight_01_FX_origin",         "me_streetlight_01_FX",         "misc/lighthaze_bog_a";  "ch_street_light_01_on",               "lamp_glow_FX",                 "misc/light_glow_white";  "highway_lamp_post",                   "ch_streetlight_02_FX",         "misc/lighthaze_villassault";  "cs_cargoship_spotlight_on_FX_origin", "cs_cargoship_spotlight_on_FX", "misc/lighthaze";  "me_dumpster_fire_FX_origin",          "me_dumpster_fire_FX",          "fire/firelp_med_pm_nodistort";  "com_tires_burning01_FX_origin",       "com_tires_burning01_FX",       "fire/tire_fire_med";  "icbm_powerlinetower_FX_origin",       "icbm_powerlinetower_FX",       "misc/power_tower_light_red_blink" ]


Sorry to say, but ML-like looks kind of chaos from what you posted. I prefer the semi-colons actually, it makes it look better structured and better maintained somehow to me.

As for PHP, it's nice but doesn't support in programming wisely and structured. I get lots of questions each day about PHP, and people really make a mess of their code and use completely wrong functions. That is because you can pass a number to a function that handle's strings for example. Using count() instead of strlen() for example.

Quote:Original post by samoth
This may sound dumb, but: if you want C, use C. No, seriously :-)

There's tinycc, it lets you compile C from memory to memory, link with and run in the address space of your running application. So basically, you can write C code which works like a script.

Obviously, running binary code in the programs address space is not without potential issues, but this isn't necessarily much better using a script language.

You might also have a look at Angelscript, which isn't precisely like C, but rather similar to C++. It's not bug-free, but nevertheless very usable, actively developed, reasonably fast, and very easy to set up and get running.


Well, maybe you're right on using C, but it thought it would be good to have a minimum version of C (C-like syntax, which is familiar to me) which doesn't need types to be defined explicitly and has not the hassle with pointers since it's just simple scripting.

I will take a look into AS, I heard it was good but forgot it untill now ^^.

And TinyC, what is it really? In what way is it tiny?

Thanks.

[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Quote:Original post by Decrius
I've seen Lua, but I'm not familiar with the syntax/don't like it very much (problematic to run too for me).

...

Sorry to say, but ML-like looks kind of chaos from what you posted. I prefer the semi-colons actually, it makes it look better structured and better maintained somehow to me.


Sounds like you need to learn some more languages. If not having familiar syntax is a problem for you, it's going to rule out a lot of languages and really hamper you as a programmer. Also, if you see enough different syntaxes, you will be able to quickly learn new languages and also get insight into the ones you already know.

BTW I find ToohrVyk's example much clearer than the C-like one.
Open your mind - this is a perfect opportunity to get familiar with some slightly different syntax.

When I first used Lua, I didn't care much for the syntax. It seemed verbose. It was only when I got into the language that I realised how much code I wasn't writing. Who cares if you have to write "end" instead of "}" when in C like languages you tend to require more code to be written anyway [grin].

The real benefit of scripting languages are the time saving aspects, such as not explicitly mentioning types, garbage collection, anonymous functions or closures, etc.
At my work we use squirrel scripting mainly because testers and technicians that learn how to create/use the squirrel scripts are also learning alot of the c/c++ syntax. Also squirrel has a zlib kind of licence and works in windows and embedded linux and is relatively easy to bind to c++ objects.

http://squirrel-lang.org/
I hear angelscript is also good.
Quote:Original post by Simian Man
Quote:Original post by Decrius
I've seen Lua, but I'm not familiar with the syntax/don't like it very much (problematic to run too for me).

...

Sorry to say, but ML-like looks kind of chaos from what you posted. I prefer the semi-colons actually, it makes it look better structured and better maintained somehow to me.


Sounds like you need to learn some more languages. If not having familiar syntax is a problem for you, it's going to rule out a lot of languages and really hamper you as a programmer. Also, if you see enough different syntaxes, you will be able to quickly learn new languages and also get insight into the ones you already know.

BTW I find ToohrVyk's example much clearer than the C-like one.


Sorry to say, but:

let delay = randomfloatrange (-20) (-15) in
iter (fun (t, fx, hz) -> global_FX(t, fx, hz, delay))

Looks very unclear with the spaces and random-looking brackets. Why does it need brackets around numbers, and what does -20 and -15 mean? It looks more like a language that tries to make source code as small as possible, which they over-do so the readability is decreasing.

As for the array, C has them concentrated by subject, and has rows. While that example has just a block of data which is much harder to read over and looks messy.

Quote:Original post by rip-off
Open your mind - this is a perfect opportunity to get familiar with some slightly different syntax.

When I first used Lua, I didn't care much for the syntax. It seemed verbose. It was only when I got into the language that I realised how much code I wasn't writing. Who cares if you have to write "end" instead of "}" when in C like languages you tend to require more code to be written anyway [grin].

The real benefit of scripting languages are the time saving aspects, such as not explicitly mentioning types, garbage collection, anonymous functions or closures, etc.


True, but since I could hardly get it running and needs all sorts of extensions to work properly, glued, to C, angelscript looks like a better alternative, as it's written to be bind to C.

And more code isn't bad. It gives readabilty and is easier to scan with your eyes, since things are more stretched out. Imagine a language where every word would do like 5 operations...you'd have a 10 line code which does a dozen of things but you can't grasp it since it's very compact written...*shivers*

AS doesn't have auto-type build-in, and it has no pointers...so what would be really the advantage to use AS over plain C? It's compiled on the fly, has no pointer stuff, but is slower and needs types...any more thoughts?

Thanks though.

PS: later I will learn more languages, I don't want to swap language each year, first need to stick with C/C++ ;)
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Quote:Original post by drac_gd
At my work we use squirrel scripting mainly because testers and technicians that learn how to create/use the squirrel scripts are also learning alot of the c/c++ syntax. Also squirrel has a zlib kind of licence and works in windows and embedded linux and is relatively easy to bind to c++ objects.

http://squirrel-lang.org/
I hear angelscript is also good.


Thanks, that looks good too. How well can it be bind to C/C++?

[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
The -20 and -15 are clearly the constants taken from your C-style snippet.

I could post any number of little programs that make perfect sense to those who know the syntax, but would baffle someone who only knows C style syntax. It looks unclear, but that does not mean it is unclear.

I have never used ML, but looking at that code, combined with the C style implementation and my knowledge of different languages I can make sense of it and how it probably works, even if I cannot write it.

That is one benefit of using many languages, eventually you can see the patterns underneath the syntax even if you have never used the language in question.

Quote:
AS doesn't have auto-type build-in, and it has no pointers...so what would be really the advantage to use AS over plain C? It's compiled on the fly, has no pointer stuff, but is slower and needs types...any more thoughts?


The most important thing to remember though is that scripting is supposed to make development time faster and easier. If it isn't doing that for you then I would drop it.

If Angelscript looks right for you - go for it.

I haven't used Angelscript but I imagine the benefits are probably safety and developer time. You have no idea how big a difference cutting out the compile and link stages can make to developer time. Small changes can be a simple matter - quit the program, change the script and run again. Or you could make it better. Your game could auto-detect when someone changes a script it uses and re-load the script.

After I made the majority of my game logic scripted, I could go days (or weeks) without compiling my application. All the changes I wanted to make were totally controlled by scripts.

Safety means that your scripts shouldn't be able to crash the game. The game should be able to detect any logic errors in the scripts and cleanly shutdown, notify the user or otherwise sanely handle the error. None of this bad pointer crap that C is known for.

Quote:
It looks more like a language that tries to make source code as small as possible, which they over-do so the readability is decreasing.


"You can make it so complex that people assume its right, or so simple that people know its right".

For someone with experience in ML, I imagine it is the latter.

This topic is closed to new replies.

Advertisement