how do i use variables in arrays - please advise a beginner
privateer
30 Aug 2013, 18:35Using arrays
I’ve searched the forum for this, but don’t seem to quite be able to find the answer. I’m new to using variables in quest, but have the VERY basics, i think.
X = 1 (for a local variable)
Object.x = 1 (for a global variable)
So far so good!?
Now, what i’d like to be able to do is use variables in arrays. In my old BASIC days it used to be x(1) = 5. And then I seem to remember you could have arrays with multiple dimensions like X(1,1,1) =5
Looking at some answers in the forum it LOOKED as though it would be simple and along the same lines. I’ve seen examples given that looked familiar: object.x[1]
But that doesn’t work for me. I get an error message ‘no template named ‘1’’. And I can’t find where I might name a template. Nor do I really know what a template is, although I’m guessing it is something like the equivalent of ‘Dimensionalising’ variables in BASIC: Dim x(10) which defined the parameters of the array – in this case saying there will be 10 values from x(1) to x(10)?)
I’d be very grateful for any help, especially if it’s given in VERY user-friendly language! Also, I’m more comfortable using the GUI rather than going directly into code – if that’s possible for this. I am, as they say, a n00b.
Many thanks!
I’ve searched the forum for this, but don’t seem to quite be able to find the answer. I’m new to using variables in quest, but have the VERY basics, i think.
X = 1 (for a local variable)
Object.x = 1 (for a global variable)
So far so good!?

Now, what i’d like to be able to do is use variables in arrays. In my old BASIC days it used to be x(1) = 5. And then I seem to remember you could have arrays with multiple dimensions like X(1,1,1) =5
Looking at some answers in the forum it LOOKED as though it would be simple and along the same lines. I’ve seen examples given that looked familiar: object.x[1]
But that doesn’t work for me. I get an error message ‘no template named ‘1’’. And I can’t find where I might name a template. Nor do I really know what a template is, although I’m guessing it is something like the equivalent of ‘Dimensionalising’ variables in BASIC: Dim x(10) which defined the parameters of the array – in this case saying there will be 10 values from x(1) to x(10)?)
I’d be very grateful for any help, especially if it’s given in VERY user-friendly language! Also, I’m more comfortable using the GUI rather than going directly into code – if that’s possible for this. I am, as they say, a n00b.
Many thanks!

jaynabonne
30 Aug 2013, 23:12First, be sure you update to Quest 5.4.1, as I think the template issue when using [] got fixed. (Though this might be another case.)
Second, Quest doesn't support arrays as such. There are some "hacky" solutions, if you really need them, but the one to use depends on your needs.
Quest only supports lists, dictionaries and objects as compound structures. Let's take each one.
1) Lists are indexed by integers, so they're similar to arrays when reading. If you have a list named "arr" with 5 entries, then you can use either ListItem(arr, 2) or arr[2] (if you have 5.4.1) to access the third element (lists are 0-based). But lists can only be built sequentially. You don't have random setting - there's no way to change the value of the third element once it's been set. So if you have a read-only array that you set up once sequentially, then you can use a list and access the items via index.
To create: NewList()
To delete: <automatically cleaned up when no longer referenced>
Index type: int
To set: ListAdd. List items must be added sequentially or in static lists.
To get: ListItem or []
2) Dictionaries have the advantage that you can read and assign to them randomly (assuming you delete the old value first), but the indices are strings only. That's not too bad as you can always convert an int to a string via ToString. Also, since the string can be anything as long as its unique, you can also do multidimensional via creative index construction. For example, if you have a dictionary called "arr", you can use DictionaryItem(arr, "1,1,1") or arr["1,1,1"] (if you have Quest 5.4.1) to access that element. You need to build the strings from numbers, but you could easily wrap that up in functions to hide the int-to-string index conversion. I did that in that silly little Breakout demo I wrote long ago.
To create: NewDictionary()
To delete: <automatically cleaned up when no longer referenced>
Index type: string
To set: DictionaryAdd (possibly after DIctionaryRemove of old value).
To get: DictionaryItem or []
3) Objects are like dictionaries in that you can assign to them randomly and in that the indices are strings. Objects have the advantage that you don't have to remove an existing value before you set a new one. The disadvantages are that objects are trickier to dynamically allocate since they must be uniquely named, they must be explicitly freed, and you can't use the [] notation - you have to use get and set.
To create: create (possibly with GetUniqueElementName)
To delete: destroy
Index type: string
To set: set
To get: get
I should probably put together a simple array library.
You wouldn't be able to use the [] notation, but it could hide the index conversions inside.
I don't know if that helps, but that's my take on it.
Second, Quest doesn't support arrays as such. There are some "hacky" solutions, if you really need them, but the one to use depends on your needs.
Quest only supports lists, dictionaries and objects as compound structures. Let's take each one.
1) Lists are indexed by integers, so they're similar to arrays when reading. If you have a list named "arr" with 5 entries, then you can use either ListItem(arr, 2) or arr[2] (if you have 5.4.1) to access the third element (lists are 0-based). But lists can only be built sequentially. You don't have random setting - there's no way to change the value of the third element once it's been set. So if you have a read-only array that you set up once sequentially, then you can use a list and access the items via index.
To create: NewList()
To delete: <automatically cleaned up when no longer referenced>
Index type: int
To set: ListAdd. List items must be added sequentially or in static lists.
To get: ListItem or []
2) Dictionaries have the advantage that you can read and assign to them randomly (assuming you delete the old value first), but the indices are strings only. That's not too bad as you can always convert an int to a string via ToString. Also, since the string can be anything as long as its unique, you can also do multidimensional via creative index construction. For example, if you have a dictionary called "arr", you can use DictionaryItem(arr, "1,1,1") or arr["1,1,1"] (if you have Quest 5.4.1) to access that element. You need to build the strings from numbers, but you could easily wrap that up in functions to hide the int-to-string index conversion. I did that in that silly little Breakout demo I wrote long ago.
To create: NewDictionary()
To delete: <automatically cleaned up when no longer referenced>
Index type: string
To set: DictionaryAdd (possibly after DIctionaryRemove of old value).
To get: DictionaryItem or []
3) Objects are like dictionaries in that you can assign to them randomly and in that the indices are strings. Objects have the advantage that you don't have to remove an existing value before you set a new one. The disadvantages are that objects are trickier to dynamically allocate since they must be uniquely named, they must be explicitly freed, and you can't use the [] notation - you have to use get and set.
To create: create (possibly with GetUniqueElementName)
To delete: destroy
Index type: string
To set: set
To get: get
I should probably put together a simple array library.

I don't know if that helps, but that's my take on it.
privateer
31 Aug 2013, 06:56Thanks Jaynabonne!
I will have to work slowly through what you've told me as I haven't played with lists, dictionaries, or object values yet (it's amazing what you can do with a ridiculously complex flag plexus!
). At least I know I was barking up the wrong tree by trying to find a way of making variables multi-dimensional.
Very useful stuff, cheers!
I will have to work slowly through what you've told me as I haven't played with lists, dictionaries, or object values yet (it's amazing what you can do with a ridiculously complex flag plexus!

Very useful stuff, cheers!