StompyScript!

Published November 24, 2005
Advertisement
After reading so much of this book, I decided today to try my hand at writing a very simple scripting language. I've got something working now. It only has two commands, but I should be able to add more.

First off, here is an example of a script:
Quote:
PrintString Hello World!
PrintNum 2
PrintString I can print strings!
PrintString From StompyScript!
PrintNum 9999


Here is the source code for the program that executes the scripts:
// StompyScript Runner// Runs scripts written in StompyScript// By Joseph Cortes#include #include #include #include #include using namespace std;// Struct for holding a parameterstruct Param{	int num;	string s;};// Struct holding one command (one line of code)struct Command{	int com_code;	Param param;};// Every line in the codevector lines;bool LoadScriptFile(string filename);void RunScript();int main(){    string filename;    	// Ask what script to run	cout << "What script to execute?" << endl;	cin >> filename;    	if(!LoadScriptFile(filename))	{		cout << "Couldn't load script!" << endl;	    return 0;	}	RunScript();	return 0;}// Loads a StompyScript file// This function takes in the StompyScript// file and breaks the commands into number codes// PrintString = 0 and PrintNum = 1bool LoadScriptFile(string filename){	ifstream script_file;	script_file.open(filename.c_str());    if(!script_file) return false;	while(!script_file.eof())	{		Command com;		string temp;		script_file >> temp;        		// Determine what command is it and act accordingly		if(temp == "PrintString")		{		   com.com_code = 0;		   		   // Skip a character		   script_file.get();		   // Check what string to print.		   // end the string if newline is found		   for(int i = 0; i < 100; i++)		   {			   char c = script_file.get();			   if(c == '\n')			      break;			   else			       com.param.s += c;		   }		   lines.push_back(com);		}		else if(temp == "PrintNum")		{			   com.com_code = 1;		   script_file >> com.param.num;		   lines.push_back(com);			}	}	script_file.close();    return true;}void RunScript(){	for(int i = 0; i < lines.size(); i++)	{		// Read command and do something		switch(lines.com_code)		{		case 0:			cout << lines.param.s << endl;			break;		case 1:			cout << lines.param.num << endl;			break;		}	}}


And here is the result:


Adding some more commands will be easy, but It's going to be kindof hard when variables get involved.
0 likes 3 comments

Comments

nilkn
I once did a command-based scripting system a few years back when I read Game Scripting Mastery. You'd be amazed at all the cool effects you can achieve with such a ridiculously simple system.

Good luck!
November 25, 2005 04:57 AM
choffstein
Right on!

Wait until you get to varaibles...especially trying to make them cross script (global), doing includes, et cetera. Basically, implementing your own stack is a major pain in the ass (my design got fucked up by recursion and I had to add in a nasty hack to make it work.)

Good luck. Scripting systems are very powerful tools indeed, and writing your own (albeit, simple) compiler is a great learning experience.
November 25, 2005 02:09 PM
Rob Loach
Check out GameMonkey [wink].
November 26, 2005 09:52 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement
Advertisement