Skip to content

Commit f9e0cfa

Browse files
committed
Simplified data functions
Data functions (i.e. `SaveData`, `HasData`, `GetData`, `GetAllData`) have been simplified. The last one has been removed and replaced by a parameterless signature of `GetData`. Furthermore, all these functions will only have an effect on the first entity in the array.
1 parent 8af8e40 commit f9e0cfa

File tree

2 files changed

+32
-43
lines changed

2 files changed

+32
-43
lines changed

QueryArray.nut

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -105,52 +105,42 @@ class csQuery.QueryArray {
105105
}
106106

107107
function SaveData(key, value) {
108-
this.Each(function (ent) : (key, value) {
109-
ent.ValidateScriptScope();
110-
local scope = ent.GetScriptScope();
111-
if (!("customData" in scope))
112-
scope.customData <- {};
113-
scope.customData[key] <- value;
114-
});
108+
local ent = this.Eq(0);
109+
ent.ValidateScriptScope();
110+
local scope = ent.GetScriptScope();
111+
if (!("customData" in scope))
112+
scope.customData <- {};
113+
scope.customData[key] <- value;
115114
return this;
116115
}
117116

118117
function HasData(key) {
119-
local env = { key = key, hasData = false }
120-
this.Each(function (ent) {
121-
ent.ValidateScriptScope();
122-
local scope = ent.GetScriptScope();
123-
if (!("customData" in scope))
124-
return;
118+
local hasData = false;
119+
local ent = this.Eq(0);
120+
ent.ValidateScriptScope();
121+
local scope = ent.GetScriptScope();
125122

126-
if (key in scope.customData)
127-
hasData = true;
128-
}.bindenv(env));
129-
return env.hasData;
130-
}
123+
if (!("customData" in scope))
124+
return;
131125

132-
function GetData(key) {
133-
return getData(false, key)
134-
}
126+
if (key in scope.customData)
127+
hasData = true;
135128

136-
function GetAllData() {
137-
return getData(true);
129+
return hasData;
138130
}
139131

140-
function getData(returnAll, key = null) {
141-
local env = { key = key, returnAll = returnAll, data = [] }
142-
this.Each(function (ent) {
143-
ent.ValidateScriptScope();
144-
local scope = ent.GetScriptScope();
145-
if (!("customData" in scope))
146-
throw "Data table has not been created";
147-
148-
if (returnAll == true)
149-
data.push(scope.customData);
150-
else
151-
data.push(scope.customData[key]);
152-
}.bindenv(env));
153-
return env.data;
132+
function GetData(key = null) {
133+
local ent = this.Eq(0);
134+
ent.ValidateScriptScope();
135+
local scope = ent.GetScriptScope();
136+
137+
if (!("customData" in scope))
138+
throw "Data table has not been created";
139+
140+
if (key)
141+
return scope.customData[key];
142+
else
143+
return scope.customData;
154144
}
155145

156146
function PrecacheModels(models) {

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,14 @@ QueryArray On(string output, function callback, string id);
5858
// Stops listening to the given output. The callback removed is the one identified with the id
5959
QueryArray Off(string output, string id);
6060

61-
// Data functions not recommended when dealing with multiple entities
62-
// Saves the given value with the given identification (key). IMPORTANT: Data saved will 'live' throughout all rounds.
61+
// Saves the given value with the given identification (key) in the first entity's scope. IMPORTANT: Data saved will 'live' throughout all rounds.
6362
QueryArray SaveData(string key, object value);
64-
// Returns Whether data under the given key has already been stored. Will not throw an exception if SaveData() has never been used before.
63+
// Returns whether data under the given key has already been stored in the first entity's scope. Will not throw an exception if SaveData() has never been used before.
6564
bool HasData(string key);
66-
// Returns an array of objects which match with the given key. Will throw an exception if SaveData() has never been used before.
65+
// Returns an array of objects which match with the given key in the first entity's scoep. Will throw an exception if SaveData() has not been used with the first entity.
6766
object[] GetData(string key);
68-
// Returns an array of tables which contain all data stored through SaveData(). Will throw an exception if SaveData() has never been used before.
69-
table[] GetAllData();
67+
// Returns a table which contains all data stored in the first entity's scope through SaveData(string key, object value). Will throw an exception if SaveData() has not been used with the first entity.
68+
table GetData();
7069

7170
// Precaches each model in the array. Don't worry, each model is precached only once
7271
QueryArray PrecacheModels(string[] models);

0 commit comments

Comments
 (0)