[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: cannot index a table loaded as a chunk
- From: "Michael Newberry" <mnewberry@...>
- Date: Fri, 20 Aug 2004 03:00:12 -0700
I show the source code below. I understand the difference between
LuaL_loadfile and lua_dofile. Originally, I tried using lua_dofile and that
did not work so I tried luaL_loadfile and that didn;t work either. But I'm
wondering about a couple of Windows things. First, can lua_dofile handle
file names with blank spaces in them. Second, this worked as of yesterday,
so could there any weirdness from the XP Service Pack 2 and file access
after my machine has been gobling up little bits and pieces of SP2 from the
internet day by day? Just a couple of thoughts.
Here is some source code fro the 3 players, separated by /////...
////////////////////////////////////////////////////////////////////////////
//////////////
// the C function that includes a lua script file in the current script
int LuaInclude( struct lua_State* L )
{
if ( lua_gettop( L ) == 1 )
{
CString sPath = lua_tostring( L, 1 );
CSplitPath path( sPath );
if ( _tcsclen( path.GetDrive() ) == 0 ) // full path not passed, default
to script folder
{
path.SetPath("");
path.SetFolder( g_sScriptsPath ); // folder where the scripts are
located
path.SetFileNameExt( sPath ); // the filename that was passed
lua_dofile( L, (LPCTSTR)path.Merge() );
}
else
lua_dofile( L, (LPCTSTR)sPath );
}
return 0;
}
////////////////////////////////////////////////////////////////////////////
//////////////
//// the lua script. This is run by lua_pcall( L, ...)
Include("Img.lua")
-- 1. select the names of images from a folder. Two will be needed.
-- get the first image
sName1, bSuccess = ReqFileName() -- get the name of an image file
if ( not bSuccess ) then
Exit("Open dialog canceled");
end
Img1 = Img:new()
^^^^ script fails right here saying that Img is nil and can;t be indexed.
////////////////////////////////////////////////////////////////////////////
//////////////
//// The c;ass 'Img.lua'. This is supposed to be loaded into the main script
by the Include statement:
if not Img then --// begin definition block //////////////////
Img = { _I = nil } -- Constructor, holds a pointer to the C++ image
object
--//////// from the book, p.138
////////////////////////////////////////////////
-- Method: Creates a new instance of an Img
-- Input: nothing
-- Returns: new Img object
function Img:new(o)
o = o or {}
setmetatable(o,self)
self.__index = self
o._I = nil
return o
end
<<<< and more functions defined for the class >>>
end
------------------
And yes, I checked that the path given to lua_dofile in the C function is
good.
Michael
----- Original Message -----
From: "Ben Sunshine-Hill" <sneftel@gmail.com>
To: "Lua list" <lua@bazar2.conectiva.com.br>
Sent: Friday, August 20, 2004 1:52 AM
Subject: Re: cannot index a table loaded as a chunk
> I'm wildly guessing you're executing luaL_loadfile, but never
> executing the resultant chunk.
>
> A chunk is a function. That's all it is. Commonly, it's a function
> which happens to set a lot of global variables, but that's more a
> convention than anything else. The point is, all luaL_loadfile does is
> push a function onto the stack. If you don't execute it, it does
> nothing.
>
> Of course, it's possible your problem lies elsewhere entirely. If so,
> post your code... it makes it much easier to troubleshoot.
>
> Ben
>
> On Fri, 20 Aug 2004 01:33:23 -0700, Michael Newberry
> <mnewberry@axres.com> wrote:
> > Sorry about the problems of clarity and the typos in my previous
message. It
> > is 1:30 in the morning and I have been wrestling with this problem all
> > evening.
> >
> > Michael
> >
> >
> >
> > ----- Original Message -----
> > From: "Michael Newberry" <mnewberry@axres.com>
> > To: "Lua list" <lua@bazar2.conectiva.com.br>
> > Sent: Friday, August 20, 2004 1:29 AM
> > Subject: cannot index a table loaded as a chunk
> >
> > > I am using lus 5.0 and I have a strange problem with tables being used
as
> > > classes. I define the class as a table. It has one variable inside.The
> > > scripthas a command that calls a Cfunction named Include(filename)
which
> > > itself uses luaL_loadfile() to load a class definition named Img. When
the
> > > main chunk gets to Img:new() [as on page 138 of the lua book], it
fails
> > > with this message from lua:
> > >
> > > "... : attempt to index global `Img' (a nil value)"
> > >
> > > In other words, the table Img defined in a separate file is considered
not
> > > yet defined. it is almost as if each chuck is having its own set of
> > "static"
> > > globals rather than merging them into a single pool.
> > >
> > > Ideas?
> > >
> > > Michael
> > >
> > >
> > >
> >
> >