• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
NicholasLopez

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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_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
0

Share this post


Link to post
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 this post


Link to post
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:

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: https://www.youtube.com/watch?v=c4b9lCfSDQM

Edited by DrNicholas
0

Share this post


Link to post
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 this post


Link to post
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_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?

0

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0