HK Edit: my code for the 'for' Function examples has been fixed to being correct~functional code now, sorry, for not fixing it up sooner, but I was really tired after doing this, as it was a lot of work (4+ hrs), and I needed a rest... though I kept posting... lol, but I got my rest now, and the code 'for' Function examples are now fixed up. My 'foreach' examples, should also work now too... but I haven't actually tested them.
------------------------
HK's Extensive Guide for (Using) Lists and Dictionaries:
General Links:
http://docs.textadventures.co.uk/quest/http://docs.textadventures.co.uk/quest/ ... notes.html (and also click on the individual blog links for more detailed and extra information)
http://docs.textadventures.co.uk/quest/tutorial/http://docs.textadventures.co.uk/quest/guides/http://docs.textadventures.co.uk/quest/aslx.htmlhttp://docs.textadventures.co.uk/quest/elements/http://docs.textadventures.co.uk/quest/types/http://docs.textadventures.co.uk/quest/functions/ (category order)
http://docs.textadventures.co.uk/quest/ ... tions.html (alphabetical order)
http://docs.textadventures.co.uk/quest/scripts/viewforum.php?f=18 (more guides: Library and Code Samples)
Lists (Stringlists and Objectlists) Links:
http://docs.textadventures.co.uk/quest/ ... lists.htmlhttp://docs.textadventures.co.uk/quest/ ... glist.htmlhttp://docs.textadventures.co.uk/quest/ ... tlist.htmlhttp://docs.textadventures.co.uk/quest/ ... glist.htmlhttp://docs.textadventures.co.uk/quest/ ... tlist.htmlhttp://docs.textadventures.co.uk/quest/ ... titem.htmlhttp://docs.textadventures.co.uk/quest/ ... titem.htmlhttp://docs.textadventures.co.uk/quest/ ... t_add.htmlhttp://docs.textadventures.co.uk/quest/ ... emove.htmlhttp://docs.textadventures.co.uk/quest/ ... mbine.htmlhttp://docs.textadventures.co.uk/quest/ ... clude.htmlhttp://docs.textadventures.co.uk/quest/ ... tsort.htmlhttp://docs.textadventures.co.uk/quest/ ... nding.htmlhttp://docs.textadventures.co.uk/quest/ ... ylist.htmlhttp://docs.textadventures.co.uk/quest/ ... tains.htmlhttp://docs.textadventures.co.uk/quest/ ... count.htmlhttp://docs.textadventures.co.uk/quest/ ... titem.htmlhttp://docs.textadventures.co.uk/quest/ ... rents.htmlDictionaries (Stringdictionaries, Objectdictionaries, and Scriptdictionaries) Links:
http://docs.textadventures.co.uk/quest/ ... aries.htmlhttp://docs.textadventures.co.uk/quest/ ... onary.htmlhttp://docs.textadventures.co.uk/quest/ ... onary.htmlhttp://docs.textadventures.co.uk/quest/ ... onary.htmlhttp://docs.textadventures.co.uk/quest/ ... onary.htmlhttp://docs.textadventures.co.uk/quest/ ... onary.htmlhttp://docs.textadventures.co.uk/quest/ ... onary.htmlhttp://docs.textadventures.co.uk/quest/ ... yitem.htmlhttp://docs.textadventures.co.uk/quest/ ... yitem.htmlhttp://docs.textadventures.co.uk/quest/ ... yitem.htmlhttp://docs.textadventures.co.uk/quest/ ... yitem.htmlhttp://docs.textadventures.co.uk/quest/ ... tains.htmlhttp://docs.textadventures.co.uk/quest/ ... count.htmlhttp://docs.textadventures.co.uk/quest/ ... tsort.htmlhttp://docs.textadventures.co.uk/quest/ ... nding.htmlhttp://docs.textadventures.co.uk/quest/ ... y_add.htmlhttp://docs.textadventures.co.uk/quest/ ... emove.htmlOther Useful Functions~Scripts for Usage with Lists and Dictionaries:
(There's also the ~ 'Format Objectlists' and etc Functions too, but I didn't include them as they're a bit more specific~advanced~specialized usage)
http://docs.textadventures.co.uk/quest/ ... /join.htmlhttp://docs.textadventures.co.uk/quest/ ... split.htmlhttp://docs.textadventures.co.uk/quest/ ... reach.htmlhttp://docs.textadventures.co.uk/quest/scripts/for.htmlhttp://docs.textadventures.co.uk/quest/scopes.htmlhttp://docs.textadventures.co.uk/quest/ ... y/got.html (a shortcut to checking for Object in, with using, the ScopeInventory ~ Player Inventory)
http://docs.textadventures.co.uk/quest/ ... jects.htmlhttp://docs.textadventures.co.uk/quest/ ... mands.htmlhttp://docs.textadventures.co.uk/quest/ ... exits.htmlhttp://docs.textadventures.co.uk/quest/ ... ripts.htmlhttp://docs.textadventures.co.uk/quest/ ... jects.htmlhttp://docs.textadventures.co.uk/quest/ ... ldren.htmlhttp://docs.textadventures.co.uk/quest/ ... verbs.html (special built-in Stringlist)
http://docs.textadventures.co.uk/quest/ ... verbs.html (special built-in Stringlist)
http://docs.textadventures.co.uk/quest/ ... butes.html (special built-in Stringdictionary)
Randomization Links:
http://docs.textadventures.co.uk/quest/ ... eroll.htmlhttp://docs.textadventures.co.uk/quest/ ... omint.htmlhttp://docs.textadventures.co.uk/quest/ ... ouble.htmlhttp://docs.textadventures.co.uk/quest/ ... hance.html-----------
my general syntax:
Attribute: Object_name.Attribute_name = Value_or_Expression
Variable: Variable_string_name = Value_or_Expression
quest terminology:
VARIABLE:
-> Attribute
-> Variable
-> Parameter (Functions and Commands use them)
Stringlists:
Attribute: Object_name.Stringlist_Attribute_name = split ("string1;string2;string3;etc4", ";")
Variable: Stringlist_Variable_string_name = split ("string1;string2;string3;etc4", ";")
Attribute example: game.food_stringlist = split ("apple;lemon;orange", ";")
Variable example: food_stringlist = split ("apple;lemon;orange", ";")
Objectlists:
Attribute: Object_name.Objectlist_Attribute_name = split ("object1;object2;object3;etc4", ";")
Variable: Objectlist_Variable_string_name = split ("split ("object1;object2;object3;etc4", ";")
Attribute example: game.food_objectlist = split ("apple;lemon;orange", ";")
Variable example: food_objectlist = split ("apple;lemon;orange", ";")
----------
Using Lists and Dictionaries (these are similiar to 'Arrays' in other code languages, I think):
Important Note:
Objects in (put in) Objectlists, have absolutely no effect on an Object's actual location and~or any other bearing at all. Objectlists' items (Objects) are completely independant and is uneffecting of everything else. It took me awhile to understand this concept. An Object's actual existance+location is its Parent (another Object, or the entire game itself as its Parent). For Example:
<object name="object1">
<object name="object2">
</object>
</object>
<object name="object3">
<attr name="objectlistx" type="objectlist">object2</attr>
</object>
'object2' exists+location is inside (a Child of its Parent) 'object1'
the 'object2' item in the 'objectlistx' Objectlist Attribute is just that, an item (as it's NOT the actual 'object2' Object), which is inside~of 'object3', has absolutely no bearing on anything at all. We can use the Objectlist do to do things, that's its purpose, but outside of that doing (actions~scriptings), it has no bearing at all on anything. For example, we can use the objectlist to actually move the 'object2' from 'object1' to 'object3', but until we actually do the 'move' scripting, 'object2' exists and is located inside of 'object1', NOT 'object3'.
About Lists:
Note: my use of 'item' is just my own general term of~for ambigiously referencing 'text~Strings' for Stringlists or 'Objects' for Objectlists
they are just a collection of text~strings (Stringlists) or Objects (Objectlists), separated as individual choices~selections (see section above too):
http://docs.textadventures.co.uk/quest/ ... split.htmlVARIABLE = split ("item1 SEPARATOR_SYMBOL item2 SEPARATOR_SYMBOL item3 SEPARATOR_SYMBOL etc4", "SEPARATOR_SYMBOL")
the generally prefered separator symbol is usually a semicolon:
VARIABLE = split ("item1;item2;item3;etc4", ";")
the point of a List, is that we can then use it, to do a ton of really cool and useful things.
but first, you got to understand more on how Lists work:
VARIABLE = split ("item1;item2;item3;etc4", ";")
game.primary_pigment_colors_stringlist = split ("red;blue;yellow", ";")
0. red (1st item)
1. blue (2nd item)
2. yellow (3rd item)
we have 3 items: red (1st item), blue (2nd item), and yellow (3rd item):
ListCount (game.primary_pigment_colors_stringlist) ====> 3 (quantity of items in the List)
HOWEVER, to actually reference~use~get that item, is via its 'index' number (think of an actual book library), which starts at 0, not 1 (ya, it is very confusing especially when you're using 'ListCount' + 'ListItem~StringListItem~ObjectListItem', grr):
red's index # is 0
blue's index # is 1
yellow's index # is 2
so, for example:
VARIABLE = StringListItem (List, index number)
StringListItem (game.primary_pigment_colors_stringlist, 0) ===> red
StringListItem (game.primary_pigment_colors_stringlist, 1) ===> blue
StringListItem (game.primary_pigment_colors_stringlist, 2) ===> yellow
StringListItem (game.primary_pigment_colors_stringlist, 3) ===> ERROR !!!! there is no 4th item, so can't compute! ERROR !!!
now, you can use variables for your 'index number, too, to do the more fancy things:
get input {
-> // you type in a number
-> // quest automatically sets the variable: result = your_typed_in_input
-> VARIABLE = StringListItem (List, result)
}
and also with using Randomization:
GetRandomInt (min integer number value, max inteer number value)
for a Static+Known quantity of items in your List:
game.primary_pigment_colors_stringlist = split ("red;blue;yellow", ";")
game.primary_pigment_colors_stringlist = StringListItem (game.primary_pigment_colors_stringlist, GetRandomInt (0, 2) )
// 'GetRandomInt' will randomly select '0', '1', or '2'
// game.primary_pigment_colors_stringlist = red (0)
// OR
// game.primary_pigment_colors_stringlist = blue (1)
// OR
// game.primary_pigment_colors_stringlist = yellow (2)
for a Dynamic+UNknown quantity of items in your List:
'ListCount' will add~sum up the quantity of items in your list each time, so this works great when you're constantly adding~removing items from your list over the duration of the game, hence the list's dynamic~unknown quantity of items in the list, hence the 'magic' of using 'ListCount', hehe
however, this is where the confusion comes in between 'quantity' and 'index number', as we got to do:
ListCount (List) - 1 ====> index number
we're finding the highest item quantity:
if there's 3 items in a list, then the max items in a list is 3 too, and thus the highest (last) item itself (via its index number) in the list is: 2 (3-1=2)
as remember the above example:
0. red (1st item)
1. blue (2nd item)
2. yellow (3rd item)
Index # vs Item Quantity:
0 vs 1
1 vs 2
2 vs 3
game.primary_pigment_colors_stringlist = split ("red;blue;yellow", ";")
game.primary_pigment_colors_stringlist = StringListItem (game.primary_pigment_colors_stringlist, GetRandomInt (0, ListCount (game.primary_pigment_colors_stringlist) - 1 ) )
// 'GetRandomInt' will randomly select '0', '1', or '2'
// game.primary_pigment_colors_stringlist = red (0)
// OR
// game.primary_pigment_colors_stringlist = blue (1)
// OR
// game.primary_pigment_colors_stringlist = yellow (2)
-----------------
About Dictionaries:
they're essentially (think of them as) 'conversions' or 'inputs~outputs'
(and most~all of the stuff about lists, applies to Dictionaries too, in terms of the referencing)
examples:
<attr name="day_stringdictionary" type="simplestringdictionary">1=sunday;2=monday;3=tuesday;4=wednesday;5=thursday;6=friday;7=saturday;sunday=1;monday=2;tuesday=3;wednesday=4;thursday=5;friday=6;saturday=7</attr>
<attr name="month_to_season_stringdictionary" type="simplestringdictionary">january=winter;february=winter;march=spring;april=spring;may=spring;june=summer;july=summer;august=summer;september=autumn;october=autumn;november=autumn;december=winter</attr>
<attr name="opposing_magic_elementals_stringdictionary" type="simplestringdictionary">fire=water;water=fire;air=earth;earth=air</attr>
examples in~of application:
game.day_integer_stringlist = split ("1;2;3;4;5;6;7", ";")
show menu ("What day is it?", game.day_integer_stringlist, false) {
game.day_string = StringdictionaryItem (game.day_stringdictionary, result)
}
game.day_stringlist = split ("sunday;monday;tuesday;wednesday;thursday;friday;saturday", ";")
show menu ("What day is it?", game.day_stringlist, false) {
game.day_string = StringdictionaryItem (game.day_stringdictionary, result)
}
// let's see if I can remember how to do this, lol ~ its been a while ... Credit goes to Pixie's Spell Library for this ingenius usage of Dictionaries:
<attr name="opposing_magic_elementals_stringdictionary" type="simplestringdictionary">fire=water;water=fire;air=earth;earth=air</attr>
ice_dragon.elemental = "water"
fireball_spell.elemental = "fire"
'Cast' Verb on your 'spell' Objects (specific, non-global, non-dynamic, example):
spell = GetObject (fireball_spell)
enemy = GetObject (ice_dragon)
if (spell.elemental = StringDictionaryItem (opposing_magic_elementals_stringdictionary, enemy.elemental) ) {
enemy.life = enemy.life - (spell.damage * 2)
msg ("You do double the damage to the enemy, using the correct~opposing spell for its elemental vulnerability~weakness!")
} else if (spell.elemental = enemy.elemental) {
enemy.life = enemy.life + spell.damage
msg ("Doh!, You used the same elemental spell as the enemy's elemental, healing it instead of damaging it! You idiot!")
} else {
enemy.life = enemy.life - spell.damage
msg ("Your elemental spell, being neither the same nor opposite of the enemy's elemental, thus does its normal damage to the enemy.")
}
------------
anyways, conceptually:
StringDictionaries:
the 'conversion' or 'input-output', only goes 1 way: left to right, so that's (if you need) why you need:
(I'm not sure if this also applies to Objectdictionaries and Scriptdictionaries, or not)
String1A (input) ---> String1B (output)
~OR~
String1A ---> (conversion) ---> String1B
example: fire=water
~ (if need) AND ~
String1B (input) ---> String1A (output)
~OR~
String1B ---> (conversion) ---> String1A
example: water=fire
ObjectDictionaries:
String1 (input) ---> Object1 (output)
~OR~
String1 ---> (conversion) ---> Object1
ScriptDictionaries:
String1 (input) ---> Script1 (output)
~OR~
String1 ---> (conversion) ---> Script1
-------------
lastly, note that there's the 'List~DictionaryContains' Function too, you can check if a List~Dictionary has a specific item in it. And all of the other Functions~Scripts that I give as the links above (at the top of my post).
'DisplayList' is useful too:
DisplayList (List, true_or_false)
colors = split ("red;blue;yellow", ";")
DisplayList (colors, true)
outputs:
1. red
2. blue
3. yellow
not sure what false does, haven't tried it yet, lol... a guess ONLY:
colors = split ("red;blue;yellow", ";")
DisplayList (colors, false)
outputs:
red
blue
yellow
and there's the 'Sort' Functions too. and etc... whatever else I'm missing...
the 'Scopes', 'Gets', and 'Alls' are very useful too...
-----------------
oops, I forgot...
to make a new List~Dictionary (using a Stringlist for example):
VARIABLE = NewStringList ()
game.colors = NewStringList ()
list add (game.colors, "red")
list add (game.colors, "blue")
list add (game.colors, "yellow")
and etc... again see all of my links at the top of this post, and ask if you got any questions, and~or want to know what applications there are with using Lists~Dictionaries~the various links (at the top of the post), hope this helps anyone wanting to learn to using lists~dictionaries....
----------
ach... I forgot the 'Iteration' stuff (I'm tired now, so this will be brief, ask and I'll explain~help in more detail):
http://docs.textadventures.co.uk/quest/ ... lists.html (this shows better how to use 'for' properly, than the 'for' link itself below, lol)
http://docs.textadventures.co.uk/quest/ ... reach.htmlhttp://docs.textadventures.co.uk/quest/scripts/for.html'foreach' and 'for'
'foreach' is for iterating through EVERY~ALL~EACH (for*EACH*) item in your list, applying the scripting to each of those items. This is generally used most often by people. In 'foreach' you reference the List directly in its 'foreach' syntax line, and (depending) also in your scripting
'for' is for specific~targetted iteration of items in your list, applying the scripting to each of those items. This is generally not used as often by people, as it is (probably~assumingly) used for advanced RPG equipment+item system usages. You do NOT reference the List within the 'for' syntax line itself, instead you reference the List in the scriptings.
foreach (placeholder_variable, List) { scripts }
for (placeholder_variable, integer number min range value, integer number max range value, optionally: skip-count number value) { scripts }
with 'for', you reference your actual List within the scripting itself, not in the 'for' syntax line, this always confuses me (causing me to have to go look up on how to use 'for' over and over again, lol ~ I can never remember this)
examples:
'foreach' Function:
game.team = split ("joe;jim;john", ";")
foreach (team_member, game.team) {
do (team_member,"run_laps_script_attribute")
// output result conceptually:
// joe.runs_laps_script_attribute -> joe runs laps
// jim.runs_laps_script_attribute -> jim runs laps
// john.runs_laps_script_attribute -> john runs laps
}
// and the exact same thing:
game.team = split ("joe;jim;john", ";")
foreach (x, game.team) {
do (x,"run_laps")
// output result conceptually:
// joe.runs_laps -> joe runs laps
// jim.runs_laps -> jim runs laps
// john.runs_laps -> john runs laps
}
// and the exact same thing:
game.team = split ("joe;jim;john", ";")
foreach (item, game.team) {
do (item,"run_laps")
// output result conceptually:
// joe.runs_laps -> joe runs laps
// jim.runs -> jim runs laps
// john.runs -> john runs laps
}
// and the exact same thing:
game.team = split ("joe;jim;john", ";")
foreach (blahblahblah, game.team) {
do (blahblahblah,"run_laps")
// output result conceptually:
// joe.runs_laps -> joe runs laps
// jim.runs -> jim runs laps
// john.runs -> john runs laps
}
------------------------
extra links (used below in my examples, well, just 'invoke', meh):
http://docs.textadventures.co.uk/quest/ ... nvoke.html~OR~
http://docs.textadventures.co.uk/quest/scripts/do.html'for' Function:
// Objectlist:
game.team = split ("joe;jim;john;jeff;jake;job;jab", ";")
for (team_member,0,0) {
do (ObjectListItem(game.team,team_member),"run_laps")
// joe.run_laps
}
for (team_member,6,6) {
do (ObjectListItem(game.team,team_member),"run_laps")
// jab.run_laps
}
for (team_member,2,2) {
do (ObjectListItem(game.team,team_member),"run_laps")
// john.run_laps
}
for (team_member,3,3) {
do (ObjectListItem(game.team,team_member),"run_laps")
// jeff.run_laps
}
for (team_member,0,2) {
do (ObjectListItem(game.team,team_member),"run_laps")
// joe.run_laps
// jim.run_laps
// john.run_laps
}
for (team_member,3,6) {
do (ObjectListItem(game.team,team_member),"run_laps")
// jeff.run_laps
// jake.run_laps
// job.run_laps
// jab.run_laps
}
for (team_member,0,6) {
do (ObjectListItem(game.team,team_member),"run_laps")
// joe.run_laps
// jim.run_laps
// john.run_laps
// jeff.run_laps
// jake.run_laps
// job.run_laps
// jab.run_laps
}
for (team_member,0,6,1) {
do (ObjectListItem(game.team,team_member),"run_laps")
// joe.run_laps
// jim.run_laps
// john.run_laps
// jeff.run_laps
// jake.run_laps
// job.run_laps
// jab.run_laps
}
for (team_member,0,6,2) {
do (ObjectListItem(game.team,team_member),"run_laps")
// joe.run_laps
// john.run_laps
// jake.run_laps
// jab.run_laps
}
for (team_member,0,6,3) {
do (ObjectListItem(game.team,team_member),"run_laps")
// joe.run_laps
// jeff.run_laps
// jab.run_laps
}
for (team_member,0,6,4) {
do (ObjectListItem(game.team,team_member),"run_laps")
// joe.run_laps
// jake.run_laps
}
for (team_member,0,6,5) {
do (ObjectListItem(game.team,team_member),"run_laps")
// joe.run_laps
// job.run_laps
}
for (team_member,2,6,1) {
do (ObjectListItem(game.team,team_member),"run_laps")
// john.run_laps
// jeff.run_laps
// jake.run_laps
// job.run_laps
// jab.run_laps
}
for (team_member,2,6,4) {
do (ObjectListItem(game.team,team_member),"run_laps")
// john.run_laps
// jab.run_laps
}
// I think you get the idea now... lol
-----------
P.S.
you can also take a look here at another application of Lists+Dictionaries:
viewtopic.php?f=18&t=5138 (HK's 'Explore' and 'Travel' Code)
ask if you got any questions!