MCPcopy
hub / github.com/4ian/GDevelop / fromJSObject

Method fromJSObject

GDJS/Runtime/variable.ts:309–356  ·  view source on GitHub ↗

* Converts a JavaScript object into a value compatible * with GDevelop variables and store it inside this variable. * @param obj - The value to convert.

(obj: any)

Source from the content-addressed store, hash-verified

307 * @param obj - The value to convert.
308 */
309 fromJSObject(obj: any): this {
310 if (obj === null) {
311 this.setString('null');
312 } else if (typeof obj === 'number') {
313 if (Number.isNaN(obj)) {
314 logger.warn('Variables cannot be set to NaN, setting it to 0.');
315 this.setNumber(0);
316 } else {
317 this.setNumber(obj);
318 }
319 } else if (typeof obj === 'string') {
320 this.setString(obj);
321 } else if (typeof obj === 'undefined') {
322 // Do not modify the variable, as there is no value to set it to.
323 } else if (typeof obj === 'boolean') {
324 this.setBoolean(obj);
325 } else if (Array.isArray(obj)) {
326 this.castTo('array');
327 this.clearChildren();
328 for (const i in obj) this.getChild(i).fromJSObject(obj[i]);
329 } else if (typeof obj === 'object') {
330 this.castTo('structure');
331 this.clearChildren();
332 for (var p in obj)
333 if (obj.hasOwnProperty(p)) this.getChild(p).fromJSObject(obj[p]);
334 } else if (typeof obj === 'symbol') {
335 this.setString(obj.toString());
336 } else if (typeof obj === 'bigint') {
337 if (obj > Number.MAX_SAFE_INTEGER)
338 logger.warn(
339 'Error while converting JS variable to GDevelop variable: Integers bigger than ' +
340 Number.MAX_SAFE_INTEGER +
341 " aren't supported by GDevelop variables, it will be reduced to that size."
342 );
343 // @ts-ignore
344 this.setNumber(parseInt(obj, 10));
345 } else if (typeof obj === 'function') {
346 logger.error(
347 'Error while converting JS variable to GDevelop variable: Impossible to set variable value to a function.'
348 );
349 } else {
350 logger.error(
351 'Error while converting JS variable to GDevelop variable: Cannot identify type of object ' +
352 obj
353 );
354 }
355 return this;
356 }
357
358 /**
359 * Unserialize a JSON string into this variable.

Calls 9

setStringMethod · 0.95
setNumberMethod · 0.95
setBooleanMethod · 0.95
castToMethod · 0.95
clearChildrenMethod · 0.95
getChildMethod · 0.95
toStringMethod · 0.65
errorMethod · 0.65
warnMethod · 0.45