Welcome to the first episode of Lua Tips & Tricks for Stingray. In this series, we'll cover using Lua to create performant game code in Stingray. While Stingray's visual scripting tool Flow can be used to accomplish alot, sometimes using Flow isn't practical or efficient. In these cases, Lua is the better choice. While writing code isn't for everyone, Lua is a fairly easy scripting language to learn. Certain tasks are much easier to implement in Lua than Flow, and will benefit from the added performance.
This series will not necessarily explain how to write Lua from scratch, but it will attempt to help developers learn how to accomplish various game related tasks in Lua. We hope it will alleviate the discomfort with writing game code, while also answering common community questions. If you have any requests for future Lua Tips & Tricks tutorials, be sure to post them in the comments.
There are basically two types of programming languages: native and interpreted. C++ falls under the native code category. It is compiled into a binary file (EXE) and executed by the CPU. Native code is very fast, but considered hard to learn and hard to master. When handling things like real-time physics or rendering, native code is the way to go. Interpreted languages are much simpler to learn and use. One benefit of an interpreted language is memory management. All the heavy lifting of memory management is handled by a virtual machine. The virtual machine reads and interprets the code line by line at runtime. This of course means that interpreted code executes much slower than native code, and is typically only used for general game code.
Although Lua is an interpreted language, Stingray uses a special version of Lua called LuaJIT (LuaJIT.org). JIT stands for Just-in-time. This means that the Stingray engine compiles the Lua code into native C++ at runtime, making it execute much faster. Lua is also a very lightweight and portable language. You could say that LuaJIT provides the ease of a scripting language with the performance of native C++ code. It can be used on a huge variety of platforms, and is great as an embedded scripting language, thus why it is in Stingray.
Episode 1 - The Vehicle Template
In this episode, we will make some tweaks to the Vehicle Template in Stingray, adding a rear-view camera along with allowing the user to spawn into a vehicle placed in the level editor. While both of these things could be accomplished with Flow, this lesson will give you a good idea of how to modify existing Lua code to add custom features.
Rather than explain the process in this blog post, we will simply include the downloadable scripts below. Be sure to watch the video below to get a full understanding of what is going on. This tutorial was written using the Stingray 1.7 Vehicle Template. Create a new Vehicle Template project before getting started.
Download the Scripts
You can download the completed script files from here: Click here to download
Simply replace the player.lua and vehicle_follow_cam.lua files in the project. These script files are found under \script\lua.
Enjoy the first episode below: