ANSI C - Using a lua script as a game object

Started by
16 comments, last by NicholasLopez 9 years, 10 months ago

EDIT:

nevermind I figured something out for now, delete this thread. If you do have a solution already figured out, post it anyways.

OP:

Hello,

I made an account years and years back (I never remember posting) and only now have I gotten around to needing some help. I am making (for myself) some tutorials on game making, and I am stuck on lua. I know how it's used, I had something working great before, but it was alot of hassle and more complex than it needed to be (variables were defined in both lua and C and every frame lua would pull/set the variables) and got to a point where it didnt work anymore (frame-independance). I have my code here:

http://pastebin.com/GXbKYxAz

What I thought I could do, was have obj_load() ran only once (once every time a player object is created) and then proc() runs the remainder of the time (i'll set up a end() function later). The issue I am having is proc() does not know what p is. Do you know what I might be able to do to have this running correctly? Thanks

Nick

Advertisement
Deleting your question and one posting an answer is pointless. Someone else might have the same problem. Leave your question (so it's searchable) and just post your solution.

Nothing is as annoying as searching for a problem, finding it, and then seeing the OP post "nm figured it out." :)

Sean Middleditch – Game Systems Engineer – Join my team!

that's what someone in chat told me to do lol. Anyways what I thought was going to work ended up not working. So I need help on this one still.

The issue I am having is proc() does not know what p is.

Actually it does.

Your `p` is global variable. obj_load() is explicit about it. You don't return created object, and you don't put it in some context, so `p` is global.

Can you show me how I fix that? I didn't really understand all of that. Thanks

Your C code creates and removes lua virtual machine with each call, is that what you really wanted?

Player object in `p` variable is lua value, it will not survive when vm is closed.

That's what the tutorials show when I look online. For me to understand, i'm gonna have to see an example.

First you should decide what role will play lua scripts.

Is it just simple scripts that will call some functions from native host, or is it fully featured program.

Simple scripts can be stateless, so you can create/delete lua virtual machines any time.

But complex program will have state. That state will be stored in one or multiple lua virtual machines, and you must decide who owns stuff.

For now you can move lines 38-49 out of procPlayer() function. Put it somewhere at the very beginning of your program.

Also move out lines 59-60, put it right before program exit. You will get single vm storing state between calls to obj_load() and proc().

I can't see in the reply window a thing for code, so if an admin can format it for me. I changed the thing in my C code to this:


/* call at program start */
void scriptBegPlayer() {
    L = lua_open();
    luaL_openlibs(L);
}

/* call at program end */
void scriptEndPlayer() {
    lua_close(L);
}

void procPlayer() {
/* load the Lua file */
    if( luaL_loadfile( L, "player.lua" ) ) {
        printf("ERROR: lua file could not be loaded!\n");
    }
/* run the Lua file */
    lua_pcall(L, 0, 0, 0);
/* this is run upon the objects creation */
    if( player.boot == false ) {
            lua_getglobal(L, "obj_load");
            lua_call(L, 0, 1);
            player.boot = true;
    } else {
            lua_getglobal(L, "proc");
            lua_call(L, 0, 1);
    }
}

and it works correctly now.

You don't have to reload script on every call to procPlayer, that's just a waste of time.

obj_load(), proc() and Player will live in vm until vm closed.

Move calls to lua_loadfile() and first lua_pcall() out of procPlayer().

This topic is closed to new replies.

Advertisement