Adding C dependencies (and other libraries) to lua main code

Hello guys,

I was wondering if anybody has successfully loaded an app into xcode with C dependencies that the main lua code can call? I couldn’t find any other work online that did this and any direction would be greatly appreciated.

Cheers :slight_smile:

This is probabbly the best example you’ll get.

https://github.com/NathanFlurry/StackIt

Keep in mind this is the old runtime, the new one works differently. For how it works, just export any project and see gamecenteraddon and iadaddon for more info. One of them has directions for enabling the c code in the Lua code in the .h file, but they don’t have directions on how to program it. Hope this helps.

@mr_ninja Thank you mr ninja, I have some time to figure it out so wish me luck!

cheers.

Hi @archistudent ,

Yes, we did this for our game as the AI was all written in C by one developer and all the View stuff written by me in Lua…

XCode can easily compile in raw C as Objective-C is just a wrapper around C…

For the interface between Lua and C we found the easiest was to:

In Addons/CommonAddon.m:

#import "lua.h"
#import "lauxlib.h"

Then:

- (void) codea:(CodeaViewController*)controller didCreateLuaState:(struct lua_State*)L
{
...
lua_register(L, "myfunction", myfunction);
...
}

static int myfunction(struct lua_State *state)
{
    lua_pushinteger(state, 100);
    lua_pushstring(state, "Hello Lua");
    
    return 2;
}

You can then call this in the Lua code as:

local num, msg = myfunction()

We never found a way to call into Lua from C as that requires a Lua state object that is provided as above, but you would have to create or get…

We got around this by having an event queue populated on the C side and just returning data by using return values on our Lua functions as created above from the draw method or similar…

Hope that helps,

Brookesi

@brookesi , does this work with the new runtime?

@Brookesi ah that sounds a little over my knowledge , but considering the code you wrote - i am wondering if i am even asking the right type of question…my goal is to get https supported in my lua code through luasec , do you think this is the right direction to do that?

@archistudent - have a look at this http://codea.io/talk/discussion/6171/implementing-addon-s-for-beginners - the forum search is your friend :slight_smile:

Hi @Mr_Ninja,

I would think so, but I havent tried it, I believe the main codea Addon class is much the same…

Hi @archistudent,

I suspect you may be able to use the builtin http.request function that takes a parameter table…

I’m guessing here, but you can add specific headers into the parameter table, some of whic could be username/password or oauth style tokens maybe…

If you explain a bit further what you require that may help…

If it is username/password passing you need, you may be able to get away with:

http://username:password@example.com/

this sends the credentials in the standard HTTP “Authorization” header.

Hello @brookesi , I am trying to send a RESTful https.request that has a some image data in its body. The backend is google cloud storage, and my friends tells me that for this backend to work, it has to accept ssl.

I’ve havent tried to hack http.request to make it work yet, and its a little above my knowledge, would it even be possible to hack the input of http.request to make it look like a https for this purpose?

I’m currently going through the addon implementation method posted above to see where that takes me.

Cheers