Scripts / Local arrays?

Author
Message
GubbyBlips
5
Years of Service
User Offline
Joined: 14th Jan 2019
Location:
Posted: 21st Feb 2020 21:49
Any way to create local arrays /table/? Trying to make a local array broke GG.

For example;
local fruit = {}
fruit[e] = {'apple','orange','grape'}

to call this, I need to call fruit [1] or fruit[2] or fruit[3]
the designator (inside the brackets) can be local, but not
the designatee- or var before the brackets because a local
signifies it is tied to [e]-- the entity assigned the script...

This is all a question. Any clarification/ how to use local arrays
would be very grateful.
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 21st Feb 2020 22:24
local fruit = { 'apple', 'orange', 'grape' }

Then fruit[ 1 ] is 'apple', fruit[ 2 ] is 'orange' etc.

This is a list being used as if it is an array btw, Lua doesn't actually have arrays as such.

Take for example:
local fruits = { apples = 1, oranges = 4, grapes = 2 }

So fruits[ 'oranges' ] is now 4, or a more readable way fruits.oranges.

And if we want the total number of fruits we could do:
local total = fruits.apples + fruits.oranges + fruits.grapes

OR
local total = 0
for _, v in pairs( fruits ) do total = total + v end
Been there, done that, got all the T-Shirts!
PM
GubbyBlips
5
Years of Service
User Offline
Joined: 14th Jan 2019
Location:
Posted: 21st Feb 2020 23:49
I'm going with the text or in other words I want to use
x = 3
Text(10,10,1,"The fruit is a "..fruit[x])
to produce "The fruit is a grape."

I know I can make x local, but doubt it for fruit itself.

if we cannot get local arrays in this manner, I think it will
still work out okay. I'm hoping.
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 21st Feb 2020 23:54
That will work fine using my first example.

i.e. fruit[ x ] where x is 3 will give you 'grape'
Been there, done that, got all the T-Shirts!
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 21st Feb 2020 23:56
There is nothing magical about local variables, they are just limited in scope to the Lua chunk they are defined in, so if defined in the top of the script they will be available everywhere in that script, if defined within a function they are only available in that function etc.

Also they are way more efficient than globals which is why I use them so much.
Been there, done that, got all the T-Shirts!
PM
3com
9
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 22nd Feb 2020 01:17
Quote: "Also they are way more efficient than globals which is why I use them so much."

And resource saver.
Really not standalone game maker, but I think local vars not being porting between levels as globals vars. If so, some sort of "preserve" var might be needed.
preserve var1 = 25
Meanly you want to preserve var1 value, between levels.
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

PM
GubbyBlips
5
Years of Service
User Offline
Joined: 14th Jan 2019
Location:
Posted: 22nd Feb 2020 02:29
I guess what I am still confused about is where does the
local [e] come into play here? Still wondering when you
assign local fruit[e], but you want fruit[x]- but still local?

So the question was -- and it will probably show that I'm ignorant on what's
going on here, but how to use a local array? My question is still there-
to use fruit[x] instead of fruit[e] -- which usually qualifies as the local
variable to [e], the entity. Would there be a way to make fruit[e]
local while still an array (of 1, 2, 3, etc... ?) instead of (e, e, e) IDK??

Testing this broke GG, and it just stopped and re-loaded
without error message.
PM
smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 22nd Feb 2020 10:05
I think you're confusing the question by miss using the term "local"
In coding "local" means it is only used within that section of code - be it inside a function, a loop or a whole script depending how and when the "local" is called.
I.e.
Local b = 19
For x = 1,9 do
Local b = 1
End

In the above b is valid twice and if called inside the 'for loop' it will give a result of 1 but if called outside the loop it will give a result of 19... So as you can see, positioning is very important when it comes to locals.

As for your question about 'e' as "local" - this is how you make something specific to the entity

local fruit = {}
script_init(e)
fruit[e] = {}
fruit[e]['grapes'] = 3
end

script_main(e)
Prompt("you have "..fruit[e]['grapes'].." grapes")
end
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
3com
9
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 22nd Feb 2020 15:30
@ 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.



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:



Note: Notice we are not declaring vars, as in the first script.

Now we guess the sergeant has their own script, such as:



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

PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 22nd Feb 2020 16:11
for k, v in pairs( g_Entity ) do
……..
end

Is way better btw.

Oh and you should check that the entity is actually an active NPC, if using standard AI scripts you can check the ai_bot_state list.
Been there, done that, got all the T-Shirts!
PM
3com
9
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 22nd Feb 2020 20:19
Quote: "for k, v in pairs( g_Entity ) do
……..
end

Is way better btw."


Indeed it is, it is a bit like:
for line in io.lines () do
Or For Each ... Next, in other languages.

Anyway he is starting, let him start with the most basic version, just so that he understands the concept.
There is much to talk about an array / table, and its use.
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

PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 22nd Feb 2020 20:25
Why teach the bad way of doing things? pairs() and ipairs() is the recommended Lua way of iterating lists and are the most efficient way of doing so.

Personally I also think they are the simplest and clearest way.
Been there, done that, got all the T-Shirts!
PM
GubbyBlips
5
Years of Service
User Offline
Joined: 14th Jan 2019
Location:
Posted: 22nd Feb 2020 23:43 Edited at: 22nd Feb 2020 23:58
fruit[e]['grapes'] = 3

script_main(e)
Prompt("you have "..fruit[e]['grapes'].." grapes")

okay, so what if you want to assign the 'grapes' part to 3, is that the same
thing? fruit[e]['3'] -- ('grapes' = 3) = will call the third iteration of fruit?

#######################
I'm glad your on the topic of pairs/ ipairs. I have no idea of their intent.
If I read your instructions and still don't know-- it's not your fault.

Thanks for the tips- I will try to digest them before summer!

BTW, I think I understand the basic precepts of lua tables, it's just how
to localize them that was confusing, for the sake of assigning multiple
entities the same script. But I think I learned something useful-- I'll check!
Ipairs is next, if I can?!
PM
3com
9
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 23rd Feb 2020 03:06 Edited at: 23rd Feb 2020 03:09
@ GubbyBlips
I recommend you check the scripts and try to understand how they work, when you have this clear, then it is time to start making your own changes.
My intention is to try to understand the basics, when you have this clear, then it will be time to take the next step.

pairs and ipairs are the most recommended way according to lua standards to go through a table, these return a key-value pair.
These also have other variants like "next", "continue"
In fact I am using them in the script that I use in convo system.
If you want to read more about it.

Edit: fixing link .
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

PM
GubbyBlips
5
Years of Service
User Offline
Joined: 14th Jan 2019
Location:
Posted: 2nd Apr 2020 06:48 Edited at: 3rd Apr 2020 23:38
I'm back at this again, and I need to explain my question more clearly.
Or, I just need to go about my task some other way...?!

I want to get an attribute of an object (let's call it speed)
and I want to find that attribute from the script of a different object,
so that the two of them communicate and influence one another.
Edit;... for clarity...

Working on better methods...

BTW, all the time necessary was allotted for all the data to accumulate
into the 'brains' of the GG engine. So no variables should be nil for lack
of the frames (I call them clicks) to have ran a few times.
PM
GubbyBlips
5
Years of Service
User Offline
Joined: 14th Jan 2019
Location:
Posted: 2nd Apr 2020 06:52
I think my original post question should have been;
I want to make my own GLOBAL arrays, but ones that reference
local data from other scripts... as I explained above...
PM
smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 2nd Apr 2020 10:59
Are you sure you have
speed = {}
in the first (or both) scripts and not
local speed = {}
In either?
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
3com
9
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 2nd Apr 2020 13:16
Local, global, define the var scope. Guessing you want to rettirvr sll the entities IDs (numbers) at "old school style" :

Firts you wrote main script, such as:

entID = {}

function getting_entityid_init(e)
-- this script retrieve all the IDs
end

Then you wrote the second script, something like:

function giving_myid_init()
entID[e] = e
end

and you attach it to all the entities you want to.

Then you will get all that's id numbers. It is not the best way for me, due :
- you has to attach it to each one
- All them should be dynamic entities, because static does nothing and return nothing, in fact there are values than you can't get from a static entity, I know because I'm working now with some relate project.
this is my favorite, when you index an array with "e" you will no longer be able to use this information in a loop such as:

for i = 1, # entID, 1
--your code here
end

Of course there are other ways to obtain this data, I think you have already seen an example related to this, it is more I think that it seemed to you "a good job", although for your question, I do not know if you have understood the concept.

The case is that if I want to know how many apples each one has, you have to wait for the report that each one sends to you, something like:
Hey i have 5 apples
uh, and i have 3.

You only have to add ,and you will know how many apples there are in total.

When you use the restriction:

for i = 1, # entID, 1
if g_Entity [i] ~ = nil then
end
end

you are asking just about dynamic entities, nor about every entities.

Globals can be done and use for every scripts around the map.
Local var declare at the very top of the script, can be use just by this script, not one other can acceded to this var.
Local var declare inside a function can be use just for that function, and so on.
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

PM
GubbyBlips
5
Years of Service
User Offline
Joined: 14th Jan 2019
Location:
Posted: 2nd Apr 2020 16:42 Edited at: 6th Apr 2020 00:12
Thanks for your help @3com. I am still learning, and that would
be at the speed of a snail unfortunately. I appreciate your time.

@smallg "speed = {}" in both
I didn't know you could do both, but it's in the first (only)

BTW, I have accomplished the task of my intent, but not in the manner I
had hoped for. My hope was smaller, easier to read and handle modular
scripts for separate items. (Allegorically speaking)
PM
3com
9
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 3rd Apr 2020 08:08
But if the script goes into a loop, then the mother is going to have to hold the child for a long time. (programmatically speaking). LOL

Although it is sometimes necessary, it is not best practice to have a script expecting a value from another script.

Hopotetic situation:

The mother will make dinner for everyone, so she has to call all the children to confirm the date.

The mother waits for a date, the children each propose a different date and the mother must decide.

The mother stores all these dates in an array / table and randomly draws one.

date = math.random (1, # dates)
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

PM

Login to post a reply

Server time is: 2024-04-24 13:06:46
Your offset time is: 2024-04-24 13:06:46