Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

External functions in CRL

edited May 2012 in Questions Posts: 437

I'm already in the middle of a new game project, i would like to know how to communicate Codea with XCode Objective C functions, do we need to declare in "C"?

Comments

  • SimeonSimeon Admin Mod
    Posts: 5,424

    Have a look at OSCommands.m / h

    That declares a simple function, openURL, for use with Lua.

    In order to publish it, put a LuaRegFunc declaration into the create method inside LuaState.m (you'll see a whole bunch of other published functions there.)

    If you want to create a whole library then have a look at vec2.c/h. You can then add the library name and open function to the codifylibs[] array in LuaState.m.

  • Posts: 437

    Ok, it is easy! :)
    I have just declared a function :-D

    [OSCommands.h] after alert function:


    int aGameCenter_Start(struct lua_State *L);


    and then the implementation in:

    [OSCommands.mm] after alert function:


    int aGameCenter_Start(struct lua_State *L){
    NSLog(@"Accessing Game Center");
    return 0;
    }


    the only thing left was to call the function "aGameCenter_Start()", in my case ,just from the first line of Main.lua in Project.codea.
    It works in the first try!

  • SimeonSimeon Admin Mod
    Posts: 5,424

    Great! Glad you worked it out.

    If you need to pass arguments or return values, it's a little more complicated — you'll have to read the Lua manual and learn about how to access parameters on the stack. (Though many of the Codea functions return and accept values, so you could look at those, too.)

  • Posts: 159

    I recently added some commands for playing background music in-game (you can specify the audio file to play as a parameter), and found that browsing through the existing Codea commands gave me enough to go on to get it working :)

  • Posts: 80

    That is interesting, I'm planning the same... Mind to share what you did? And what happens in code with, let's say playmp3("damngoodmusic.mp3")... Throws that an error, do I have to add these things after developing in codea and before making an app?

  • Posts: 159

    I'm not at my laptop at the moment but I'll share something tomorrow. Essentially, because the Codea runtime already includes OALSimpleAudio, I just hooked into that and called its playBg methods. I copied the mp3s I'm using into my Xcode project.

    Will post better instructions and some code tomorrow :)

  • Posts: 115

    Thanks frosty, this could be a big advance in my future endeavors.

  • Posts: 437

    I was thinking in the music some days ago, i would like to know how you do that @frosty :)
    When i read frosty it comes to my mind Mr Frosty from Clayfighter (SNES) hahaha

    minute 7:13

  • edited May 2012 Posts: 437

    @Simeon:
    Why is CRL printing all the files it loads?
    example:

    #

    Loading file /var/mobile/Applications/7C259CE1-2556-400C-BF4B-990C730BF641/Documents/Project.codea/Boss.lua
    [lua code here]

    #

    I've just commented in Project.m

    //DBLog(@"Loading file %@", filePath); //Line 171
    ...
    //DBLog(@"Buffer = \n%@", buffer.text); //Line 179
    

    I guess DBLog is the NSLog function to print console text...

    I have another question, is this conversion of a number well done?

    int aGameCenter_reportScore(struct lua_State *state) {
        [CodeaGameCenter reportScore:luaL_checknumber(state, 1)];
        return 0;
    }
    

    CodeaGameCenter is a class i'm preparing for Space Puzzle to release it to the Appstore, as you suggested me, on my way to the first GC leaderboard, the achievements will be easy to get done,but my goal is a 2D GameCenter multiplayer tennis game with physics. Of course my plan is to share all of these :)

    hey @frosty : here an example of music play with the component in AVP framework :

    NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
            resourcePath = [resourcePath stringByAppendingString:@"/mymusic.mp3"];
            NSLog(@"Path to play: %@", resourcePath);
            NSError* err;
    
            //Initialize our player pointing to the path to our resource
            player = [[AVAudioPlayer alloc] initWithContentsOfURL:
                                [NSURL fileURLWithPath:resourcePath] error:&err];
    
            if( err ){
                //bail!
                NSLog(@"Failed with reason: %@", [err localizedDescription]);
            }
            else{
                //set our delegate and begin playback
                player.delegate = self;
                [player play];
            }
    

    tutorial -> http://mobileorchard.com/easy-audio-playback-with-avaudioplayer/

  • SimeonSimeon Admin Mod
    Posts: 5,424

    DBLog is only enabled in debug builds (hence the "DB" naming). It won't affect your release builds, but feel free to no-op the #define in the precompiled header (.pch).

  • @juaxix you say:

    "Ok, it is easy! I have just declared a function
    [OSCommands.h] after alert function:

    and then the implementation in:
    [OSCommands.mm] after alert function:

    the only thing left was to call the function aGameCenter_Start()"

    What about this:

    " a LuaRegFunc declaration into the create method inside LuaState.m"

    //OS Commands
    LuaRegFunc(openURL);
    // Added
    LuaRegFunc(aGameCenter_Start);

    Is this declaration needed or will it work without this LuaRegFunc declaration?

  • Posts: 437

    Ok @bernbout you need to declare the bridge between objc with lua and declare with luareg to be able to use the function inside the code of your Codea app...

  • hmmm - I'm going to have to try this out.

  • @Reefwing. This is what I meant when I posted about using native UI objects. If I can hook into an internal function and call it, I should be able to hook into native UI controls no? Needs more exploration. Would love to know what you come up with.

    @Juaxix. Thanks for clearing that up.

  • .@bernbout - sorry I thought you meant native support in Codea. I guess it would be theoretically possible based on this. I'm not convinced it would be worth the effort though, you may as well just do it in Xcode using Interface Builder. Delegates for example might be tricky.

    I will have a play with Game Center and see how hard it is - the description above sounds straight forward enough. I will then do a tute on it if I can get it working.

    I would vote for native support of Game Center in Codea - is that on the development roadmap @Simeon?

  • @Reefwing No, what I meant was suppose I wanted to use the Time Picker - a native IOS comtrol - you know the one where you have the scrolling rings so you can set the time/date etc - but it should appear in my Codea project.

    So I wanted to be able to declare it in the Codea code and be able to hook into it at runtime.

    At runtime it would appear at the specified location and then values could be set and the selected values returned to the Codea code.

    Similarly with a Button or Textbox or any other simple native IOS control.

    That way I just need a class in Codea say for a button, where I can set its locations, dimensions and the callback for the button-click.

    That way I don't need the extensive LUA code for actually drawing a button, and the UI will have a consistent look.

    The object here is not to use Xcode and IB , which I can do, but to use the native UI controls in a Codea project instead of creating the controls myself in Lua like the aforementioned button class.

  • SimeonSimeon Admin Mod
    Posts: 5,424

    .@bernbout you could certainly modify the runtime to add UIKit elements on top of the render view. However they won't respect any of the current transform or style settings — so they would basically sit outside all of your rendering code. Perhaps this is all you need.

  • .@veeeralp - thanks that should make it easy. And it also answers my question about why it isn't included natively.

    .@bernbout - I understand and as Simeon says (I bet he gets that a lot) it would be possible. My point was more that I didn't think the effort/reward ratio was low enough. Personally if I was doing an App with mostly UIKit controls I would use Xcode. And yes I totally get that you would prefer not to.

Sign In or Register to comment.