Jump to content

  • Log In with Google      Sign In   
  • Create Account

ANSI C - Using a lua script as a game object


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
17 replies to this topic

#1 DrNicholas   Members   -  Reputation: 130

Like
0Likes
Like

Posted 03 June 2014 - 06:09 PM

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, 03 June 2014 - 09:05 PM.
restored from history


Sponsor:

#2 SeanMiddleditch   Members   -  Reputation: 7261

Like
2Likes
Like

Posted 03 June 2014 - 08:31 PM

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." :)

#3 DrNicholas   Members   -  Reputation: 130

Like
0Likes
Like

Posted 03 June 2014 - 11:41 PM

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.



#4 vstrakh   Members   -  Reputation: 513

Like
0Likes
Like

Posted 04 June 2014 - 02:44 AM

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.



#5 DrNicholas   Members   -  Reputation: 130

Like
0Likes
Like

Posted 04 June 2014 - 08:59 AM

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



#6 vstrakh   Members   -  Reputation: 513

Like
0Likes
Like

Posted 04 June 2014 - 11:30 AM

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.



#7 DrNicholas   Members   -  Reputation: 130

Like
0Likes
Like

Posted 04 June 2014 - 12:24 PM

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



#8 vstrakh   Members   -  Reputation: 513

Like
0Likes
Like

Posted 04 June 2014 - 02:46 PM

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



#9 DrNicholas   Members   -  Reputation: 130

Like
0Likes
Like

Posted 04 June 2014 - 03:38 PM

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.


Edited by Washu, 04 June 2014 - 06:34 PM.


#10 vstrakh   Members   -  Reputation: 513

Like
0Likes
Like

Posted 05 June 2014 - 01:38 AM

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



#11 DrNicholas   Members   -  Reputation: 130

Like
0Likes
Like

Posted 06 June 2014 - 01:29 PM

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

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:

http://pastebin.com/aD61kMNK

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:


Edited by DrNicholas, 06 June 2014 - 01:35 PM.


#12 rip-off   Moderators   -  Reputation: 8763

Like
0Likes
Like

Posted 07 June 2014 - 06:16 AM

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.

#13 DrNicholas   Members   -  Reputation: 130

Like
0Likes
Like

Posted 09 June 2014 - 04:58 PM

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_getglobal(L, "obj_load");
			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_getglobal(L, "obj_load");
			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?



#14 rip-off   Moderators   -  Reputation: 8763

Like
0Likes
Like

Posted 10 June 2014 - 01:41 AM

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?

#15 vstrakh   Members   -  Reputation: 513

Like
0Likes
Like

Posted 10 June 2014 - 02:12 AM


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.



#16 BeerNutts   Crossbones+   -  Reputation: 3018

Like
0Likes
Like

Posted 10 June 2014 - 08:59 AM

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, 10 June 2014 - 08:59 AM.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#17 DrNicholas   Members   -  Reputation: 130

Like
0Likes
Like

Posted 10 June 2014 - 11:34 AM

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.



#18 DrNicholas   Members   -  Reputation: 130

Like
0Likes
Like

Posted 12 June 2014 - 04:13 PM

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, 13 June 2014 - 03:42 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS