Created
June 17, 2020 21:37
-
-
Save erodozer/aa84be874fbcebf5bd2c479e2def3673 to your computer and use it in GitHub Desktop.
Convenient wrapper for Godot ECMAScript to support Rxjs flows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { fromEventPattern } from 'rxjs'; | |
module.exports = { | |
fromEvent(instance, target, signal) { | |
return fromEventPattern( | |
handler => { | |
target.connect(signal, instance, handler); | |
}, | |
handler => { | |
target.disconnect(signal, instance, handler); | |
} | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { fromEvent } from 'godot-rxjs'; | |
import { merge } from 'rxjs/operators'; | |
export default class InventoryUI extends godot.Control { | |
_ready() { | |
merge( | |
fromEvent(InventorySingleton, this, 'add_item'), | |
fromEvent(InventorySingleton, this, 'remove_item'), | |
).subscribe(() => this.refreshUI()); | |
} | |
refreshUI() { | |
const itemList = this.get_node("ItemList"); | |
itemList.clear(); | |
for (let item of Inventory.items) { | |
const { | |
name, | |
amount, | |
} = item; | |
itemList.add_item(`${name}: x${amount}`); | |
const idx = itemList.get_item_count(); | |
itemList.set_item_metadata(idx, item); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment