Fake the Editor to make it believe it is a device
13th September, 2011
Sometimes, it is boring to test your script if your game targets several devices, because you have some “if ( android ) then elseif ( iphone )…” in your code for instance.
There is an easy trick to make your script thinking that it is running on a specific device.
Warning: This tutorial is only viable for games using the Lua script, it doesn’t work for games exported in native code, because it plays with Lua pointers.
The idea is to make some StoneScript functions pointing to custom Lua functions.
1. Create an AIModel “DeviceSimulator”
2. Create a function named getOSType, in which you return the constant of the OS type you want to simulate:
3. Create a function named getClientType that return the constant of the client type you want to simulate:
4. Create a onInit handler in the AI, and override the lua pointers for both system.getOSType and system.getClientType functions by your own ones:
if ( system.getClientType ( ) == system.kClientTypeEditor ) then system.getOSType = this.getOSType system.getClientType = this.getClientType end
5. Compile your AIModel. You will get this error:
“malformed expression, expected ‘(‘”
You can ignore this error, the script will work.
6. Place the AIModel in your User Main AIs of your game, and be sure to place it on the very top (for the onInit of the AIModel to be executed before anything else)
Now, anywhere in your code, when you will call system.getOSType ( ) or system.getClientType ( ), it will call your own functions, returning the constant of your choice.
For instance, in that case, when calling system.getOSType ( ), you will get system.kOSTypeAndroid, even if you are in ShiVa Editor.
This is a very useful usage of the Lua Pointers.
Many powerful things can be done by going deeper than only using the “basic” Lua scripting.
Leave a Reply