Scripts / Stacked arrays?

Author
Message
GubbyBlips
5
Years of Service
User Offline
Joined: 14th Jan 2019
Location:
Posted: 11th Sep 2020 22:37
GG stock global variables use what seem to be 'stacked' arrays--
don't know the official term, but they look like;

g_Entity[e]['health']
g_Entity[e]['angley']
g_Entity[e]['x']

How to set those up yourself? I might have a better use for that
type of array when adding some scripts to multiple entities ?
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 11th Sep 2020 23:01
They are called lists and they are the only structure in Lua.

You create an empty list with {} and you can refer to entries in the list by the key value in two ways.

list[ key ]
or
list.key

Whichever you find more readable.

So for example if I have a script attached to an entity and have more than one of them on the map I will have a list named sensibly to store the per-entity values.
Eg I have a script called helicopter.lua and in it I have:
Been there, done that, got all the T-Shirts!
PM
GubbyBlips
5
Years of Service
User Offline
Joined: 14th Jan 2019
Location:
Posted: 12th Sep 2020 16:19
Hi AmenMoses. Thanks for that great info.
What was getting me was that I considered the [e] part as already a created
list assignment. So the confusion for me was that make for example a local--
bananas[e] = 8. That is already a list variable, and to add the next segment--
bananas[e]['old'] = 3
bananas[e]['green'] = 4
bananas[e]['nice'] = 1

Well, you just can add on the secondary list array (?) I'm still thinking
of it that way- it's still not sunk in clear (i'm slow).
I see where you made
helicopters[ e ] = { state = 'init' }
local heli = helicopters[ e ]
with
if heli.state == 'init' then

So........... I'll just have to get used to the syntax (reading it looks foreign).
Thanks.

Uggg- I think I have to re-write my script as I probably made it difficult
to assign multiple entities to the same script.
PM
smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 12th Sep 2020 18:07
basically you just assign it like the first time but for the new level
i.e.
list_name = {}
which means you can write things like
list_name[e] = this_entity
or
list_name[1] = entity_number_1

then to get to the next list index you write
list_name[e] = {}
now you can write
list_name[e][1] = first_stat
list_name[e][2] = second_stat
etc
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 12th Sep 2020 18:21
Lists can contain lists, so for example:

local myList = {}
myList.name = "mylist"
myList.subList = { name = "subList", subSubList = { value1 = 10, value2 = "Hello World" } }

etc
Been there, done that, got all the T-Shirts!
PM
GubbyBlips
5
Years of Service
User Offline
Joined: 14th Jan 2019
Location:
Posted: 12th Sep 2020 21:21
Terrific help. Thanks much.
PM
Teabone
Forum Support
17
Years of Service
User Offline
Joined: 8th Jun 2006
Location: Earth
Posted: 4th Dec 2020 04:14
Thread has been moved to Scripts
Store Assets - Store Link
Free Assets - Resource Link

i7 -2600 CPU @ 3.40GHz - Windows 7 - 8GB RAM - Nivida GeForce GTX 960
Flatlander
GameGuru Master
17
Years of Service
User Offline
Joined: 22nd Jan 2007
Location: The Flatlands
Posted: 5th Dec 2020 23:47 Edited at: 5th Dec 2020 23:52
If AmenMoses and smallg don't mind I would like to give my understanding using terminology I have always used with various programming languages that include BASIC, Pascal, C, Visual Basic and Visual Basic 6, C++, COBOL, and Assembly Language. The ability to have tables/matrices in Lua is referred to as arrays within the official documentation. So, that is why I like to use the terminology of arrays.

Arrays are ordered arrangements of objects, which can have one-dimensionality containing a collection of rows or a multi-dimensionality containing multiple rows and columns.

In Lua, arrays are implemented using indexing tables with integers. A one-dimensional array can be represented using a simple table structure and can be initialized and read using a simple for loop.



When trying to access an element in an index that is not actually there, it returns nil. In Lua, indexing generally starts at index 1. But, it is possible to create objects at index 0 and even below 0 as well, I would suggest to always use "1" as the start index.

OK, let's now look at multi-dimensional arrays. These can be implemented in two ways.

* Array of arrays
* Single dimensional array by manipulating indices

I'm only going to look at an array using "array of arrays."


So, g_Entity[e]['health'], g_Entity[e]['angley'], and g_Entity[e]['x'] are examples of multi-dimensional arrays using "array of arrays".
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: 6th Dec 2020 01:06
Forget everything you have ever learned.

In Lua you have only one structure, it is a called a list.

Within a list you have a key (which is the index to the list) and a value.

The value can be a value, a string or a list.

All values are 64 bit floating point,

All the magic comes from that simple basis.
Been there, done that, got all the T-Shirts!
PM
GubbyBlips
5
Years of Service
User Offline
Joined: 14th Jan 2019
Location:
Posted: 6th Dec 2020 03:20

Thanks for that Flatlander. That was a clear and succinct demonstration
of arrays that helped me see how it's done.
I guess we have the habit of using 'arrays' when as AmenMoses mentions,
the official term is "lists". But even lua.org documentation uses "arrays" and
"matrices". I guess the idea is that when we use a tool to build a structure,
we shouldn't call the structure the tool- is that it?
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 6th Dec 2020 13:53
Strictly speaking arrays and matrices are fixed sized, lists are not fixed.

For example if you created an array with ten items in any other language and then tried to add an eleventh you would get an error (in C++ you can have dynamically sized arrays though) but in Lua it will quite happily add more entries.

In fact depending on how you define your supposed array in Lua you may not end up with what you think you have.

For example if you have:
local myArray = {}

myArray[3] = 'C'
myArray[2] = 'B'
myArray[1] = 'A'

for _, v in pairs( myArray ) do
print( v )
end

Would give you
C
B
A

for _, v in ipairs( myArray ) do
print( v )
end

Would give you
A
B
C

(That's the difference between pairs and ipairs btw)

But you have to be careful:
myPairs[ 2 ] = nil

for _, v in pairs( myArray ) do
print( tostring( v ) )
end

Would now give you:
C
nil
A

Whereas
for _, v in ipairs( myArray ) do
print( v )
end

Would now give you:
A

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

Login to post a reply

Server time is: 2024-03-28 12:12:55
Your offset time is: 2024-03-28 12:12:55