* 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)
| 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. |