• 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
O-san

Clean lua stack

9 posts in this topic

Hello,
I'm trying to clean the lua stack by using the following piece of code:
[CODE]
int i=0, prev=1,count=0;
while(i!=prev || count<10){
lua_settop (luaVM, 0);
lua_gc (luaVM, LUA_GCCOLLECT, 0);
prev=i;
i=lua_gc(luaVM, LUA_GCCOUNT, 0);
count++;
}
[/CODE]
But it doesn't clean entirely. I still have variables that are not 'nil' when the while loop has finished. The variable 'i' never turn zero but I guess that is expected. I do a minimum of 10 garbage collections just to be on the safe side.

Thanks for reading. Edited by O-san
0

Share this post


Link to post
Share on other sites
Thanks for the response. Yeah, I could re-initialize lua by closing, opening it and registering my functions. I thought my lua variables would be nil when running a script again after gc had been done... well I guess that's not the case. Any ideas how this can be accomplished? i.e.

if initial==nil then initial=0 end

So that it holds true every time I run the script after a gc or similar. Edited by O-san
0

Share this post


Link to post
Share on other sites
In your code, it is pointless to call lua_gc() with LUA_GCCOLLECT 10 times, since LUA_GCCOLLECT forces a full garbage collection cycle. Any garbage that can be collected, will be collected. Subsequent calls will have no garbage to collect, so they just waste time. If you want to do incremental garbage collection, use LUA_GCSTEP and use the last parameter to control the step size. Tweak the value until you get something you like; too low and your garbage pile may outpace your collector, too high might adversely affect your simulation loop if you are generating lots of garbage.

Garbage collection will only clean up "garbage"; that is, variables and objects that are no longer actively referenced in your Lua state. If you have a global variable, i, that is still referenced in the global table of your state, then it won't be collected. The state is persistent between running scripts.

One way of ensuring that variables do not stay persistent between calls to dofile() or similar is to use the local keyword. Any variable declared local within a script chunk will be garbage collected after the script chunk is finished (unless a reference to the variable is returned and stored elsewhere non-locally). Conversely, you could cleanup before your script exits by explicitly setting any objects or variables to nil.
2

Share this post


Link to post
Share on other sites
Thanks for the reply. What I would like is a method to clean the stack of variables and functions that are declared from a script file. Right now the easiest way seems to be lua_close followed by lua_open and re-register my C fuctions.
0

Share this post


Link to post
Share on other sites
Well, like I said, if you declare those variables and functions local inside the script, then they will be automatically cleared when the script exits. Say you have a Lua file that consists of this:

[code]
-- test1.lua
a=3
[/code]

From a Lua prompt, execute call [b]dofile("test1.lua")[/b]. After that executes, call [b]print(a)[/b] and the result will be 3.

Then, modify the script file to this:

[code]
-- test2.lua
local a=3
[/code]

From a new prompt, use dofile to execute that file then again call [b]print(a)[/b]. The result this time now will be nil, since the variable a is local to the script.

If you want a bit more control over the permanence of some things, you can use tables to your advantage. In the script, declare all functions and variables and assign them to be members of a table, rather than lurking in the global space:

[code]
-- test3.lua
localspace=
{
a=3,
f=function(g) print(g) end
}
[/code]

After that script executes, the only thing in global state will be the [b]localspace[/b] table. Then, you can easily wipe everything the script declared simply by setting [b]localspace[/b] to nil; everything it contains will be sent to the garbage heap.

In fact, many references will recommend that you use levels of nesting, rather than dumping everything in the global environment directly. With enormous programs that have lots of data, it can make a difference, since Lua data structures reduce down to a lot of map-type data structures, and searching a set of nested structures can require overall fewer operations than searching one big global map.
2

Share this post


Link to post
Share on other sites
Well, I want the variable to be persistant from frame to frame (script is called each frame), but nil the first frame. The script is associated with an episode, so each time I start the episode the value should be nil. I use lua_pcall if I remember correctly.. I'm at my day job right now.. Sorry if it sounds fuzzy. :-)
0

Share this post


Link to post
Share on other sites
Back at home I tried using a table and it works excellent. I'm not very experienced with tables yet but I've found them quite useful =) Edited by O-san
0

Share this post


Link to post
Share on other sites
Tables are the very heart and soul of Lua. Lua wouldn't be 1/10th as useful without them. :D

Glad you got it figured out.
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