JsToDef - simple way to send messages from JS to Lua

JsToDef

Native Extension that makes possible to send messages from JavaScript to Lua in HTML5 build.

Setup

You can use the JsToDef extension in your own project by adding this project as a Defold library dependency. Open your game.project file and in the dependencies field under project add:

https://github.com/AGulev/jstodef/archive/master.zip

Or point to the ZIP file of a specific release.

API JavaScript side

JsToDef.send(message_id, message)

Where message_id is a string that helps you to identify this message on Lua side.

message is a custom value that might be one of the next types: object, number, boolean, string, undefined(if you donā€™t need any extra data).

Example:
JsToDef.send("MyCustomMessageName", "custom message");
JsToDef.send("ObjectEvent", {foo:"bar", num:16, isAv:true});
JsToDef.send("FloatEvent", 19.2);
JsToDef.send("IntEvent", 18);
JsToDef.send("StrintEvent", "custom string");
JsToDef.send("EmptyEvent");
JsToDef.send("BooleanEvent", true);
JsToDef.send("BooleanEvent", false);

API Lua side

If you are working on cross-platform application the best practice to check the existence of jstodef module, this module exists only in html5 build:

if jstodef then
  -- any operations with jstodef
end

Add Listener

jstodef.add_listener(listener)

Where listener is a function with the next parameters:

self is the current script self.

message_id is a string that helps you to identify this message.

message is a custom value that might be one of the next types: table, number, boolean, string, nil.

It is possible to add a few listeners.

Example:
local function js_listener(self, message_id, message)
  if message_id == "MyCustomMessageName" then
    -- do something
  end
end

function init(self)
  if jstodef then
    jstodef.add_listener(js_listener)
  end
end

Remove Listener

jstodef.remove_listener(listener)

Where listener is the function that was previously added as a listener with jstodef.add_listener() method.

function final(self)
  if jstodef then
    jstodef.remove_listener(js_listener)
  end
end

Issues and suggestions

If you have any issues, questions or suggestions please create an issue or contact me: me@agulev.com

22 Likes

awesomeļ¼ļ¼ļ¼ļ¼

1 Like

Hello guys,
I know this is an older thread but i wanted to know if it is possible to add this extension to playable ad project which was also created by @AGulev. I wanted to make playable for UAC(google) and there is a problem that you canā€™t use game input because UAC will also call ExitApi.exit() which takes you right to the store page. Thanks.

The JsToDef extension should work in any HTML5 project, so yes, I believe you would be able to use it in a playable ad. Itā€™s worth a try at least!

1 Like

OK. Quick attemp. Not working , it say: ā€œYou didnā€™t set callback for JsToDefā€ (and yes iā€™ve setted it in code)ā€¦ Have to play with HTML template. I let you know guys when when i make it.

Hello, iā€™ve solved that issue. The problem was that i am lazy trash :D. I didnā€™t rename on_input function. After that i rename it everything worked fine. Thanks guys and 1+ to @AGulev for awesome extension.

1 Like

You didnā€™t set callback for JsToDef

how to check exist callback on not in lua?

	<script type='text/javascript'>
		var checkFocus = function() {
      if (typeof JsToDef === 'object') {
        JsToDef.send("checkFocus", document.hasFocus());
      }
		}
		window.addEventListener("focus", checkFocus);
		window.addEventListener("blur", checkFocus);
	</script>

and i realy dont use callback in all scripts

all works fine - but i confused this error in console