Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HTML5] Implement Godot <-> JavaScript interface. #48719

Merged
merged 2 commits into from
May 20, 2021

Conversation

Faless
Copy link
Collaborator

@Faless Faless commented May 14, 2021

4.0 version of: #48691 .

The API is the same, with the exception of JavaScript.create_callback which now takes a callable (first line below):

var _my_js_callback = JavaScript.create_callback(myCallback) # This reference must be kept
var console = JavaScript.get_interface("console")

func _init():
	var buf = JavaScript.create_object("ArrayBuffer", 10) # new ArrayBuffer(10)
	print(buf) # prints [JavaScriptObject:OBJECT_ID]
	var uint8arr = JavaScript.create_object("Uint8Array", buf) # new Uint8Array(buf)
	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 ]"

	# Equivalent of JavaScript: Array.from(uint8arr).forEach(myCallback)
	JavaScript.get_interface("Array").from(uint8arr).forEach(_my_js_callback)

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)

Not really easy to test in current master, but you can build the thread version:
scons p=javascript target=release_debug threads_enabled=yes
And check the console. If the startup get stuck, try editing platform/javascript/js/libs/library_godot_os.js and have godot_js_os_hw_concurrency_get always return 2 (otherwise Godot will spawn to many threads and crash your browser).

}
},

godot_js_wrapper_create_object__sig: 'iiiiiiii',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be more readable as viii ;) (ok I'm pretty sure I already made that joke last year :P)

@Faless Faless force-pushed the js/4.x_interfaces branch from fdc57cc to 2aefa94 Compare May 20, 2021 12:26
@Faless Faless force-pushed the js/4.x_interfaces branch from 2aefa94 to 9811035 Compare May 20, 2021 12:33
@akien-mga akien-mga merged commit aa55522 into godotengine:master May 20, 2021
@akien-mga
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants