Scripts / [SOLVED] table library

Author
Message
Flatlander
GameGuru Master
17
Years of Service
User Offline
Joined: 22nd Jan 2007
Location: The Flatlands
Posted: 7th Mar 2020 18:47
In version 5.1 of Lua, they added "table.maxn". GG uses Lua 5.2 and I assumed it should also be available; but, that seems not to be the case. When I use this: max_elements = table.maxn(g_weapons) the error message tells me that "trying to call the field 'maxn' a nil value". So, is this deprecated in 5.2? Or, it just is allowed to work in GG?
Alienware Aurora R7 with SSD 256GB boot drive ( C: ) and a secondary drive ( D: ) that is 2TB
Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz 3.19 with Intel Turbo-burst
Installed RAM 16.0 GB
64-bit operating system, x64-based processor
Windows 10 Home
NVIDIA GeForce GTX 1070 with 8192 MB GDDR5 and 8095 MB shared system memory

The author of this post has marked a post as an answer.

Go to answer
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 7th Mar 2020 19:14 Edited at: 7th Mar 2020 19:17
This post has been marked by the post author as the answer.
Looks like it got taken out of 5.2 as it's not in the 5.2 manual (which is free online btw).

https://www.lua.org/manual/5.2/manual.html#6.5
Been there, done that, got all the T-Shirts!
PM
Flatlander
GameGuru Master
17
Years of Service
User Offline
Joined: 22nd Jan 2007
Location: The Flatlands
Posted: 7th Mar 2020 21:50
Thanks, AM. I guess I was looking in the 5.1 manual. I played around with a couple of ways to get the max number and decided on the following code. Of course, if it is a really long list it may take too long to iterate through the list.

Alienware Aurora R7 with SSD 256GB boot drive ( C: ) and a secondary drive ( D: ) that is 2TB
Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz 3.19 with Intel Turbo-burst
Installed RAM 16.0 GB
64-bit operating system, x64-based processor
Windows 10 Home
NVIDIA GeForce GTX 1070 with 8192 MB GDDR5 and 8095 MB shared system memory
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 7th Mar 2020 22:26
That wouldn't give the same result!

If g_weapons is a simple contiguous list (i.e. like an array) then #g_weapons will tell you how many entries it has.

If otoh it is a more complex list then:


Will tell you how many entries there are in the list.

Been there, done that, got all the T-Shirts!
PM
Flatlander
GameGuru Master
17
Years of Service
User Offline
Joined: 22nd Jan 2007
Location: The Flatlands
Posted: 8th Mar 2020 03:36
I'm using the list as an array. I was looking at two or three Lua forums and there was a discussion about whether or not to use table.maxn or to use the '#' sign. I couldn't believe it would be that simple just to use the '#' in front of the list name. But, by golly, it do work. Thanks for the suggestion.
Alienware Aurora R7 with SSD 256GB boot drive ( C: ) and a secondary drive ( D: ) that is 2TB
Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz 3.19 with Intel Turbo-burst
Installed RAM 16.0 GB
64-bit operating system, x64-based processor
Windows 10 Home
NVIDIA GeForce GTX 1070 with 8192 MB GDDR5 and 8095 MB shared system memory
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 8th Mar 2020 12:13
Yes as long as you have list[1], list[2], …. list[n] as keys than #list will work.

In fact to ensure that this is the case when adding to an array simply use:

list[ #list + 1 ] = value

That way you ensure to grow the 'array' in a contiguous way.

Been there, done that, got all the T-Shirts!
PM

Login to post a reply

Server time is: 2024-03-28 23:37:37
Your offset time is: 2024-03-28 23:37:37