Skip to main content
GameDev.net gamedev.net
🔒 Locked

Embedded Lua "dofile" path problem

Started by Corefanatic Sep 19, 2011 at 12:14 AM 3 replies 6.9k views
Original Post
Corefanatic
Corefanatic
Hi,
I have a small problem:

I have Lua embedded in C++. I load and execute my initialization script just fine. At the end of it is dofile command that should execute another script containing loading of my game assets. However, no matter how I change the path of the file, it will not execute.

Should the path be relative to my executable or to the script that is calling the dofile?
JTippetts
JTippetts
dofile() wants the script to be relative to the execution directory. However, if you use require, you can make use of the LUA_PATH global variable to specify directories to search. Using require is similar to dofile, but with the added bonus that subsequent calls to require for a particular file won't trigger a reload/re-parse of the file.
Corefanatic
Corefanatic

dofile() wants the script to be relative to the execution directory. However, if you use require, you can make use of the LUA_PATH global variable to specify directories to search. Using require is similar to dofile, but with the added bonus that subsequent calls to require for a particular file won't trigger a reload/re-parse of the file.


Thank you, that clears up the things and fixed my problem.

I have another question:

In my engine, I load all the scripts into the memory, so I can call doLine and execute scripts from c++. The function to retreive the script returns char*. Is there way to execute this array of characters from within LUA, aka doLine and pass in the char* pointer?
Corefanatic
Corefanatic

The function loadstring can be called with a string, and will return a compiled Lua chunk as an anonymous function:


s="for i=1,10,1 do print(i) end"
chunk=loadstring(s)
chunk()
1
2
3
4
5
6
7
8
9
10



Awesome, thank you!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.