Jump to content
  • Advertisement
Sign in to follow this  
KahunaCoder

Loading LUA file

This topic is 2934 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello guys, forgive me if this question has been asked already but I tried searching and couldn't really find anything that helped me out. I've already created a base game engine and I've managed to integrate lua into it.

However, I have a question regarding opening up lua scripts. For testing purposes, I created a myLuaTest.lua file and added it to my Assets folder where most of my game files are stored. I'm using this line to load it in but it can't find it:

int luaScriptID = luaL_loadfile(g_luaVM, "Assets//myLuaTest.lua");
if(luaScriptID != 0)
{
OutputDebugString(L"Error Loading LUA script.\n");
}

I was wondering, is there a reason it's not finding that file in my Assets folder? Do I need to tell lua what my root directory is?

Again, thank you for your patience and your time to help me out.

Share this post


Link to post
Share on other sites
Advertisement
I've tried all combinations of forward and backward slashes...still same problem.

I can try again...thx.

Yep, I've tried \, \\, and / with the same outcome.

Share this post


Link to post
Share on other sites
It's possible that your "myLuaTest.lua" script has an error in it that causes Lua not to load it. What are the contents of that file? Better yet, make an empty Lua script and try using that file instead to see if it at least gets by that line of code. If it does, then you need to check the Lua error that is being generated for your current file to track down the error. Take a look at this example for example for getting the error if there is one after you call that API function.

If that does not solve it, then you just need to output the current working directory before you load the file so you can track down the current path your program is using to work out what you need to do differently. For example, if you drag and drop onto an exe in Windows, the current directory is not necessarily the directory where the exe is but rather some OS specific path. Minor things like that or the way you execute the program from a batch file or a loader process can change your directory and you might not notice it unless you debug.

Share this post


Link to post
Share on other sites
Hello Drew, thanks for your reply. Initially, my test file was blank, but it still had the same behaviors. So I found a test script and this is the contents of my lua file:

function lFunction()
print("Hello From Lua!")
end

I actually saw that link you gave me. I even went so far and decided to include the full path name of where the lua file exists. Still no change.

I'll continue to research how lua behaves during loads. My initial guess was that it was relative to where the executable was, so I cut and paste that test file to that directory. Again, still no go.

I am running the program from Visual Studio.

Hrm...I wonder if my pointer to the lua virtual machine is valid? I just noticed that when I compile my project, it will build but I will get an error:

warning LNK4248: unresolved typeref token (01000013) for 'lua_State'; image may not run

I've checked msdn's site for that compile error and it says:

To correct this error
Provide the type definition in the MSIL module.

Unfortunately, I'm not sure what that really means. :( Again, thanks for your patience!

Share this post


Link to post
Share on other sites
Hi :) Thought I'd just jump on the bandwagon as I have a similar problem. I've compiled the LUA 5.1.4 source to produce a debug and a release library file (lua5.1.lib). I've created a test project called LUATest. In it I've specified the debug and release folder these libs live in under "Additional Library Directories" for the debug/release configurations respectively. I've specified lua5.1.lib in "Additional Dependencies".

The test program I currently have compiles and runs with no reported errors or warnings. Here is a listing for it:


#include <cstdlib>
#include <iostream>

extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}

#include "Debug.h"
#include "Macros.h"

using namespace std;

lua_State* g_pLua = NULL;

int main( int argc, char** argv )
{
g_pLua = lua_open();
if( !g_pLua )
return EXIT_SUCCESS;

luaL_openlibs( g_pLua );

// do stuff
luaL_dostring( g_pLua, "print(\"Hello World from Lua!\")" );
static const char* szLuaMainFilename = "C:\\Users\\Gareth\\Documents\\Visual Studio 2008\\Projects\\LUA\\LUA\\Game\\Scripts\\Main.lua";
if( luaL_dofile( g_pLua, szLuaMainFilename ) )
{
// failed to load file for some reason
DEBUG_PRINT_VA("%s\n", lua_tostring(g_pLua, -1));
}

lua_close( g_pLua );
system( "PAUSE" );
return EXIT_SUCCESS;
}


The LUA in the luaL_dostring() call executes successfully. The file it then tries to load is "Main.lua" which is just a simple program.


print( "Harro! This is Main.lua\n" );


Unfortunately, I also can't get LUA to open and run this file using luaL_dofile(). The error message is reported as: "cannot open C:\Users\Gareth\Documents\Visual Studio 2008\Projects\LUA\LUA\Game\Scripts\Main.lua No such file or directory." I tried dropping Main.lua into the same directory as the executable. I tried putting a copy in every directory of the project thinking if it worked I could use process of elimination to identify a directory that worked. I tried moving the file to C:\Main.lua and loading it from there. I also tried (as you can see) full paths with no luck. I tried forward slashes instead of backslashes. I've also tried changing the working directory for the project.

In my main project I have an asynchronous file loader which I could use in conjunction with luaL_dostring(), but for the purposes of this test project I want to use luaL_dofile(). In fact, there's no reason this should fail. I looked into the definition and it fails on line 564 of lauxlib.c when fopen() returns a null pointer. It fails on a call to a common function, fopen()!?

Has anyone else had a similar problem? Can anyone shed any light on this issue?

Notes: I'm running Windows 7 64 bit with Visual C++ 2008.

Share this post


Link to post
Share on other sites
C:\Users\Gareth\Documents\Visual Studio 2008\Projects\LUA\LUA\Game\Scripts\Main.lua

copy and paste that into your folder window path and see if it exists (i doubt it does)... EDIT: If it does, try putting the script temporarily under C:... or make it relative to where the exe executes is "..\\scripts\\script.lua" would be located in a scripts folder at the same level of the Debug folder (if you have it configured that way).

as for the OP... try dofile... im not sure if load file returns the same thing as dofile but... do file returns 0 or 1 based on success, if it fails, you can grab the lua error.

That resolved link is also dangerous, and looking it up... it seems to be from using precompiled headers, which i think you want to disable in the project properties (possibly under Linker but i am not sure).

[Edited by - AverageJoeSSU on November 4, 2010 11:58:03 AM]

Share this post


Link to post
Share on other sites
Well, now it's always the simplest of things ;)

Looks like when I saved off the file in notepad it automagically made it a .txt file (I must have forgotten to save as all file types). So the file saved as Main.lua.txt! I only noticed that when Windows 7 explorer autofilled after I tried your suggestion. I've only had this laptop a short while and haven't turned on file endings. So when I saw Main.lua in explorer I just assumed it was a lua file :) Oh well. Yep, I'm going back to using relative paths. I was just trying to get it to load any file at all...

Thanks for the help! Perhaps KahunaCoder has a similar issue?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!