lua mysteries

Discussion of Lua and LuaWML support, development, and ideas.

Moderator: Forum Moderators

Post Reply
denispir
Posts: 184
Joined: March 14th, 2013, 12:26 am

lua mysteries

Post by denispir »

Hello Luaists,

I have much programmed in Lua long ago, but my very bad memory cannot remmember anything of the details. Below a few issues I find myself unable to deal with alone:

* I have searched long, also much on the lua users wiki, but there definitely seems to be nothing about directories, changing dir, knowing current dir... How to do? Is there a builtin functionality? Indeed I don't want to import a whole module, written in C like lfs, just for that! And precisely, since such modules are in C, reading their code does not point me to any Lua-proper feature.

* Also, to read or write a file in current dir, under *nix I have to manually add './' in front of the filename. (Was hard to find the bug!) Then, how to write a func independant of actual system?

* By the way, I also could not find any way to know under which system the code runs: is there any? Else, how to deal with issues like the above one?

* About reading in a file (and in my case stdin), the "formats" mentionned in the doc ("n", "l", "a"...) just don't work: I get an error about invalid arg. I remember a bug about that, not the solution.

Code: Select all

        // input is io.stdin
        local coord = input:read('n')         -- 'n' reads and converts a number
        --> lua: make_area_map.lua:158: bad argument #1 to 'read' (invalid option)
* math.tointeger and math.type mentionned in the doc just don't exist! The math table doesn't have such keys (value=nil). I don't remember them by the way, but there used to be something else, probably a toint func. How to know if a number is integer (or rather natural number, a valid index, more precisely an x/y on map)? I could work around other issues for a while, in dev time, but this ones blocks me totally.
User avatar
Ravana
Forum Moderator
Posts: 2948
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: lua mysteries

Post by Ravana »

Most of Lua is blocked for security.
denispir
Posts: 184
Joined: March 14th, 2013, 12:26 am

Re: lua mysteries

Post by denispir »

Ravana wrote: December 5th, 2019, 6:02 pm Most of Lua is blocked for security.
What a fast reply!
Well, maybe, but most of what I ask also does not seem to exist in Lua itself. And for what is supposed to exist, I cannot see any security reason for Wesnoth to prevent us to know whether a number is an int! ;-)
Finally, how to know to which elements of Lua we have access or not?
User avatar
Ravana
Forum Moderator
Posts: 2948
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: lua mysteries

Post by Ravana »

Code: Select all

$ math.type
function: 00bc9500
$ math.tointeger
function: 00bc96a0
Last point is available, all others should be restricted. There is wesnoth API for reading files.
Tad_Carlucci
Inactive Developer
Posts: 503
Joined: April 24th, 2016, 4:18 pm

Re: lua mysteries

Post by Tad_Carlucci »

The full language is available.

Several optional, add-on libraries which come with the language are disabled.

To answer the question more specifically: stick to the Lua language and avoid the use of libraries and you'll be fine.
I forked real life and now I'm getting merge conflicts.
denispir
Posts: 184
Joined: March 14th, 2013, 12:26 am

Re: lua mysteries

Post by denispir »

Tad_Carlucci wrote: December 5th, 2019, 7:27 pm The full language is available.

Several optional, add-on libraries which come with the language are disabled.

To answer the question more specifically: stick to the Lua language and avoid the use of libraries and you'll be fine.
All right. I did not try to use any external library. But my problems remain, including number issues above: math.type and math.tointeger do not exist as by Ravana and yet I have lua5.3, as shown by the simple command "lua" in the terminal. Unfortunately, since apt/dpkg is broken by me (the well-known issue by unbuntu, with no remedy it seems) I won't try to uninstall and reinstall it, else I probably have no lua at all anymore (I must wait until I'm ready to completely erase everything and change to another distrib).
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: lua mysteries

Post by gfgtdf »

The version lua that wesnoth uses is independent from which lua version you have otherwise installed on your system. Also ravana did not say that these fun ruins don't exist.
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.
Tad_Carlucci
Inactive Developer
Posts: 503
Joined: April 24th, 2016, 4:18 pm

Re: lua mysteries

Post by Tad_Carlucci »

Wesnoth presently has Lua 5.3.5 built-in on master and the 1.14 branch.

The following standard Lua libraries are loaded:

"table"
"string"
"math"
"coroutine"
"debug"
"os" (see below)
"utf8"

N.B. All Lua language features, including libraries, are included in the Wesnoth executable. Wesnoth does not use, or require, any external Lua features.

N.B. Within the "os" library, several functions are disabled. If I read it correctly, only os.clock os.date os.time and os.difftime are allowed. The Lua functions loadfile and dofile are not available to Lua scripts.

N.B. While it's possible to develop and test basic Lua scripts using an external Lua interpreter, none of the Wesnoth-specific features will be available.
I forked real life and now I'm getting merge conflicts.
User avatar
Celtic_Minstrel
Developer
Posts: 2166
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: lua mysteries

Post by Celtic_Minstrel »

denispir wrote: December 5th, 2019, 5:59 pm * I have searched long, also much on the lua users wiki, but there definitely seems to be nothing about directories, changing dir, knowing current dir... How to do? Is there a builtin functionality? Indeed I don't want to import a whole module, written in C like lfs, just for that! And precisely, since such modules are in C, reading their code does not point me to any Lua-proper feature.

* Also, to read or write a file in current dir, under *nix I have to manually add './' in front of the filename. (Was hard to find the bug!) Then, how to write a func independant of actual system?
Though Lua has an "io" module, this module is not available to Lua scripts in Wesnoth. There is no access to the filesystem in Wesnoth. You can load files in the core or add-on directories via wesnoth.read_file or wml.load, however. If you're using Lua outside of Wesnoth then yeah, the "io" module has no concept of the filesystem - it's just read and write as far as I can see.
denispir wrote: December 5th, 2019, 5:59 pm * By the way, I also could not find any way to know under which system the code runs: is there any? Else, how to deal with issues like the above one?
If you're asking whether your Lua code in Wesnoth runs on the game's host or on the machines of the other players, the answer is both.
denispir wrote: December 5th, 2019, 5:59 pm * math.tointeger and math.type mentionned in the doc just don't exist! The math table doesn't have such keys (value=nil). I don't remember them by the way, but there used to be something else, probably a toint func. How to know if a number is integer (or rather natural number, a valid index, more precisely an x/y on map)? I could work around other issues for a while, in dev time, but this ones blocks me totally.
These definitely exist in Wesnoth Lua. I noticed them just a few days ago. If you press ~ at the main menu, you should be able to inspect the math table and verify that they work.

Incidentally, if your goal is to use Lua in Wesnoth, that Lua console obtained through pressing ~ is probably a better place to experiment than the lua run from the command-line. Note that if you open the Lua console at the menu, it won't have all the Wesnoth API functions - you need to actually start a game and press it in-game to get everything. Also note that it won't open unless debug mode is enabled (run with --debug flag or enter :debug command while in a game).
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
Pentarctagon
Project Manager
Posts: 5526
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: lua mysteries

Post by Pentarctagon »

There is also --unsafe-scripts you can use from the command line to enable such things, though of course other people won't be able to play any add-on coded assuming those are enabled.
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
User avatar
Celtic_Minstrel
Developer
Posts: 2166
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: lua mysteries

Post by Celtic_Minstrel »

I thought that only enabled the "package" module though, not "io".
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
Pentarctagon
Project Manager
Posts: 5526
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: lua mysteries

Post by Pentarctagon »

Since it lets you load and use arbitrary code, pretty much anything is probably possible.
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
User avatar
Celtic_Minstrel
Developer
Posts: 2166
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: lua mysteries

Post by Celtic_Minstrel »

True, true, but you still won't have the "io" module. You'd have to find a substitute.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
Pentarctagon
Project Manager
Posts: 5526
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: lua mysteries

Post by Pentarctagon »

Relatedly, is it documented somewhere as to how to use it? All I could really find is the output of --help and this issue on github, and given there's been a few people now who might want to make use of it, having a work example would be good (plus it was initially added for a reason, I assume).
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
User avatar
Celtic_Minstrel
Developer
Posts: 2166
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: lua mysteries

Post by Celtic_Minstrel »

If you're asking for documentation on the "io" or "package" libraries, these are part of the Lua distribution so they're documented there.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
Post Reply