Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Communicating from Lua to Cocoa and back? — Gideros Forum

Communicating from Lua to Cocoa and back?

MagnusviriMagnusviri Member
edited February 2012 in General questions
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?

Dislikes: code_monkey

+1 -1 (+0 / -1 )Share on Facebook

Comments

  • Have you looked at plugins?

    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.
  • I hadn't read that. I read something somewhere else that said plugins were via included libraries, and so I assumed that didn't include the app delegate and view controller. But I just read what you pointed me at and it looks like it doesn't matter where the plugin is located and I'm guessing but it seems like

    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?
  • I'm not quite sure what you're trying to achieve, so I don't know what advice to give.

    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.
  • However, the application delegate will of course be different once the application is exported.
    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.
  • Once I get the root view controller can I call a method in it or am I restricted to adding views? Gee, it's been too long since I've done cocoa programming, I can't even remember how to add a view in cocoa... Oh yeah addSubview. Anyway, that's a method so I'm assuming I can call that, or does Gideros call that and do I have to use a Gideros wrapper for it?

    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.
  • I guess I should clarify what I'm thinking then. At first my idea was to put a simple call in my Lua files to make sure that the Xcode project was my Xcode project, so just anti-piracy.

    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.
  • Once I get the root view controller can I call a method in it or am I restricted to adding views? Gee, it's been too long since I've done cocoa programming, I can't even remember how to add a view in cocoa... Oh yeah addSubview. Anyway, that's a method so I'm assuming I can call that, or does Gideros call that and do I have to use a Gideros wrapper for it?
    Once you have the root view controller:

    UIViewController* rootVC = g_getRootViewController();

    you can do
    [rootVC methodName];
    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.
    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.
  • 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?
    I see no reason why not - there's an example in the link in the previous post that has Gideros Studio code that sends data to an NSDictionary, that is held in an array in the plugin, and then the Gideros Studio code can ask for a particular index into that array. (Sample 6).
    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.
    If you know C and C++, then you can do it all in that - it seems to me that Gideros Studio is closer to C++ than Objective C, but you can still link to Objective C classes no problem.

    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
  • Yes ok, I now have a better idea of what is going on. It's very impressive and can do exactly what I'm thinking of.
Sign In or Register to comment.