NWNX:EE  8193.36.12
array_example.nss
Go to the documentation of this file.
1 //#include "inc_array"
2 #include "nwnx_time"
3 
4 // nwnx_data also includes inc_array, so don't double dip.
5 #include "nwnx_data"
6 
7 void Log(string msg)
8 {
9  WriteTimestampedLogEntry(msg);
10 }
11 
13 {
14 
15  string array = "test";
16 
17  // By default, temporary arrays are created on the module.
18  Array_PushBack_Str(array, "BItem1");
19  Array_PushBack_Str(array, "AItem2");
20  Array_PushBack_Str(array, "AItem3");
21  Array_PushBack_Str(array, "BItem2");
22  Array_Debug_Dump(array, "After first load");
23 
24  int foo = Array_Find_Str(array, "AItem3");
25  Log("Found element AItem3 at index = " + IntToString(foo));
26 
27  Array_Set_Str(array, 2, "Suck it up...");
28  Array_Debug_Dump(array, "After set 2 = 'Suck it up...'");
29 
30  Array_Erase(array, 1);
31  Array_Debug_Dump(array, "After delete 1");
32 
33  Array_PushBack_Str(array, "MItem1");
34  Array_PushBack_Str(array, "QItem2");
35  Array_PushBack_Str(array, "NItem3");
36  Array_PushBack_Str(array, "KItem2");
37 
38  Array_Debug_Dump(array, "After add more");
39  Array_SortAscending(array);
40 
41  Array_Debug_Dump(array, "After sort");
42 
43  Array_Shuffle(array);
44  Array_Debug_Dump(array, "After shuffle");
45 
46  Log( (Array_Contains_Str(array, "NItem3")) ? "Passed.. found it" : "Failed.. should have found it" );
47  Log( (Array_Contains_Str(array, "KItem2")) ? "Passed.. found it" : "Failed.. should have found it" );
48  Log( (Array_Contains_Str(array, "xxxxxx")) ? "Failed.. not found" : "Passed.. should not exist" );
49 
50  Array_Clear(array);
51  // Load up the array with 100 entries
52  int i;
53 
56  Log("Start Time: " + IntToString(b.seconds) + "." + IntToString(b.microseconds));
57  for (i=0; i<1000; i++)
58  {
59  Array_PushBack_Str(array, IntToString(d100()) + " xxx " + IntToString(i));
60  }
62  Log("Loaded 1000: " + IntToString(b.seconds) + "." + IntToString(b.microseconds));
63  Array_Shuffle(array);
65  Log("Shuffled 1000: " + IntToString(b.seconds) + "." + IntToString(b.microseconds));
66  for (i=5; i<995; i++)
67  {
68  // Delete the third entry a bunch of times
69  Array_Erase(array, 3);
70  }
72  Log("Delete ~990: " + IntToString(b.seconds) + "." + IntToString(b.microseconds));
73  Array_Debug_Dump(array, "After mass insert/delete");
74 
75 }
76 
78 {
79  string array="chicken";
80  // Let's create an array "on" our favorite creature: the deadly nw_chicken
81  // Note - arrays aren't really attached to the item, but the module, and they
82  // are tagged with the objects string representation.
83  object oCreature = CreateObject(OBJECT_TYPE_CREATURE, "nw_chicken", GetStartingLocation());
84  if (!GetIsObjectValid(oCreature))
85  {
86  Log("NWNX_Creature test: Failed to create creature");
87  return;
88  }
89 
90  Array_PushBack_Str(array, "BItem1", oCreature);
91  Array_PushBack_Str(array, "AItem2", oCreature);
92  Array_PushBack_Str(array, "AItem3", oCreature);
93  Array_PushBack_Str(array, "BItem2", oCreature);
94  Array_Debug_Dump(array, "After Chicken array load", oCreature);
95 
96 }
97 
99 {
100  Log("");
101  Log("Start NWNX_Data test.");
102  string array = "test2";
103 
104  NWNX_Data_Array_PushBack_Str(GetModule(), array, "XItem1");
105  NWNX_Data_Array_PushBack_Str(GetModule(), array, "ZItem2");
106  NWNX_Data_Array_PushBack_Str(GetModule(), array, "ZItem3");
107  NWNX_Data_Array_PushBack_Str(GetModule(), array, "XItem2");
108  Array_Debug_Dump(array, "After first load");
109 
110  int foo = NWNX_Data_Array_Find_Str(GetModule(), array, "ZItem3");
111  Log("Found element AItem3 at index = " + IntToString(foo));
112 
113  NWNX_Data_Array_Set_Str(GetModule(), array, 2, "Suck it up...");
114  Array_Debug_Dump(array, "After set 2 = 'Suck it up...'");
115 
116  NWNX_Data_Array_Erase(NWNX_DATA_TYPE_STRING, GetModule(), array, 1);
117  Array_Debug_Dump(array, "After delete 1");
118 
119  NWNX_Data_Array_PushBack_Str(GetModule(), array, "MItem1");
120  NWNX_Data_Array_PushBack_Str(GetModule(), array, "QItem2");
121  NWNX_Data_Array_PushBack_Str(GetModule(), array, "NItem3");
122  NWNX_Data_Array_PushBack_Str(GetModule(), array, "KItem2");
123 
124  Array_Debug_Dump(array, "After add more");
126 
127  Array_Debug_Dump(array, "After sort");
128 
129 }
130 
131 // Uncomment and assign to some event click.
132 /* */
133 void main()
134 {
135  Log("Start");
136 
138 
140 
141  TestNWNXArray();
142 }
143 /* */
NWNX_Data_Array_PushBack_Str
void NWNX_Data_Array_PushBack_Str(object obj, string tag, string element)
Definition: nwnx_data.nss:258
main
void main()
Definition: array_example.nss:133
Array_Debug_Dump
void Array_Debug_Dump(string tag, string title="xxx", object obj=OBJECT_INVALID)
Definition: inc_array.nss:498
NWNX_Data_Array_Set_Str
void NWNX_Data_Array_Set_Str(object obj, string tag, int index, string element)
Definition: nwnx_data.nss:329
NWNX_Time_GetHighResTimeStamp
struct NWNX_Time_HighResTimestamp NWNX_Time_GetHighResTimeStamp()
Definition: nwnx_time.nss:56
Array_Shuffle
void Array_Shuffle(string tag, object obj=OBJECT_INVALID)
Reorders the array such each possible permutation of elements has equal probability of appearance.
Definition: inc_array.nss:414
Array_Set_Str
void Array_Set_Str(string tag, int index, string element, object obj=OBJECT_INVALID)
Definition: inc_array.nss:471
Array_Find_Str
int Array_Find_Str(string tag, string element, object obj=OBJECT_INVALID)
Definition: inc_array.nss:301
Array_Clear
void Array_Clear(string tag, object obj=OBJECT_INVALID)
Clears the entire array, such that size==0.
Definition: inc_array.nss:232
NWNX_Data_Array_SortAscending
void NWNX_Data_Array_SortAscending(int type, object obj, string tag)
Sorts the collection based on descending order.
Definition: nwnx_data.nss:314
Array_PushBack_Str
void Array_PushBack_Str(string tag, string element, object obj=OBJECT_INVALID)
Definition: inc_array.nss:375
TestArrayOnModule
void TestArrayOnModule()
Definition: array_example.nss:12
NWNX_Time_HighResTimestamp
A high resolution timestamp.
Definition: nwnx_time.nss:26
Log
void Log(string msg)
Definition: array_example.nss:7
Array_Erase
void Array_Erase(string tag, int index, object obj=OBJECT_INVALID)
Erases the element at index, and shuffles any elements from index size-1 to index + 1 left.
Definition: inc_array.nss:281
NWNX_Data_Array_Find_Str
int NWNX_Data_Array_Find_Str(object obj, string tag, string element)
Definition: nwnx_data.nss:204
NWNX_DATA_TYPE_STRING
const int NWNX_DATA_TYPE_STRING
Definition: nwnx_data.nss:15
NWNX_Data_Array_Erase
void NWNX_Data_Array_Erase(int type, object obj, string tag, int index)
Erases the element at index, and shuffles any elements from index size-1 to index + 1 left.
Definition: nwnx_data.nss:195
TestArrayOnChicken
void TestArrayOnChicken()
Definition: array_example.nss:77
TestNWNXArray
void TestNWNXArray()
Definition: array_example.nss:98
Array_SortAscending
void Array_SortAscending(string tag, int type=TYPE_STRING, object obj=OBJECT_INVALID)
Sorts the collection based on descending order.
Definition: inc_array.nss:458
Array_Contains_Str
int Array_Contains_Str(string tag, string element, object obj=OBJECT_INVALID)
Definition: inc_array.nss:239