I'm curious if it is possible for the Lua portion of the code to run code that is part of the Xcode project. For example, I am guessing this is how Gideros works.
UIApplicationMain is created in main.mm. It calls application:didFinishLaunchingWithOptions in the app delegate. That adds the view control, and then calls gdr_initialize. The view controller and app delegate call various gdr functions to let gdr know that events have occurred, like app suspend, touch events, etc. All of the gdr stuff calls the appropriate functions in the Lua project.
But is the reverse possible? Is there a way in my Lua code to send a message or call a function that is in the Xcode app delegate or view controller object and get the result?
Comments
Using a plugin you can call a function which can then get at the app delegate etc.
Section 8 in the Ultimate Guide to Gideros should tell you how to write plugins:
http://giderosmobile.com/DevCenter/index.php?title=Ultimate_Guide_to_Gideros_Studio
Section 8.3 has a very simple C function that is called from Lua and returns a value.
REGISTER_PLUGIN("myFirstPlugin", "1.0")
is what causes a C function to be available to Lua (as long as the other requirements like the includes, g_initializePlugin, and loader are met). And I'm guessing that this can be in the app delegate or view controller implementation files. Is that right?
My apps: http://www.yummyyellow.com
I would probably keep the plugin in a separate file, and get [UIApplication sharedApplication].delegate in that file.
You can get the application's root view controller with
UIViewController* rootVC = g_getRootViewController(); //g_getRootViewController is a Gideros function
so you can then add views to rootVC.view.
In testing, the application delegate is GiderosiPhonePlayerAppDelegate, but once exported, it's yourappAppDelegate.
I don't know whether that makes a difference to anything though.
Is there more documentation on this other than those links you sent me? I'm daily reading more and more documentation but I haven't thought plugins was something I'd be interested in so when I skimmed everything at the start I didn't mentally bookmark anything related to plugins.
My apps: http://www.yummyyellow.com
But when I got to thinking about it I started wondering just how much back and forth communication I could do.
Although I don't consider myself an advanced cocoa developer, that's where all of my mobile coding experience is. And I've written a lot of cocoa and have a lot of cocoa demo code. And I just started wondering exactly how much back and forth is possible.
For example, now that I'm explaining this, another idea I just got is I'm wondering if I can have my entire title screens, high score screens, etc in cocoa, and the actual gameplay is in Lua. I could do something like have my own View controller and when I want to run the game I add the Gideros view controller's view to the screen (so it gets touch events) and so the Lua stage displays.
I'm almost positive this is possible. But could I have Lua call my Obj-c code? So say I want to store all of my high score data inside of an NSDictionary and uses Cocoa's NSURL class to send the NSDictonary to a webserver serialized and wrapped up using Cocoa stuff (I have code that does this--sort of). Can I have my Lua code kick off that method?
And I believe it is possible with all you've said so I'm just going to have to experiment and honestly it isnt something I want to attempt now since it isn't a requirement since my only project right now is to learn basics. I guess as I learn more I keep having super detailed questions and I ask those hoping for easy answers. But I think this is enough of an answer and a very cool one as well. I like mixing languages for some reason.
My apps: http://www.yummyyellow.com
UIViewController* rootVC = g_getRootViewController();
you can do
[rootVC methodName];
I learned almost everything about plugins from the included gamekit.mm example. This is a plugin for GameKit.
It actually has an example of an Objective C class which gets the root view controller, and presents a modal view controller on it.
There's a very simple working plugin example that you can download from github and place into your project:
http://www.giderosmobile.com/forum/discussion/433/sample-iphone-plugin
The code is mostly in the Ultimate Guide, but it may help you trace your way through what's happening. There's an example in it of linking to an Objective C class.
It took me a while to understand how it all fits together, and I'm not sure that I have it 100% yet, as it seems the more you know the more you realise you don't know . But the concept of writing as much as you can in Lua and then linking to a plugin when you can't do what you need to, is fascinating to me too.
I'm a fair way through writing a plugin that creates a Core Data (SQLite) database, and retrieves data. That's an example of code that is thoroughly Objective C, and it links very well.
This is another link that I read over and over to try and understand how it all works:
http://www.lua.org/pil/24.html
My apps: http://www.yummyyellow.com