Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/agulev/jstodef
Library for sending messages from JavaScript to Defold (Lua)
https://github.com/agulev/jstodef
defold defold-game-engine defold-library emscripten lua
Last synced: 3 months ago
JSON representation
Library for sending messages from JavaScript to Defold (Lua)
- Host: GitHub
- URL: https://github.com/agulev/jstodef
- Owner: AGulev
- License: mit
- Created: 2019-03-08T12:30:01.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-04-10T08:06:54.000Z (10 months ago)
- Last Synced: 2024-10-25T09:48:39.842Z (3 months ago)
- Topics: defold, defold-game-engine, defold-library, emscripten, lua
- Language: C++
- Homepage:
- Size: 49.8 KB
- Stars: 38
- Watchers: 6
- Forks: 7
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JsToDef
[![Build Status](https://github.com/AGulev/jstodef/workflows/Build%20with%20bob/badge.svg)](https://github.com/AGulev/jstodef/actions)
This is [Native Extension](https://www.defold.com/manuals/extensions/) for the [Defold Game Engine](https://www.defold.com) that makes possible to send messages from JavaScript to Lua in [HTML5 build](https://www.defold.com/manuals/html5/).
## Installation
To use this library in your Defold project, add the needed version URL to your `game.project` dependencies from [Releases](https://github.com/AGulev/jstodef/releases)## 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:
```javascript
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:
```lua
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:
```lua
local function js_listener(self, message_id, message)
if message_id == "MyCustomMessageName" then
-- do something
end
endfunction 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.
```lua
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](https://github.com/agulev/jstodef/issues) or contact me: [email protected]