lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


hello again,
just a couple more things i would like to know:

how do you '#include' seperate source files?
dofile( "my_utils.lua" ) ?

also i was wondering about garbage collection and specifically table re-use,
after a table has been filled with data is it possible to free the table and
all indices,
so in effect you have an empty table again? (a global table)
e.g.
-- create a new table...
table = {}
-- fill table with garbage...
table.something = 'something'
table.number = 1
table[1] = 1.99
table[2].number = 999

-- free the table?...
while table[1] do tremove(table) end

-- result: a fresh empty table? (all previously allocated data/indices are
free'd?)

thanks again in advance :-)

here is some of my Lua code so far that may be of interest or maybe not...
note the *** comments!
and also after that i have included a C function i am using to import Lua
tables from C
----------------------------------------------------------------------------
-----------------------------------

readfrom( 'export.txt' )
file = read( "*a" )

-- *** Peter Shook's brilliant floating point parser ***
function getfloat( str )
	local s = strfind( str, '%-?%.?%d' )
	if s then
		local b, e1, e2, e3, e4
		b, e1 = strfind( str, '^%-?%d+%.%d*', s )	-- D+ . D*
		b, e2 = strfind( str, '^%-?%d*%.%d+', s )	-- D* . D+
		b, e3 = strfind( str, '^%-?%d+', s )		-- D+
		local c = e1 or e2 or e3
		b, e4 = strfind( str, '^[Ee][+-]?%d+', c + 1 )
		local e = e4 or c
		return tonumber( strsub( str, s, e ) )
	end
end
--- *** i would like to store the above function in a seperate source file
***

particle = {}
particle.emitter = {}
particle.emitter.force = {}

db_particle =
{
	{ s = 'particles',		v = 'particle.particles',
t = 0 },
	{ s = 'delay',			v = 'particle.emitter.delay',
t = 1 },
	{ s = 'delayFuzz',		v = 'particle.emitter.delayFuzz',
t = 1 },
	{ s = 'emitter_life',	v = 'particle.emitter.emitter_life',	t =
1 },
	{ s = 'theta',			v = 'particle.emitter.theta',
t = 1 },
	{ s = 'thetaFuzz',		v = 'particle.emitter.thetaFuzz',
t = 1 },
	{ s = 'phi',			v = 'particle.emitter.phi',
t = 1 },
	{ s = 'phiFuzz',		v = 'particle.emitter.phiFuzz',
t = 1 },
	{ s = 'speed',			v = 'particle.emitter.speed',
t = 1 },
	{ s = 'speedFuzz',		v = 'particle.emitter.speedFuzz',
t = 1 },
	{ s = 'life',			v = 'particle.emitter.life',
t = 1 },
	{ s = 'lifeFuzz',		v = 'particle.emitter.lifeFuzz',
t = 1 },
	{ s = 'startSize',		v = 'particle.emitter.startSize',
t = 1 },
	{ s = 'startSizeFuzz',	v = 'particle.emitter.startSizeFuzz',	t =
1 },
	{ s = 'rot_speed',		v = 'particle.emitter.rot_speed',
t = 1 },
	{ s = 'force.x',		v = 'particle.emitter.force.x',
t = 1 },
	{ s = 'force.y',		v = 'particle.emitter.force.y',
t = 1 },
	{ s = 'force.z',		v = 'particle.emitter.force.z',
t = 1 }
}

--- *** i would like to store the above table in a seperate source file as
it will be used in both an export script and an import script ***

b = 1
e = 1
i = 1
while db_particle[i] do
	b, e = strfind( file, db_particle[i].s, e )
	b = strfind( file, '=', e + 1 )
	b = b + 1;
	e = strfind( file, '\n', b )
	v = getfloat( strsub( file, b, e ) )
	dostring( db_particle[i].v .. " = v\n" )	-- *** is there
anyway of avoiding the dostring()? ***
	dprintf( '%f\n', v )
	i = i + 1
end

dprintf( 'done!\n' )

----------------------------------------------------------------------------
-----------------------------------
// this function puts a Lua table var on the stack from a string
// e.g. input "particle.emitter.force.x"
// not much error checking!
int lua_get_table( lua_State* L, const char* strTable )
{
	dbgAssertErr( L, E_NULL_POINTER );
	dbgAssertErr( *strTable, E_NULL_STRING );
//	dbgLog1( "stack in: %i\n", lua_gettop(L) );
	char* pBegin;
	char* pEnd;
	pBegin = pEnd = (char*)strTable;
	char strField[40];
	while( *pEnd )
	{
		while( *pEnd && *pEnd != '.' )
			pEnd++;
		strncpy( strField, pBegin, pEnd - pBegin );
		strField[ pEnd - pBegin ] = 0;
		if( pBegin == strTable )
			lua_getglobal( L, strField );
		else
		{
			lua_pushstring( L, strField );
			lua_gettable( L, -2 );
			lua_remove( L, -2 );
		}
		if( !*pEnd )
			break;
		pBegin = ++pEnd;
	}
//	dbgLog1( "stack out: %i\n", lua_gettop(L) );
	return 1;
}