|
| 1 | +# Stack Script Module for AGS |
| 2 | +by monkey0506 |
| 3 | + |
| 4 | +## Description |
| 5 | + |
| 6 | +The Stack module introduces a vectorized stack type into which you can place any type of data. |
| 7 | +Great if you need to store data of various different types, or all of one type; this module can |
| 8 | +handle all of your data storage needs! |
| 9 | + |
| 10 | +## Foreword: About Stacks |
| 11 | + |
| 12 | +You can think of a "stack" as a special sort of array. Just like an array can hold multiple |
| 13 | +variables, so can a stack. The major differences are the fact that 1) these stacks are *vectorized* |
| 14 | +and 2) they are *generic*. The stacks being *vectorized* means that you don't have to explicitly |
| 15 | +state the size of the stack prior to using it. It will grow/shrink as needed. This is also great as |
| 16 | +it optimizes the memory consumption. The fact that the stacks are *generic* means that they do not |
| 17 | +abide to any single data type. You can "push" any type of data, from basic `int`s and `float`s to |
| 18 | +`GUI`s and even other `Stack`s! You *do* have to convert your data when pushing it onto or popping |
| 19 | +it off of the stack, but the conversion is simple. Below is the function list, after that there are |
| 20 | +examples to put everything into perspective for you! |
| 21 | + |
| 22 | +## Dependencies |
| 23 | + |
| 24 | +AGS v3.1.2+ |
| 25 | + |
| 26 | +## Macros (#define-s) |
| 27 | + |
| 28 | +#### Stack_VERSION |
| 29 | + |
| 30 | +Defines the current version of the module, formatted as a `float`. |
| 31 | + |
| 32 | +#### Stack_VERSION_100 |
| 33 | + |
| 34 | +Defines version 1.0 of the module. |
| 35 | + |
| 36 | +## Enumerated types |
| 37 | + |
| 38 | +#### StackDataType |
| 39 | + |
| 40 | +- `eStackDataInt`: The stored data is an int |
| 41 | +- `eStackDataFloat`: The stored data is a float |
| 42 | +- `eStackDataString`: The stored data is a String |
| 43 | +- `eStackDataCharacter`: The stored data is a Character |
| 44 | +- `eStackDataInventoryItem`: The stored data is an InventoryItem |
| 45 | +- `eStackDataGUI`: The stored data is a GUI |
| 46 | +- `eStackDataGUIControl`: The stored data is a GUIControl |
| 47 | +- `eStackDataStack`: The stored data is a Stack object |
| 48 | + |
| 49 | +#### StackPopType |
| 50 | + |
| 51 | +- `eStackPopFirstInLastOut`: The first item pushed onto the stack will be the last item popped back |
| 52 | + out. This is the default setting for all stacks. |
| 53 | +- `eStackPopFirstInFirstOut`: The first item pushed onto the stack will be the first item popped |
| 54 | + back out. |
| 55 | +- `eStackPopRandom`: All items on the stack are popped out in random order. |
| 56 | + |
| 57 | +#### StackSettings |
| 58 | + |
| 59 | +- `eStackStrictTypes`: Sets whether the module obeys data types (`true`) or whether conversions are |
| 60 | +possible (`false`). The default value is `false`. |
| 61 | + |
| 62 | +**NOTE:** Data of the type `eStackDataStack` will ignore this setting. Data of this type may only |
| 63 | +be used/returned by functions which specifically state it in their description below. Conversion is |
| 64 | +not possible for this type. |
| 65 | + |
| 66 | +## Functions and Properties |
| 67 | + |
| 68 | +### StackData |
| 69 | + |
| 70 | +#### StackData.GetAsCharacter |
| 71 | + |
| 72 | +`Character* StackData.GetAsCharacter()` |
| 73 | + |
| 74 | +Returns this data as a `Character`. |
| 75 | + |
| 76 | +#### StackData.GetAsFloat |
| 77 | + |
| 78 | +`float StackData.GetAsFloat()` |
| 79 | + |
| 80 | +Returns this data as a `float`. Uses 0 as a `null` value. |
| 81 | + |
| 82 | +#### StackData.GetAsGUIControl |
| 83 | + |
| 84 | +`GUIControl* StackData.GetAsGUIControl()` |
| 85 | + |
| 86 | +Returns this data as a `GUIControl`. |
| 87 | + |
| 88 | +#### StackData.GetAsGUI |
| 89 | + |
| 90 | +`GUI* StackData.GetAsGUI()` |
| 91 | + |
| 92 | +Returns this data as a `GUI`. |
| 93 | + |
| 94 | +#### StackData.GetAsInt |
| 95 | + |
| 96 | +`int StackData.GetAsInt()` |
| 97 | + |
| 98 | +Returns this data as an `int`. Uses 0 as a `null` value. |
| 99 | + |
| 100 | +#### StackData.GetAsInventoryItem |
| 101 | + |
| 102 | +`InventoryItem* StackData.GetAsInventoryItem()` |
| 103 | + |
| 104 | +Returns this data as an `InventoryItem`. |
| 105 | + |
| 106 | +#### StackData.GetAsString |
| 107 | + |
| 108 | +`String StackData.GetAsString()` |
| 109 | + |
| 110 | +Returns this data as a `String`. |
| 111 | + |
| 112 | +#### StackData.GetType |
| 113 | + |
| 114 | +`StackDataType StackData.GetType()` |
| 115 | + |
| 116 | +Returns the [type](#stackdatatype) of this data. |
| 117 | + |
| 118 | +--------- |
| 119 | + |
| 120 | +### Stack |
| 121 | + |
| 122 | +#### Stack.CharacterToData |
| 123 | + |
| 124 | +`static StackData Stack.CharacterToData(Character *theCharacter)` |
| 125 | + |
| 126 | +Returns a StackData object containing `theCharacter`. |
| 127 | + |
| 128 | +#### Stack.Clear |
| 129 | + |
| 130 | +`void Stack.Clear()` |
| 131 | + |
| 132 | +Removes all items from the stack. |
| 133 | + |
| 134 | +#### Stack.Copy |
| 135 | + |
| 136 | +`StackData Stack.Copy()` |
| 137 | + |
| 138 | +Produces a specially formatted `StackData` object which contains a *copy* of this stack. This |
| 139 | +object can **only** be used with/by the following functions: |
| 140 | +[StackData.GetType](#stackdatagetype), [Stack.Push](#stackpush), [Stack.Pop](#stackpop), |
| 141 | +[Stack.LoadFromStack](#stackloadfromstack). |
| 142 | + |
| 143 | +#### Stack.FloatToData |
| 144 | + |
| 145 | +`static StackData Stack.FloatToData(float theFloat)` |
| 146 | + |
| 147 | +Returns a StackData object containing `theFloat`. |
| 148 | + |
| 149 | +#### Stack.GUIControlToData |
| 150 | + |
| 151 | +`static StackData Stack.GUIControlToData(GUIControl *theControl)` |
| 152 | + |
| 153 | +Returns a StackData object containing `theControl`. |
| 154 | + |
| 155 | +#### Stack.GUIToData |
| 156 | + |
| 157 | +`static StackData Stack.GUIToData(GUI *theGUI)` |
| 158 | + |
| 159 | +Returns a StackData object containing `theGUI`. |
| 160 | + |
| 161 | +#### Stack.IntToData |
| 162 | + |
| 163 | +`static StackData Stack.IntToData(int theInt)` |
| 164 | + |
| 165 | +Returns a StackData object containing `theInt`. |
| 166 | + |
| 167 | +#### Stack.InventoryItemToData |
| 168 | + |
| 169 | +`static StackData Stack.InventoryItemToData(InventoryItem *theItem)` |
| 170 | + |
| 171 | +Returns a StackData object containing `theItem`. |
| 172 | + |
| 173 | +#### Stack.IsEmpty |
| 174 | + |
| 175 | +`bool Stack.IsEmpty()` |
| 176 | + |
| 177 | +Returns whether the stack has **no** items in it. |
| 178 | + |
| 179 | +#### Stack.ItemCount |
| 180 | + |
| 181 | +`writeprotected int Stack.ItemCount` |
| 182 | + |
| 183 | +Returns the number of items stored in the stack. |
| 184 | + |
| 185 | +#### Stack.LoadFromStack |
| 186 | + |
| 187 | +`bool Stack.LoadFromStack(StackData otherStack)` |
| 188 | + |
| 189 | +Replaces this stack's data with the data from `otherStack`. `otherStack` must be a `StackData` |
| 190 | +object which was formatted by [Stack.Copy](#stackcopy) for this function to operate. Returns |
| 191 | +whether the data was successfully loaded. |
| 192 | + |
| 193 | +#### Stack.Pop |
| 194 | + |
| 195 | +`StackData Stack.Pop(optional bool remove, optional int index)` |
| 196 | + |
| 197 | +Pops an item off the stack (the index is determined by the stack's [PopType](#stackpoptype-1) |
| 198 | +setting). You may optionally choose whether or not to `remove` the item from the stack and/or |
| 199 | +supply an alternate `index`. The data returned may be a `StackData` object of type |
| 200 | +`eStackDataStack`. See the definition of this type and [Stack.Copy](#stackcopy) for further |
| 201 | +information. |
| 202 | + |
| 203 | +#### Stack.PopType |
| 204 | + |
| 205 | +`StackPopType Stack.PopType` |
| 206 | + |
| 207 | +Gets/sets the `StackPopType` for this stack, which controls the order in which items are popped off |
| 208 | +the stack. The default setting for this is `eStackPopFirstInLastOut`. |
| 209 | + |
| 210 | +#### Stack.Push |
| 211 | + |
| 212 | +`bool Stack.Push(StackData data, optional int index)` |
| 213 | + |
| 214 | +Pushes `data` onto the end of the stack. If `index` is provided, the item at that index in the |
| 215 | +stack will be replaced with `data`. Returns whether the operation was successful. Rejects `null` |
| 216 | +data and invalid indices. This function will accept `StackData` objects which have been formatted |
| 217 | +by [Stack.Copy](#stackcopy). |
| 218 | + |
| 219 | +#### Stack.StringToData |
| 220 | + |
| 221 | +`static StackData Stack.StringToData(String theString)` |
| 222 | + |
| 223 | +Returns a StackData object containing `theString`. |
| 224 | + |
| 225 | +## Examples |
| 226 | + |
| 227 | +This module is admittedly a bit intimidating, so here's a couple examples of how the scripts can be used. |
| 228 | + |
| 229 | +### Stack definition |
| 230 | + |
| 231 | +Here we'll just define a `Stack` to use with the rest of our examples. |
| 232 | + |
| 233 | + Stack mystack; |
| 234 | + mystack.PopType = eStackPopFirstInFirstOut; // pop the items out in the same order they are pushed in |
| 235 | + |
| 236 | +We'll assume this definition for the rest of the examples. |
| 237 | + |
| 238 | +### Pushing int data |
| 239 | + |
| 240 | +Let's push some integers onto our stack, and pop them back out! |
| 241 | + |
| 242 | + mystack.Push(Stack.IntToData(351)); // note we must convert our ints to our generic StackData type |
| 243 | + mystack.Push(Stack.IntToData(493)); |
| 244 | + mystack.Push(Stack.IntToData(826)); |
| 245 | + Display("Our stack has %d items in it.", mystack.ItemCount); |
| 246 | + int i = 0; |
| 247 | + int size = mystack.ItemCount; // note that since we are popping items off our stack, it is shrinking. we must store its size first! |
| 248 | + while (i < size) |
| 249 | + { |
| 250 | + StackData data = mystack.Pop(); |
| 251 | + Display("The item from %d is %d", i, data.GetAsInt()); // we must convert our data back to an int to display it |
| 252 | + i++; |
| 253 | + } |
| 254 | + Display("Our stack NOW has %d items in it.", mystack.ItemCount); |
| 255 | + |
| 256 | +That will display the following: |
| 257 | + |
| 258 | + Our stack has 3 items in it. |
| 259 | + The item from 0 is 351. |
| 260 | + The item from 1 is 493. |
| 261 | + The item from 2 is 826. |
| 262 | + Our stack NOW has 0 items in it. |
| 263 | + |
| 264 | +### Stacked stacks |
| 265 | + |
| 266 | +Let's try pushing stacks onto each other! |
| 267 | + |
| 268 | + mystack.Push(Stack.StringToData("Hello World!")); |
| 269 | + mystack.Push(Stack.StringToData("This is the script.")); |
| 270 | + mystack.Push(Stack.StringToData("Well, I've got to go.")); |
| 271 | + mystack.Push(Stack.StringToData("Goodbye World.")); |
| 272 | + Stack otherstack; |
| 273 | + StackData mystackCopy = mystack.Copy(); |
| 274 | + otherstack.Push(mystackCopy); // we use Stack.Copy to push mystack onto otherstack |
| 275 | + Display("otherstack has %d item(s).", otherstack.ItemCount); |
| 276 | + |
| 277 | +Displays: |
| 278 | + |
| 279 | + otherstack has 1 item(s). |
| 280 | + |
| 281 | +*But wait! What I really wanted to do was just make `otherstack` a copy of the data in `mystack`.* |
| 282 | + |
| 283 | +Don't worry, that's easy enough to fix! |
| 284 | + |
| 285 | + otherstack.Clear(); // removes all the items from otherstack (note this step is not necessary) |
| 286 | + otherstack.LoadFromStack(mystackCopy); // load from stack will now copy each element from mystackCopy into otherstack |
| 287 | + Display("otherstack NOW has %d item(s).", otherstack.ItemCount); |
| 288 | + |
| 289 | +Displays: |
| 290 | + |
| 291 | + otherstack NOW has 4 item(s). |
| 292 | + |
| 293 | +That's it for the example code. If you have any questions, comments, bug reports, etc. feel free to |
| 294 | +contact me on the forums. For any bug reports, please post to the forum thread so everyone can be |
| 295 | +notified! |
0 commit comments