Difference between revisions of "Introduction to Pluggy Arrays"
Finished
imported>Speedo m |
imported>Speedo (Finished) |
||
Line 1: | Line 1: | ||
This article provides an introduction to arrays and their use via [[:Category: Pluggy| Pluggy]]. Those who are familiar with arrays may wish to skip ahead to the [[Introduction to Pluggy Arrays#Overview for Programmers|Overview for Programmers]]. | This article provides an introduction to arrays and their use via [[:Category: Pluggy| Pluggy]]. Those who are familiar with arrays may wish to skip ahead to the [[Introduction to Pluggy Arrays#Overview for Programmers|Overview for Programmers]]. | ||
Line 8: | Line 6: | ||
An example array containing 6 integer elements: | An example array containing 6 integer elements: | ||
{| border="2" cellpadding="5" cellspacing="0" | {| border="2" cellpadding="5" cellspacing="0" | ||
|- | |- align="center" | ||
!style="background:#ffdead;"|Index | !style="background:#ffdead;"|Index | ||
!style="background:#ffeded;"|Value | !style="background:#ffeded;"|Value | ||
|- | |- align="center" | ||
!style="background:#ffdead;"|0 | !style="background:#ffdead;"|0 | ||
|style="background:#ffeded;"|1 | |style="background:#ffeded;"|1 | ||
|- | |- align="center" | ||
!style="background:#ffdead;"|1 | !style="background:#ffdead;"|1 | ||
|style="background:#ffeded;"|5 | |style="background:#ffeded;"|5 | ||
|- | |- align="center" | ||
!style="background:#ffdead;"|2 | !style="background:#ffdead;"|2 | ||
|style="background:#ffeded;"|16 | |style="background:#ffeded;"|16 | ||
|- | |- align="center" | ||
!style="background:#ffdead;"|3 | !style="background:#ffdead;"|3 | ||
|style="background:#ffeded;"|9 | |style="background:#ffeded;"|9 | ||
|- | |- align="center" | ||
!style="background:#ffdead;"|4 | !style="background:#ffdead;"|4 | ||
|style="background:#ffeded;"|31 | |style="background:#ffeded;"|31 | ||
|- | |- align="center" | ||
!style="background:#ffdead;"|5 | !style="background:#ffdead;"|5 | ||
|style="background:#ffeded;"|28 | |style="background:#ffeded;"|28 | ||
Line 92: | Line 90: | ||
==When to Use Arrays== | ==When to Use Arrays== | ||
*When you need to store a large amount of information, needing a large number of variables. Arrays combined with OBSE's [[Label|looping functions]] can allow you to address hundreds of variables with just a few lines of code, versus the hundreds of lines of code (and hundreds of variable declarations) that would be required otherwise. | |||
*When you need a data storage solution that can grow or shrink, rather than being static. | |||
*When you'd like to easily store a "set" of data, such as multiple pieces of information about an item, as shown in the [[Introduction to Pluggy Arrays#Data Types|Data Types Example]]. | |||
==Example== | |||
The following example could be used to store information about items in the player's inventory. It uses a pair of arrays to hold the information: | |||
*'''pInventory''', which holds the ObjectID of the item. | |||
*'''pItemCount''', which store the number of items that the player has. | |||
These arrays simply using matching indexes to store information, meaning that if you find an item at index 5 in '''pInventory''', you know that you can look at index 5 of '''pItemCount''' to find out how many of those items the player is carrying. | |||
<pre> | |||
long pInventory | |||
long pItemCount | |||
long index | |||
ref tItem | |||
long tCount | |||
... | |||
set pInventory to CreateArray -1 0 1 | |||
set pItemCount to CreateArray -1 0 1 | |||
set index to 0 | |||
label | |||
set tItem to player.GetInventoryObject index | |||
set tCount to player.GetItemCount tItem | |||
SetRefInArray pInventory index tItem 1 | |||
SetInArray pItemCount index tCount 1 | |||
set index to index + 1 | |||
if (index < player.GetNumItems) | |||
goto | |||
endif | |||
;code that uses the stored information | |||
DestroyArray pInventory | |||
DestroyArray pItemCount | |||
</pre> | |||
==Overview for Programmers== | ==Overview for Programmers== |