Processing custom scripts

Started by
7 comments, last by All8Up 12 years, 4 months ago
I'm currently working on a game and I wanted to make the game be more flexible in functionality by adding scripts. At the moment, every time I need to reach for a script, I basically open the file, parse it, and let the code act accordingly to the script. So I was wondering, would it make sense to load those scripts once, save them internally in some format, and then work with the already analyzed scripts, or is it okay if the code keeps parsing the scripts? I did this once before in Java but the scripts were fairly simple and I only ran them when an object in the game was spawned (because the settings for those objects were saved in scripts) and I didn't notice any performance hit. It was a 2D sidescroller back then, now we're attempting an RPG if that changes anything.

If I need to expand on the details, then please say so :)

So, parse once, or parse every time?

Yo dawg, don't even trip.

Advertisement
Why not use one of the various scripting languages already out there? Lua, javascript, python, etc.. They all are designed for this purpose and their VMs and interfaces are already coded for you.
Co-founder/Lead Programmer
Bonafide Software, L.L.C.
Fairmont, WV 26554 US
I actually haven't even thought about using another language for scripting. That certainly not a bad idea, but I do eventually want to make a custom scripting system, if for nothing else but practice :) I'll have a look into those languages and see what I can conjure up.

Yo dawg, don't even trip.

Most of the ones out there are also open source, so you might be able to get some ideas for your custom scripting language from their code bases.
Co-founder/Lead Programmer
Bonafide Software, L.L.C.
Fairmont, WV 26554 US

I'm currently working on a game and I wanted to make the game be more flexible in functionality by adding scripts. At the moment, every time I need to reach for a script, I basically open the file, parse it, and let the code act accordingly to the script. So I was wondering, would it make sense to load those scripts once, save them internally in some format, and then work with the already analyzed scripts, or is it okay if the code keeps parsing the scripts? I did this once before in Java but the scripts were fairly simple and I only ran them when an object in the game was spawned (because the settings for those objects were saved in scripts) and I didn't notice any performance hit. It was a 2D sidescroller back then, now we're attempting an RPG if that changes anything.

If I need to expand on the details, then please say so :)

So, parse once, or parse every time?


In general, loading and reparsing all the time is a bad idea. Most script languages allow you to load/parse a script and then save the binary VM opcodes out in a directly usable manner. I use lua for most of this and have wrapped it up with a couple nice utility items for the purposes you are discussing:

1. Direct load, parse and use of script. This is exactly what you say you are doing and I do use it that way in the cases where this is just initialization script which is used once like a config file. If it becomes a performance issue, I have an offline compiler which takes the source and turns it into .luab files which are the binary compiled versions which load in preference to the .lua files. I.e. when I load the script I look for a luab first and use that if it's time stamp is newer than the raw lua fie.
2. Actual game script is loaded and compiled to binary once and the binary representation is kept around for further use.

At this point it is up to you but generally the reason to use scripting is to speed up development turn around times and as such dynamic hot reloading of scripts is often desired. Using the file event api's for your OS is a good starting point but keep in mind that you will often need to modify multiple scripts before they reload. So, make sure you assign scripts a switch which says auto reload or manual reload. In lua, I simply use a meta table item which tells the engine if it should monitor the script source for changes and auto reload or not. For the non-auto reloads I bind a hot key, usually something complicated like "shift-ctrl-alt-R" just to make sure I don't accidentally activate it. It then proceeds to purge and reload all the scripts are not flagged auto in one shot. This freezes the engine inbetween frames (I heavily multi-thread so this has to be done) and causes a considerable hitch but for this purpose, but it is still better than shutting down and restarting the game.

And as jonbonazza says, I suggest using an existing language also. Making new languages is really silly unless you have a very specific reason for it. You mention practice, that's fine but really isn't the game itself more important? :)

In general, loading and reparsing all the time is a bad idea. Most script languages allow you to load/parse a script and then save the binary VM opcodes out in a directly usable manner. I use lua for most of this and have wrapped it up with a couple nice utility items for the purposes you are discussing:

1. Direct load, parse and use of script. This is exactly what you say you are doing and I do use it that way in the cases where this is just initialization script which is used once like a config file. If it becomes a performance issue, I have an offline compiler which takes the source and turns it into .luab files which are the binary compiled versions which load in preference to the .lua files. I.e. when I load the script I look for a luab first and use that if it's time stamp is newer than the raw lua fie.
2. Actual game script is loaded and compiled to binary once and the binary representation is kept around for further use.

At this point it is up to you but generally the reason to use scripting is to speed up development turn around times and as such dynamic hot reloading of scripts is often desired. Using the file event api's for your OS is a good starting point but keep in mind that you will often need to modify multiple scripts before they reload. So, make sure you assign scripts a switch which says auto reload or manual reload. In lua, I simply use a meta table item which tells the engine if it should monitor the script source for changes and auto reload or not. For the non-auto reloads I bind a hot key, usually something complicated like "shift-ctrl-alt-R" just to make sure I don't accidentally activate it. It then proceeds to purge and reload all the scripts are not flagged auto in one shot. This freezes the engine inbetween frames (I heavily multi-thread so this has to be done) and causes a considerable hitch but for this purpose, but it is still better than shutting down and restarting the game.

And as jonbonazza says, I suggest using an existing language also. Making new languages is really silly unless you have a very specific reason for it. You mention practice, that's fine but really isn't the game itself more important? :)


Well, the script isn't all that complicated. Basically, I have a more or less generic entity class which is initialized with a script file to discern between various entities so for the time being, I only load each file once. I was thinking about being able to define new functionality in scripts with functions and I was wondering if I should define some kind of internal storage. Maybe I'll use a scripting language like Lua, I'll still have to think about it.

As far as practicing, to be honest, right now I don't really care so much for games. Unlike other people, I didn't get into programming because I thought making games was cool. I got into programming because I wanted to know why my computer does things the way it does, and I figured it would be best to learn how it works through learning to control it first :) It just so happened that games are something I can apply my knowledge in. I'm not sure if seeing things this way alters my ability as a games programmer, but I personally like it better that way. I find my goals are far less stretched and I can test what I learn a lot faster before I am even half way through the game. That's not to say I don't like making games because I do :D It's just that when I make them I tend to focus more on things I have yet to learn than to crank out as many games as I can like an assembly line.

Yo dawg, don't even trip.


[quote name='AllEightUp' timestamp='1323555580' post='4892646']
In general, loading and reparsing all the time is a bad idea. Most script languages allow you to load/parse a script and then save the binary VM opcodes out in a directly usable manner. I use lua for most of this and have wrapped it up with a couple nice utility items for the purposes you are discussing:

1. Direct load, parse and use of script. This is exactly what you say you are doing and I do use it that way in the cases where this is just initialization script which is used once like a config file. If it becomes a performance issue, I have an offline compiler which takes the source and turns it into .luab files which are the binary compiled versions which load in preference to the .lua files. I.e. when I load the script I look for a luab first and use that if it's time stamp is newer than the raw lua fie.
2. Actual game script is loaded and compiled to binary once and the binary representation is kept around for further use.

At this point it is up to you but generally the reason to use scripting is to speed up development turn around times and as such dynamic hot reloading of scripts is often desired. Using the file event api's for your OS is a good starting point but keep in mind that you will often need to modify multiple scripts before they reload. So, make sure you assign scripts a switch which says auto reload or manual reload. In lua, I simply use a meta table item which tells the engine if it should monitor the script source for changes and auto reload or not. For the non-auto reloads I bind a hot key, usually something complicated like "shift-ctrl-alt-R" just to make sure I don't accidentally activate it. It then proceeds to purge and reload all the scripts are not flagged auto in one shot. This freezes the engine inbetween frames (I heavily multi-thread so this has to be done) and causes a considerable hitch but for this purpose, but it is still better than shutting down and restarting the game.

And as jonbonazza says, I suggest using an existing language also. Making new languages is really silly unless you have a very specific reason for it. You mention practice, that's fine but really isn't the game itself more important? :)


Well, the script isn't all that complicated. Basically, I have a more or less generic entity class which is initialized with a script file to discern between various entities so for the time being, I only load each file once. I was thinking about being able to define new functionality in scripts with functions and I was wondering if I should define some kind of internal storage. Maybe I'll use a scripting language like Lua, I'll still have to think about it.

As far as practicing, to be honest, right now I don't really care so much for games. Unlike other people, I didn't get into programming because I thought making games was cool. I got into programming because I wanted to know why my computer does things the way it does, and I figured it would be best to learn how it works through learning to control it first :) It just so happened that games are something I can apply my knowledge in. I'm not sure if seeing things this way alters my ability as a games programmer, but I personally like it better that way. I find my goals are far less stretched and I can test what I learn a lot faster before I am even half way through the game. That's not to say I don't like making games because I do :D It's just that when I make them I tend to focus more on things I have yet to learn than to crank out as many games as I can like an assembly line.
[/quote]

Trust me, I agree with that point of view. I started programming because I wanted to copy a game and make it better, while I never did that (hey I was 12 :)), it started me programming and I've been doing it professionally for a very long time with a couple pitstops in between. But, while I always like to screw around learning new things, I also have a pretty strong desire to finish things to a high quality on the way. It actually annoys me, to a certain degree, when the bosses say: you've taken it far enough, let the junior finish it, we have something else for you... It's like pulling fingernails to walk away sometimes after all the initial architecture and setup work to let someone else fill in the finishing touches.

Long story short, learning as much as you can is a great thing but if you don't apply it to finish something, it is mental masturbation. Unless you finish things you never really know how it would really pan out in usage. I.e. a scripting language can look really cool, then you go to use it in real code and realize just what a mess it is, or at least which bits suck and which don't. I'll never write another scripting language myself, just not worth the time/effort in 90% of the cases. Not to mention new languages means you have to train everyone from scratch, bah....
Yeah, I think I'm just going to go with Lua at this point. I'll plow through the reference manual and try to set it up. I'll save my parsers for a rainy day ^.^ Thanks guys for all your input!

Yo dawg, don't even trip.


Yeah, I think I'm just going to go with Lua at this point. I'll plow through the reference manual and try to set it up. I'll save my parsers for a rainy day ^.^ Thanks guys for all your input!


If you need help with Lua, let me know. I've become something of a master in integration details, language wise I'm only partially useful though. :) I've implemented integrations several times, I've never been able to really use it in regards to the integration, not my job. :/

This topic is closed to new replies.

Advertisement