Saturday 23 November 2013

Scripting Procrastination, Squirrel and Lua

This blog I will be discussing scripting and my experience with it. At the end of this blog you will know where I am with scripting in my game. To begin this blog it would be best to go over what scripting is and what it does.

My game currently is written in C++, a great general purpose and robust intermediate programing language. C++ code is written, compiled and executed. When a line of code is changed in the program the entire program must recompile before it can be executed. Now this is fine for small console applications where the compile time is short. With larger applications like my game for example with precompiled headers, third party libraries, engine source files and my user source files the compile time of my game is quite lengthy.  Recompiling time has started to get around a minute in some occasions. If you are working with a full industrial engine the compile time is not worth it. There must be a way to compile code on run time. Scripting provides this functionality.

Scripts are small programs written in text style files and compiled at run time. You can code these files separately from your engine and they will still compile. Scripts have two main functions within engines.

You can take variables from your sources code and use it within the script. For example you can write functions in a script and then call it in your C++ code. A function called foo is written in the script. The function takes in variables and returns a value. In your C++ code you would call this function and send it the variables you want it to work with. The function will return a value after it is done its execution. If the function is not returning the values you are looking for then you can easy change the function and you will not have to recompile your C++ code.
The second way to use scripting and the implementation that we are using in our game is calling C++ code from scripts. For example you have a GameObject initialization and it takes in variables to determine what it is. The function takes in the maya name, health, if it is a static or dynamic object, and if it is visible. In your script you call this function and send it the information for the object that you want to create.

Our GDW game requires scripting to be implemented in some way or another. It does not matter what scripting language you use as long as it is implemented. At the beginning we were looking at all of our choices: Angel script, Python, Lua and Squirrel were the ones that we initially looked at. We then narrowed it down to two Lua and Squirrel.

First attempt was Squirrel as we liked the language and though that it would work for our purposes. At the start the Squirrel was easy to install as it came with header and library files ready to go. But as we started to try and use it we found that the documentation was almost nonexistent and everything that we could find told us to use a third party library called Sqrat. After many attempts to implement it we scrapped Squirrel and went with our alternative.

Since the documentation for Squirrel was so terrible we were glad to see that there was documentation as far as the eye could see. This was a promising start indeed. When we tried to download and install it we realized that Lua is written for UNIX based system and we had a hard time creating the source and lib files. I spent a solid day trying to cMake the project just like tutorials said but my efforts were in vain. After a good session of fist a cuffs with creating the source files we finally found premade files. Now that we had Lua source files we could install it but when it came to using it we found yet again that everyone was using a third party library called LuaBind. After we got LuaBind installed we were able to get a console message on the screen and that is all we have for scripting so far.

During this semester we spent a lot of time implementing scripting and have not had the result that we were looking at. It was decided that we would put it on hold while we turned our focus to gameplay. I am happy to say that yes we now have gameplay. Now just have to dance with scripting again… joy.

Overall scripting is great when used correctly (still have to figure that out) and can make developing games easier. Again this is just another tool for you to use to make programs and games better.

No comments:

Post a Comment