-
Notifications
You must be signed in to change notification settings - Fork 716
/
variabletools.ts
263 lines (245 loc) · 8.01 KB
/
variabletools.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
* GDevelop JS Platform
* Copyright 2013-2016 Florian Rival (Florian.Rival@gmail.com). All rights reserved.
* This project is released under the MIT License.
*/
namespace gdjs {
export namespace evtTools {
/**
* A namespace of functional equivalents to the gdjs.Variable methods, needed for events code generation.
* @private
* @namespace
*/
export namespace variable {
/**
* Get the value of a variable. Equivalent to `variable.getAsNumber()`.
* This shortcut function is needed for events code generation.
* @private
*/
export const getVariableNumber = function (
variable: gdjs.Variable
): number {
return variable.getAsNumber();
};
/**
* Get the string of a variable. Equivalent to `variable.getAsString()`.
* This shortcut function is needed for events code generation.
* @private
*/
export const getVariableString = function (
variable: gdjs.Variable
): string {
return variable.getAsString();
};
/**
* Compares the boolean value of a variable.
* Equivalent to `variable.getAsBoolean() === boolean`.
* This shortcut function is needed for events code generation.
* @private
*/
export const getVariableBoolean = function (
variable: gdjs.Variable,
compareWith: boolean
): boolean {
return variable.getAsBoolean() === compareWith;
};
/**
* Set the boolean value of a variable. Equivalent to `variable.setBoolean()`.
* This shortcut function is needed for events code generation.
* @private
*/
export const setVariableBoolean = function (
variable: gdjs.Variable,
newValue: boolean
) {
variable.setBoolean(newValue);
};
/**
* Toggles the boolean value of a variable.
* This shortcut function is needed for events code generation.
* @private
*/
export const toggleVariableBoolean = function (variable: gdjs.Variable) {
variable.setBoolean(!variable.getAsBoolean());
};
/**
* Check if a scene variable exists.
* This shortcut function is needed for events code generation.
* @private
*/
export const sceneVariableExists = function (
runtimeScene: gdjs.RuntimeScene,
variableName: string
): boolean {
return runtimeScene.getScene().getVariables().has(variableName);
};
/**
* Check if a global variable exists.
* This shortcut function is needed for events code generation.
* @private
*/
export const globalVariableExists = function (
instanceContainer: gdjs.RuntimeInstanceContainer,
variableName: string
): boolean {
return instanceContainer.getGame().getVariables().has(variableName);
};
/**
* Check if a child exists in a variable.
* This shortcut function is needed for events code generation.
* @private
*/
export const variableChildExists = function (
variable: gdjs.Variable,
childName: string
): boolean {
return variable.hasChild(childName);
};
/**
* Remove the child with the given name in a variable.
* This shortcut function is needed for events code generation.
* @private
*/
export const variableRemoveChild = function (
variable: gdjs.Variable,
childName: string
): void {
variable.removeChild(childName);
};
/**
* Clear the children in a variable.
* This shortcut function is needed for events code generation.
* @private
*/
export const variableClearChildren = function (variable: gdjs.Variable) {
variable.clearChildren();
};
/**
* Pushes a variable onto an array.
* This shortcut function is needed for events code generation.
* @private
*/
export const variablePushCopy = function (
array: gdjs.Variable,
variable: gdjs.Variable
) {
array.pushVariableCopy(variable);
};
/**
* Pushes a value onto an array.
* This shortcut function is needed for events code generation.
* @private
*/
export const valuePush = function (
array: gdjs.Variable,
value: string | float | boolean
) {
array.pushValue(value);
};
/**
* Removes an index from an array.
* This shortcut function is needed for events code generation.
* @private
*/
export const variableRemoveAt = function (
array: gdjs.Variable,
index: number
) {
array.removeAtIndex(index);
};
/**
* Get the number of children in a variable.
* This shortcut function is needed for events code generation.
* @private
*/
export const getVariableChildCount = function (
variable: gdjs.Variable
): number {
return variable.getChildrenCount();
};
/**
* Shortcut to get the first value of an array variable as a number.
*/
export const getFirstVariableNumber = function (
array: gdjs.Variable
): number {
if (array.getChildrenCount() === 0) {
return 0;
}
return array.getAllChildrenArray()[0].getAsNumber();
};
/**
* Shortcut to get the last value of an array variable as a string.
*/
export const getFirstVariableString = function (
array: gdjs.Variable
): string {
if (array.getChildrenCount() === 0) {
return '';
}
return array.getAllChildrenArray()[0].getAsString();
};
/**
* Shortcut to get the last value of an array variable as a number.
*/
export const getLastVariableNumber = function (
array: gdjs.Variable
): number {
const children = array.getAllChildrenArray();
return children.length === 0
? 0
: children[children.length - 1].getAsNumber();
};
/**
* Shortcut to get the last value of an array variable as a string.
*/
export const getLastVariableString = function (
array: gdjs.Variable
): string {
const children = array.getAllChildrenArray();
return children.length === 0
? ''
: children[children.length - 1].getAsString();
};
}
export namespace common {
/** @deprecated */
export const getVariableNumber = gdjs.evtTools.variable.getVariableNumber;
/** @deprecated */
export const getVariableString = gdjs.evtTools.variable.getVariableString;
/** @deprecated */
export const getVariableBoolean =
gdjs.evtTools.variable.getVariableBoolean;
/** @deprecated */
export const setVariableBoolean =
gdjs.evtTools.variable.setVariableBoolean;
/** @deprecated */
export const toggleVariableBoolean =
gdjs.evtTools.variable.toggleVariableBoolean;
/** @deprecated */
export const sceneVariableExists =
gdjs.evtTools.variable.sceneVariableExists;
/** @deprecated */
export const globalVariableExists =
gdjs.evtTools.variable.globalVariableExists;
/** @deprecated */
export const variableChildExists =
gdjs.evtTools.variable.variableChildExists;
/** @deprecated */
export const variableRemoveChild =
gdjs.evtTools.variable.variableRemoveChild;
/** @deprecated */
export const variableClearChildren =
gdjs.evtTools.variable.variableClearChildren;
/** @deprecated */
export const variablePushCopy = gdjs.evtTools.variable.variablePushCopy;
/** @deprecated */
export const valuePush = gdjs.evtTools.variable.valuePush;
/** @deprecated */
export const variableRemoveAt = gdjs.evtTools.variable.variableRemoveAt;
/** @deprecated */
export const getVariableChildCount =
gdjs.evtTools.variable.getVariableChildCount;
}
}
}