@ GubbyBlips
Adding something else. A militar based lesson. LOL
An array is like a bag or a backpack to store things, and then be able to access them when you need it.
Following the example. The array is a bag of fruits that you have filled in the supermarket.
Now let's imagine that there is an empty bag at the door of the bar, and
each soldier when leaving the bar will introduce 1 fruit in the bag.
Now the sergeant wants to know how many fruits are in the bag, what fruit each one has put in, and their identification number.
Now let's imagine that each soldier has a script attached.
local soldierBag = {} -- array constructor
local myID = {}
function soldierid_init(e)
-- here "e" is the soldier id.
myID = e -- store soldier's id. (here "e" is the entity's id)
soldierBag[1] = orange -- here [1] is de array index (key) and "orange" the value
soldierBag[2] = apple -- ditto
soldierBag[3] = lemon -- ditto
end
function soldierid_main(e)
prompt(myID .." - " ..soldierBag[2])
end
An array must be indexed, an array is like a key and value pair, in this case the soldier's id is the key and the fruit is the value.
Guessing than soldier id is equal to 1, output could be:
1 - apple -- 1 (key) - apple (value)
This way to get soldier's info coming in handy to work from the soldier itself, as you can see in the script above, we are getting that info from the soldier itself. Really easy, but what's about sergeant? he has not the soldier ids and therefore the soldier fruits, or What fruit did each soldier put in the bag. Here we has to change the script body, such as:
local sName = ""
function soldier_info_init(e)
sName = GetEntityName(e) -- entity name
-- here "e" is the soldier id. Notice we are indexing array with "e"
myID[e] = e -- global array myID, store soldier's ids.
if sName == "Masked Civilian1" then
Fruits[e] = "orange" -- the fruit the soldier put in the bag.
elseif sName == "Masked Civilian2" then
Fruits[e] = "apple"
else
Fruits[e] = "lemon"
end
end --init
function soldier_info_main(e)
end --main
Note: Notice we are not declaring vars, as in the first script.
Now we guess the sergeant has their own script, such as:
myID = {} -- store soldier's id
Fruits = {}-- store soldier's fruits
local sName = ""
local b = 5
function guetting_info_init(e)
end
function guetting_info_main(e)
for a = 1,g_EntityElementMax,1 do
if g_Entity[a] ~= nil then
if a ~= e then
if myID[a] == a then
sName = GetEntityName(a)
Text(40,b*a,3,myID[a] .." - " ..sName.." - " .. Fruits[a])
end
end
end
end
end
Notice here we are declaring 2 arrays as global vars:
myID = {} -- store soldier's id
Fruits = {}-- store soldier's fruits
myID store the all soldier's ids
Fruits store the all soldier's fruits.
Here you has to loop thru every entity over the map, in order to get the specific soldier's info, ie: the soldier ID (number). Here you are not getting info from the soldier itself, so "e" here do nothing.
Here you work with "myID[a]" so you are indexing array with the iterator (a) rather than with "e".
If you have some question about my "lesson" please let me know.
Laptop: Lenovo - Intel(R) Celeron(R) CPU 1005M @ 1.90GHz
OS: Windows 10 (64) - Ram: 4 gb - Hd: 283 gb - Video card: Intel(R) HD Graphics
cpu mark: 10396.6
2d graphics mark: 947.9
3d graphics mark: 8310.9
memory mark 2584.8
Disk mark: 1146.3
Passmark rating: 3662.4