Is there any way to make use of libraries of celx functions in celx scripts?
require "celx_utils"
fails with the popup message
Fatal Error
[string "c\Program Files\celestia\extras\Addons\poi..."]:3: attempt to call global 'require' (a nil value)
Specifying
ScriptSystemAccessPolicy "allow"
in celestia.cfg doesn't make any difference.
p.s. it'd be nice if the text in the error popups could be selected and pasted.
"require" fails in celx scripts
-
- Developer
- Posts: 1356
- Joined: 07.01.2005
- With us: 19 years 10 months
- Location: Nancy, France
Selden,
Specifying
in celestia.cfg is a good start.
Then, you must request permission to access the libraries by adding the following lines at the beginning of your script:
Finally, AFAIR, the required file should be defined with a path relative to the celestia base directory.
Specifying
Code: Select all
ScriptSystemAccessPolicy "allow"
Then, you must request permission to access the libraries by adding the following lines at the beginning of your script:
Code: Select all
celestia:requestsystemaccess()
wait(0)
Finally, AFAIR, the required file should be defined with a path relative to the celestia base directory.
@+
Vincent
Celestia Qt4 SVN / Celestia 1.6.1 + Lua Edu Tools v1.2
GeForce 8600 GT 1024MB / AMD Athlon 64 Dual Core / 4Go DDR2 / XP SP3
Vincent
Celestia Qt4 SVN / Celestia 1.6.1 + Lua Edu Tools v1.2
GeForce 8600 GT 1024MB / AMD Athlon 64 Dual Core / 4Go DDR2 / XP SP3
-
Topic authorselden
- Developer
- Posts: 10192
- Joined: 04.09.2002
- With us: 22 years 2 months
- Location: NY, USA
Vincent,
Thanks!
(Although I really think that "require" shouldn't need system access. That opens one up to many possible problems. Accessing a celx code library is much safer than writing to the system environment.)
Thanks!
(Although I really think that "require" shouldn't need system access. That opens one up to many possible problems. Accessing a celx code library is much safer than writing to the system environment.)
Last edited by selden on 13.06.2007, 13:40, edited 1 time in total.
Selden
-
Topic authorselden
- Developer
- Posts: 10192
- Joined: 04.09.2002
- With us: 22 years 2 months
- Location: NY, USA
A related problem:
An Addon's "require"d Lua files are not found if they're in the Addon's subdirectory celxx.
ScriptedOrientation Lua scripts are found if they're in the Addon's subdirectory celxx. That subdirectory should be searched for an Addon's other Lua files, too.
Mixing an Addon's files in with Celestia's own files is not appropriate.
An Addon's "require"d Lua files are not found if they're in the Addon's subdirectory celxx.
ScriptedOrientation Lua scripts are found if they're in the Addon's subdirectory celxx. That subdirectory should be searched for an Addon's other Lua files, too.
Mixing an Addon's files in with Celestia's own files is not appropriate.
Selden
selden wrote:A related problem:
An Addon's "require"d Lua files are not found if they're in the Addon's subdirectory celxx.
ScriptedOrientation Lua scripts are found if they're in the Addon's subdirectory celxx. That subdirectory should be searched for an Addon's other Lua files, too.
Mixing an Addon's files in with Celestia's own files is not appropriate.
Lua (5.1) lets you specify where it should look for "require"d modules. You do this by setting the 'package.path' variable to a string which contains a list of file patterns, separated by semicolons. Each file pattern is a filename template with a question mark where the module name should be substituted.
For example, the following:
Code: Select all
package.path = "lookhere/?.lua;lookthere/?.lua";
require "test";
would load the module "test" from "lookhere/test.lua" or "lookthere/test.lua".
- Hank
-
Topic authorselden
- Developer
- Posts: 10192
- Joined: 04.09.2002
- With us: 22 years 2 months
- Location: NY, USA
Hank,
I'm sorry to report that package.path doesn't do what I need.
My test script (test_require.celx) is in
C:\Program Files\Celestia\extras\Addons\pointing_hubble\
My file of Lua functions (celutil.lua) is in
C:\Program Files\Celestia\extras\Addons\pointing_hubble\celxx\
My test script contains
My file of functions contains
When I double-click on icon of test_require.celx, I get a "Fatal Error" popup that says
Apparently the Lua interpreter's definition of "." is not what I expected.
I'm sorry to report that package.path doesn't do what I need.
My test script (test_require.celx) is in
C:\Program Files\Celestia\extras\Addons\pointing_hubble\
My file of Lua functions (celutil.lua) is in
C:\Program Files\Celestia\extras\Addons\pointing_hubble\celxx\
My test script contains
Code: Select all
celestia:requestsystemaccess(); wait(0)
package.path = "./celxx/?.lua";
require "celutil";
My file of functions contains
Code: Select all
celestia:print("./celxx/celutil loaded" )
When I double-click on icon of test_require.celx, I get a "Fatal Error" popup that says
[string "C:\Program Files\Celestia\extras\Addons\poi..."]:20:module 'celutil' not found
no field package.preload['celutil']
no file '.\celxx\celutil.lua'
no file '.\celutil.dll'
no file 'C:\Program Files\Celestia\celutil.dll'
no file 'C:\Program Files\Celestia\loadalldll'
Apparently the Lua interpreter's definition of "." is not what I expected.
Selden
selden wrote:Hank,
I'm sorry to report that package.path doesn't do what I need. :(
...
Apparently the Lua interpreter's definition of "." is not what I expected. :(
Lua doesn't recognize "." in a file pattern as a reference to the current working directory. You might try a relative path in the file pattern, i.e. "celxx/?.lua", but I'm not sure what the working directory would be here, and Lua may actually require an absolute path.
You could use the explicit path "C:\Program Files\Celestia\extras\Addons\pointing_hubble\celxx\?.lua". Although that would work, it's platform- and installation-specific, which is obviously undesirable. Instead, you could obtain the path to the current Lua source file using the debug library, and use that to construct the path for the library. Something like this:
Code: Select all
local sourceinfo = debug.getinfo(1,"S");
local sourcefile = sourceinfo.source;
sourcefile = string.sub(sourcefile,2);
local sourcedir = string.sub(sourcefile,string.find(sourcefile,".*%/"));
package.path = sourcedir.."celxx/?.lua";
At least, I think something like that might work...
- Hank
-
Topic authorselden
- Developer
- Posts: 10192
- Joined: 04.09.2002
- With us: 22 years 2 months
- Location: NY, USA
Hank,
Since I'm running under Windows, I had to change your code somewhat: the sourcefile string contains "\" not "/". The following worked for me:
Since I'm running under Windows, I had to change your code somewhat: the sourcefile string contains "\" not "/". The following worked for me:
Code: Select all
local sourceinfo = debug.getinfo(1,"S");
local sourcefile = sourceinfo.source;
local n_1,n_2 = string.find(sourcefile,".*%\\")
if (n_1 == nil) then n_1,n_2 =string.find(sourcefile,".*%/") end
local sourcedir = string.sub(sourcefile,n_1,n_2);
package.path = sourcedir.."celxx/?.lua";
require "celutil"
Selden
selden wrote:Hank,
Thanks, but...
This is all extremely ugly :(
What are the chances of adding the Addon's local directory and its subdirectory celxx to the search list of require so its search list is the same as the search list for ScriptedOrientation? Eliminating this inconsistency would help a lot.
It may be ugly, but it works. There's some beauty in that, I think. The alternative is to wait for a future version of Celestia to provide a simpler solution with built-in code. Fortunately, with Lua, that's not the only option. But I would agree with you that pre-loading package.path with a sensible default would be useful for script writers. It shouldn't be too difficult to implement; just a simple matter of programming...
- Hank