Monday, September 23, 2013

[HobbyRPG] tolua++ and menus!

This is turning into some sort of development blog, but from my perspective. Anyway, since LUA and UI were pretty big stuff we intend to add for this sprint, progress was seemingly slow due to time needed to research. I almost went into a huge hole of creating our own UI system but Dom managed to save me by just looking up cocos2d documentation -_-. I think when I figured that I could reinvent the wheel, I would just go ahead and do it without consulting Google or look for existing implementations.
Anyway, a not-so-glamorous progress to motivate ourselves!


For those who can't see the changes, I have neatly boxed them in red, since they are all code-based changes. Dom already told me a bit on how he intends me to use the UI, so I'll work on it once I can find better art. I'll try to follow his idea lol. 

LUA binding is a bitch. Well, it isn't once you know what's happening, but when you have no idea wtf is LUA, it's a bitch. cocos2d-x comes with tolua++ support, and I had no idea what that meant until I spent a couple of hours reading its online reference before I sneezed myself to bed on Saturday night. I felt super terrible yesterday, so terrible that I gave up on implementing it. Today I sneezed less, so I managed to get it working with a few good guesses. 

The documentation online for tolua++ is pretty heavy read for those who don't understand what's going on. Examples and tutorials are almost non-existent; in fact, the best tutorials I found were in Chinese or in Japanese. I totally gave up on the Chinese one. The Japanese one is easier to understand thanks to the existence of katakana. After seeing a few code snippets here and there, I finally figured it out. 

In a gist, tolua++ basically takes in a .pkg or .h file and generate a .cpp file with a function for you to call in your program. Then you just grab the lua state in your program (by right from tolua++.h, cocos2d-x has a wrapper around it) and throw it into the function that tolua++ gave you. I'm too lazy to explain in detail, but here's some poorly made step by step screenshots on how to expose a class to LUA:

First, get tolua++ to run on command line! Download the tolua++ executable and let adjust your Environment Settings' PATH variable to point to it. You know it's working when you open your command line and type "tolua++". The help page will appear:


Go to your project's root and create a .pkg file like so. Note that it looks almost like your .h file. These are the functions you want to expose. 


I must emphasize that the file included in the $cfile line MUST be accessible from the .pkg file's location. If not, tolua++ will complain.  Finally, just run tolua++: 


And voila, if there's no errors, you should magically have a file that looks something like below. If there are errors, well, just try to resolve them. Remember to Google for help (when in doubt, just Google the whole error message).


Add this to your IDE and pray that it works. If it doesn't, you screwed up somewhere in the .pkg file. If you have trouble, remember to Google, ask stackoverflow.com, attempt to find the answer in tolua++'s online documentation and/or actually try to figure out and guess how tolua++ binds your C++ code (it's not THAT magical). 

Moving on, the red box is the function you should be interested in. Simply call this function in your program and pass the lua_state in. The next screenshot is a cocos2d-x example since they have a wrapper: 

And now, when I run my script it works! (script is in first screenshot).

After you have successfully done this, the entire online tolua++ documentation will suddenly make sense! *gasp*





1 comment:

  1. Hi, I just started to make a game with Cococs2d-X for Android device. I want to use lua for scripting in my game, but I don't know how to get lua works with Cococs2d-x. Can you write a tutorial about this?

    ReplyDelete