Followers 0

# ANSI C - Using a lua script as a game object

## 17 posts in this topic

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

Edited by Hodgman
restored from history
0

##### Share on other sites
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." :)
2

##### Share on other sites

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.

0

##### Share on other sites
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.

0

##### Share on other sites

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

0

##### Share on other sites

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.

0

##### Share on other sites

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

0

##### Share on other sites

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().

0

##### Share on other sites

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_call(L, 0, 1);
player.boot = true;
} else {
lua_getglobal(L, "proc");
lua_call(L, 0, 1);
}
}


and it works correctly now.

Edited by Washu
0

##### Share on other sites

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().

0

##### Share on other sites

I thought that is what is going on. Also, new issue.

[attachment=22001:fps_differences.PNG]

now I took this picture when I thought the issue was something different, but it still stands. Here is my player script:

http://pastebin.com/qjrYTSZU

here is anything and everything related to collision:

someone brought it up to me that this is a one-off issue, I am not sure what that is, so someone is going to have to explain that to me. someone brought up that i have two seperate states for falling and jumping, but those are just there to make sure the player begins falling when ySpeed is 0 and the other is to make sure ySpeed never exceeds maximum velocity (140). when I had things printed to console, the y of the ledge the player lands on is 112, the player makes contact at 110.8, this issue I have no idea howto fix.

If you show me what must be done I will be most grateful. Thanks

On the side note: If anyone ever needs help with frame-independant jumping, i found this video very helpful: https://www.youtube.com/watch?v=c4b9lCfSDQM

Edited by DrNicholas
0

##### Share on other sites
That last post is quite a different question altogether, and probably deserves a separate thread. You can link back to this thread to provide background about how you're approaching the scripting.

Just as a hint, I don't think you've posted enough information. For example, we can't see the code where the C variable player.hitbox is set. Also I would be a bit worried that you're not compiling with high enough warnings and warnings set to errors: your C function collision_lua() does not always return a value.
0

##### Share on other sites

I have a response to vstrakh. You said:

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

So I am doing this:

lua_State *L = NULL;

/* call at program start */
void scriptBegPlayer() {
/* initialize Lua */
L = lua_open();
/* load Lua base libraries */
luaL_openlibs(L);
/* register functions within the Lua script */
lua_register(L, "get_dt", getdt_lua);
lua_register(L, "keyboard", keyboard_lua);
lua_register(L, "collision", collision_lua);
lua_register(L, "render", renderPlayer);
lua_register(L, "set_hitbox", set_hitboxPlayer);
lua_register(L, "hitbox_surface", hit_surfPlayer);
/* 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);
}

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

void procPlayer() {
/* this is run upon the objects creation */
if( player.boot == false ) {
lua_call(L, 0, 1);
player.boot = true;
} else {
lua_getglobal(L, "proc");
lua_call(L, 0, 1);
}
}


and the player spawns but doesn't move at all.

Now if I did:

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_call(L, 0, 1);
player.boot = true;
} else {
lua_getglobal(L, "proc");
lua_call(L, 0, 1);
}
}


it works, however I have a small (but very noticeable after longplay) memory leak. How do I fix this?

0

##### Share on other sites
Is this the only such script you load? Have you tried debugging, for instance adding print statements to the "proc" function, or other functions it calls, to see what is or isn't going on?
0

##### Share on other sites

So I am doing this:

...

and the player spawns but doesn't move at all.

You didn't show updated script.

Your scripts in previous posts was only creating global functions/variables, and did nothing else.

If you've added some lines to perform other tasks every time chunk loded - that's totally different case.

0

##### Share on other sites

If you're looking to handle jumping, can I suggest you look at this post.  IMO, having a specific jumping and falling state is silly, it all can be determined based on his y velocity and if he's on the ground or not.

Edited by BeerNutts
0

##### Share on other sites

You did not look at the pastebin, vstrakh

I just said my updated script works better. I had some collision issue's, but those were fixed in another thread. Script works great, but has a memory leak cause it's being loaded over and over I think.

0

##### Share on other sites

So I have done some playing around. Here is my C code:

http://pastebin.com/vY0YrXSx

Here is my lua code:

http://pastebin.com/ybp6qN9E

if I take luaL_loadfile() and put it into scriptBegPlayer(), then most of the player does not work, I see it still renders to screen and I can print stuff (as long as it's within the function being called). Other than that the player stays in one finite location (no falling even) and keypresses dont work. However if I keep luaL_loadfile() where it is inside procPlayer(), it works, but I have a memory leak.

Anybody know how to fix this?

EDIT:

issue was with dt. just made dt a function instead and it worked

EDIT 2:

still getting the memory leak, now that I got one issue solved, i'll probably solve the other issue eventually

EDIT 3:

the memory leak is

lua_call(L, 0, 1);


not sure why and not sure how to fix it

Edited by DrNicholas
0

## Create an account

Register a new account