Author Topic: What are arrays, and why should we use them?  (Read 14095 times)

0 Members and 1 Guest are viewing this topic.

Offline erwin

  • Sr. Member
  • ****
  • Posts: 314
    • View Profile
What are arrays, and why should we use them?
« on: October 24, 2015, 02:40:34 am »
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?

Code: [Select]
* 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?

Code: [Select]
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.
Code: [Select]
*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:
Code: [Select]
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
Code: [Select]
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
Code: [Select]
set correctobjects 10 11 12 12345
set numobjects 4
compared to
Code: [Select]
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
Code: [Select]
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#msg6927

It's just easier to edit the first part - everything else follows automatically from the code. For example:
Code: [Select]
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
Code: [Select]
set questitem1 123 153 167 14678 21563
set questitem2 642 7434 2359 4928 9872
set questitem3 6982 7939 1765 53

and it's fixed!
« Last Edit: October 24, 2015, 02:54:01 am by erwin »

Offline rynald

  • Administrator
  • Newbie
  • *****
  • Posts: 25
    • View Profile
Re: What are arrays, and why should we use them?
« Reply #1 on: October 25, 2015, 05:28:10 pm »
After the next update you can do

Code: [Select]
eval s some words
insert_word just 1 s
%echo% %s%
replace_word three 2 s
%echo% %s%
%echo% %s.word_count%
%echo% %s.wordat(3)%

which gives:

just some words
just three words
3
words

(wordat already exists)

Offline erwin

  • Sr. Member
  • ****
  • Posts: 314
    • View Profile
Re: What are arrays, and why should we use them?
« Reply #2 on: October 25, 2015, 10:39:28 pm »
rynald you are the best :)

Offline erwin

  • Sr. Member
  • ****
  • Posts: 314
    • View Profile
Re: What are arrays, and why should we use them?
« Reply #3 on: October 25, 2015, 10:41:59 pm »
on second thought, is there a nice page / link / helpfile / command to show all such DG functions / fields?

Offline rynald

  • Administrator
  • Newbie
  • *****
  • Posts: 25
    • View Profile
Re: What are arrays, and why should we use them?
« Reply #4 on: October 26, 2015, 02:36:05 pm »
There aren't many commands that aren't mentioned in the help files or in the reference at http://old.tbamud.com/Oasis_DG_pages/. Having our own wiki would be nice though.

Offline erwin

  • Sr. Member
  • ****
  • Posts: 314
    • View Profile
Re: What are arrays, and why should we use them?
« Reply #5 on: October 27, 2015, 11:44:43 am »
Yeah, it would be nice to have our own wiki.

I like the reference as a basis reference, but there are some 4D unique functions (dg_letter, mod, etc) which aren't there, nor 4D specific fields (helper, hero, etc).