Scripts / [SOLVED] Creating Variables with Multiple Variables?

Author
Message
Teabone
Forum Support
18
Years of Service
User Offline
Joined: 8th Jun 2006
Location: Earth
Posted: 19th Nov 2018 06:39
Hi everyone, i was curous if this is possible with LUA as its something ive been able to do in other languages. The ability to create a variable using combined variables. For example:

variable = 1

if object(variable) == 1 then

and have "object1" return 1?

Is something like that possible?
Twitter - Teabone3 | Youtube - Teabone3 | Twitch - Teabone3 | TGC Assets - Store Link | Patreon - Teabone3

i7 -2600 CPU @ 3.40GHz - Windows 7 - 8GB RAM - Nivida GeForce GTX 960

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

Go to answer
DVader
20
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 19th Nov 2018 12:52 Edited at: 19th Nov 2018 12:55
Yes it's called an array or (in lua a table). The're very important for certain functionality and you need them for certain things like dealing with multiple objects using the same script for example.

Will initialise a table.

To feed it info.
That's the very basics. Generally you will use e as normal, but it is possible of course like any language to use your own number range as well. I use arrays in most my store scripts as they tend to have to work out the box with multiple uses and it's just not possible with simple variables.
Of course "table" above is for example only you can call it anything.
SPECS: Ryzen 1700 CPU. Nvidia 970GTX. 16 Gig Memory. Win 10.
Bolt Action Gaming
GameGuru Tool Maker
11
Years of Service
User Offline
Joined: 24th Oct 2013
Location: Harrisburg, PA (USA)
Posted: 19th Nov 2018 15:37
You can also create tables of tables, or any data really. It's quite flexible in that respect.
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 19th Nov 2018 22:50 Edited at: 19th Nov 2018 22:52
This post has been marked by the post author as the answer.
Lua only has three data 'types': tables, strings and numbers.

A table can contain other tables (or lists if you prefer), strings, numbers and functions.

A table consists of a key/value pairing, the key can be anything you want and so can the value.

If you do not provide a key then Lua generates them starting at 1 and going up sequentially.

So:

local myTable = { 'a', 'b', 'c', 'd' }

Creates a table with key/value pairings of 1:'a', 2:'b', 3:'c' and 4:'d'.

So myTable[ 3 ] will return 'c'.

local myTable = { a = 1, b = 2, c = 3, d = 4 }

Creates a table with key/value pairings of 'a':1, 'b':2, 'c':3 and 'd':4.

So myTable[ 'c' ] would return 3.

The best way of accessing a table is via the pairs and ipairs functions like this:

for k, v in pairs( myTable ) do
<some code>
end

This iterates over all entries of table myTable returning the key in k and the value in v.

For example:
for k, v in pairs( g_Entity ) do
if v.health > 0 then SetEntityHealth( k, 0 ) end
end

Would 'kill' all entities. (not that you would want to do that but you get the idea)

btw table[ 'a' ] can also be written as table.a, I prefer the latter as it is more readable imo.
Been there, done that, got all the T-Shirts!
PM
Teabone
Forum Support
18
Years of Service
User Offline
Joined: 8th Jun 2006
Location: Earth
Posted: 20th Nov 2018 05:11
Thanks so much guys this is all really helpful. I can start to optimize my code a lot better now.
Twitter - Teabone3 | Youtube - Teabone3 | Twitch - Teabone3 | TGC Assets - Store Link | Patreon - Teabone3

i7 -2600 CPU @ 3.40GHz - Windows 7 - 8GB RAM - Nivida GeForce GTX 960

Login to post a reply

Server time is: 2024-12-22 20:06:40
Your offset time is: 2024-12-22 20:06:40