Home Tutorials Download Beta Store Forum Documentation KnowledgeBase Wiki Blog
Fake the Editor to make it believe it is a device

Scripting, Tutorials

1 Star2 Stars3 Stars4 Stars5 Stars (6 votes, average: 4.83 out of 5)
Loading ... Loading ...

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.

Steps

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:

return system.kOSTypeAndroid

3. Create a function named getClientType that return the constant of the client type you want to simulate:

return system.kClientTypeEmbedded

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)

Conclusion

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

Leave a Reply

You must be logged in to post a comment.