2021-01-26 09:06:29 +00:00
<?xml version="1.0" encoding="UTF-8" ?>
2023-03-01 00:44:37 +00:00
<class name= "JavaScriptObject" inherits= "RefCounted" version= "4.1" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "../class.xsd" >
2021-01-26 09:06:29 +00:00
<brief_description >
2022-09-02 12:40:41 +00:00
A wrapper class for web native JavaScript objects.
2021-01-26 09:06:29 +00:00
</brief_description>
<description >
2022-09-02 12:40:41 +00:00
JavaScriptObject is used to interact with JavaScript objects retrieved or created via [method JavaScriptBridge.get_interface], [method JavaScriptBridge.create_object], or [method JavaScriptBridge.create_callback].
2022-11-01 14:29:38 +00:00
[b]Example:[/b]
2021-01-26 09:06:29 +00:00
[codeblock]
extends Node
2023-03-30 03:50:32 +00:00
var _my_js_callback = JavaScriptBridge.create_callback(myCallback) # This reference must be kept
2022-09-02 12:40:41 +00:00
var console = JavaScriptBridge.get_interface("console")
2021-01-26 09:06:29 +00:00
func _init():
2022-09-02 12:40:41 +00:00
var buf = JavaScriptBridge.create_object("ArrayBuffer", 10) # new ArrayBuffer(10)
2021-01-26 09:06:29 +00:00
print(buf) # prints [JavaScriptObject:OBJECT_ID]
2022-09-02 12:40:41 +00:00
var uint8arr = JavaScriptBridge.create_object("Uint8Array", buf) # new Uint8Array(buf)
2021-01-26 09:06:29 +00:00
uint8arr[1] = 255
prints(uint8arr[1], uint8arr.byteLength) # prints 255 10
console.log(uint8arr) # prints in browser console "Uint8Array(10) [ 0, 255, 0, 0, 0, 0, 0, 0, 0, 0 ]"
2022-09-02 12:40:41 +00:00
# Equivalent of JavaScriptBridge: Array.from(uint8arr).forEach(myCallback)
JavaScriptBridge.get_interface("Array").from(uint8arr).forEach(_my_js_callback)
2021-01-26 09:06:29 +00:00
func myCallback(args):
# Will be called with the parameters passed to the "forEach" callback
# [0, 0, [JavaScriptObject:1173]]
# [255, 1, [JavaScriptObject:1173]]
# ...
# [0, 9, [JavaScriptObject:1180]]
print(args)
[/codeblock]
2022-08-28 18:27:45 +00:00
[b]Note:[/b] Only available in the Web platform.
2021-01-26 09:06:29 +00:00
</description>
<tutorials >
</tutorials>
</class>