Molly asked!
The easiest way to think of arrays (in a non coding context) is to think of sentences. For example, most variables are set to "one word / one number / one character" values.
PS. I'm not sure if they exist, but are there dg "replace" code which replaces items / characters in an array/string, as well as dg_code to find the length of the array? If yes, could any coders give the syntax, much appreciated! If not, would it be possible to code it in? Thanks!
PPS. Hashsets in DG?
* Disclaimer: These are not real quest variables, but how things could be
robinhooddagger: done
killedgreenie: 1
highscore: 12345
gotvoucher1: yes
gotvoucher2: yes
gotvoucher3: yes
gotvoucher4: yes
gotvoucher5: yes
But variables with "one word / one number / one character" entries give too much clutter - eg type vstat player virisin. One solution would be to store arrays as variables, and
make the array as intuitive sounding as possible.For example, here are some variables as arrays. Which one do you think is more intuitive?
helpervoucher: to be or not to be
helpervoucher: 1 0 0 1 1
helpervoucher: yes yes no no yes
helpervoucher: found found notfound found notfound
So here are two ways how to use arrays, for simple quests.
One way is to store items found on one variable. Eg, you might have a quest that wanted you to find five items.
*Do something to trigger the quest
set myfirstquest notfound notfound notfound notfound notfound incomplete
remote myfirstquest %actor.id%
* This gives a variable that looks like this
myfirstquest: notfound notfound notfound notfound notfound incomplete
Now, we simply change the appropriate "word" in the variable. If the player finds the third item, then we just change the third notfound to found. If the player has completed this quest at least once, then we change the incomplete to complete.
A second way that arrays can be used is to store things on a trigger. For example, you might have a quest that wants you to give the mob some grapes. But there are lots of grapes. One possible trigger could be:
if (%object.vnum == 10) || (%object.vnum == 11) || (%object.vnum% == 12)
do something
endif
However, this becomes clunky if there are lots of possible objects. Furthermore, there's a max number of characters per line. Here's a cleaner way
set correctobjects 10 11 12
* Technically, don't need numobjects unless you want to be fancy
* Could always set numobjects = 3 manually below
set numobjects 3
set j 0
while %j% < %numobjects%
eval j %j% + 1
* This extracts the jth number of correctobjects.
* If j is 1, it will get the number 10. If j = 2, it will get the number 11. If j = 3, it will get 12
extract objecttocheck %j% %correctobjects%
if %objecttocheck% == %object.vnum%
%echo% Correct object, hooray
set gotcorrect 1
endif
done
if !%gotcorrect%
%echo% We didn't get a correct object.
endif
You might say: "Hey, this actually results in a more lines." Yes, you're right, it does. But there are three advantages to this.
First advantage - You only need to edit the variables numobjects and correctobjects. For example, if you added a new vnum 12345 you just need
set correctobjects 10 11 12 12345
set numobjects 4
compared to
if (%object.vnum == 10) || (%object.vnum == 11) || (%object.vnum% == 12) || (%object.vnum% == 12345)
There's fewer chances of mistakes, even though it's just changing one line. Furthermore, some clients have terrible wrapping, and you might get
if (%object.vnum == 10) || (%object.vnum == 11) || (%object.vnum%
== 12) || (%object.vnum% == 12345)
which screws up the code.
Second advantage: There's only a maximum number of characters per line. It's harder to go over the limit using arrays.
Third advantage: If the quest wasn't just to give grapes, but to give grapes, nuts, clothes, gems, etc, then you'd be repeating a lot of code. Take a look at
http://4dimensions.org/forum/index.php/topic,682.msg6927.html#msg6927It's just easier to edit the first part - everything else follows automatically from the code. For example:
Note Number: 9
From: Tor
Subject: Quest doesn't work
Posted: Tue Oct 21 03:23:42 2015
To: imm
=================================================
The questmob wanted a list of items.
1. An emerald
2. A ruby
3. A garnet
4. An onyx
However, I gave the mob some items, such as
emerald from Thandar, Arabian Desert, Flying Fortress
ruby from Dragon Island ...
garnet from ....
onyx from ...
If you've received this note, and the builder didn't use arrays you could edit the trigger, which would take extremely long - because you want to search through the script to find where the if statements occur, find the syntax the builder used, and add them in.
Or, if the builder just used an array like the trigger of the day, all you need to do is to just
set questitem1 123 153 167 14678 21563
set questitem2 642 7434 2359 4928 9872
set questitem3 6982 7939 1765 53
and it's fixed!