ANSI C - Using a lua script as a game object

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

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:

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:

Advertisement
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.

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?

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?

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.

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.

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)

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.

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

This topic is closed to new replies.

Advertisement