Scripts / Variable inside Variable?

Author
Message
Teabone
Forum Support
18
Years of Service
User Offline
Joined: 8th Jun 2006
Location: Earth
Posted: 11th Dec 2019 01:50 Edited at: 11th Dec 2019 02:51
Hi all,

I'm trying to optimize my code more similar to how i could in other programming languages. Is it possible to have variables be part of other variables?

For instance:

itemID = 1

if item(itemID) + 10 == 100 then
item(itemID) = item(itemID) + 20
end

Is something like that possible or do I have to :

if item1 + 10 == 100 then
item1 = item1 + 20
end

if item2 + 10 == 100 then
item2 = item2 + 20
end

I'm assuming I'll have to use tables for this? Or perhaps its not possible?
Store Assets - Store Link

i7 -2600 CPU @ 3.40GHz - Windows 7 - 8GB RAM - Nivida GeForce GTX 960
smallg
Community Leader
19
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 11th Dec 2019 10:27
Circle brackets () are functions in lua, you need to use square brackets [], iow tables/arrays but yes you can use variables as the indexes/pointers.
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: 11th Dec 2019 20:39 Edited at: 11th Dec 2019 20:41
Lists are key/value pairings.

{} - empty list
dlist = { day1 = 'Sunday', day2 = 'Monday' … etc … } - list of days keyed by day1, day2 etc
Prompt( dlist[ 'day1' ] ) - would give you "Sunday"

slist = { 'Sunday', 'Monday' ... etc … } - list without specified key, Lua automatically uses key vales 1, 2 etc
Prompt( slist[ 2 ] ) - would give you "Monday"

Instead of the square brackets you can use . to specify the key so:
Prompt( dlist.day2 ) - would give you "Monday" (I find this form more readable)
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: 11th Dec 2019 20:50
Here is a more useful example:
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: 11th Dec 2019 22:56
What other languages are you familiar with btw?

Lua is a bit special in that it can mimic other languages quite well, for example you can use lists to mimic C structs or Ada records, you can also use them to mimic objects with all the oops bells and whistles if that takes your fancy.

I strongly advise getting hold of "Programming in Lua, fourth edition" and giving it a good read if you haven't already.
Been there, done that, got all the T-Shirts!
PM

Login to post a reply

Server time is: 2024-12-22 09:28:08
Your offset time is: 2024-12-22 09:28:08