How do I access an item's commands in javascript?

Quick personal background: I’m very new to Stonehearth modding and javascript, though I did a bit of lua modding in WoW years ago, mostly just tweaking existing mods, I grew up learning Basic in many forms, I used Java in school, and I’m a .NET and Office developer by trade.

I don’t have a good sense of how to reference things outside of the local scope. My starting point is @Gruffus’s Town Monitor mod. I’m trying to replicate some functionality found in the Stonehearth unit frame (stonehearth\ui\game\unit_frame), but I can’t figure out where the command object is being passed from and how to reference it in javascript in order to call App.stonehearthClient.doCommand(…) on the item.

Can anyone point me in the right direction for accessing/calling an item’s commands via javascript?

In the unit frame, we grab a map of all of the commands that is on a specific entity inside of the commands: function() { on line 233 (of the latest build). This is done using this.get('model.stonehearth:commands.commands') The command is a javascript object with the same key-values as the command’s json and some extra data like whether the command is enabled or not. If you print out the commands array from line 252 you can see what the format of it looks like.

1 Like

Thanks, but this is where I run into scope issues. I don’t know what “this” is in that context, and when I try to use that same command I get an Uncaught TypeError: this.get is not a function. So I assume I have to call it at some higher level, but I’m not sure what that level is or how to do it. Can I do something like App.stonehearthClient.get(…) or App.view.get(…) or do I need the instance of my current view/component?

I guess maybe the problem is that I don’t have a proper reference to the item. I thought I was getting it via the following:

radiant.call_obj(‘stonehearth.inventory’, ‘get_item_tracker_command’, ‘stonehearth:usable_item_tracker’)
.done(function (response) {
if (self.isDestroying || self.isDestroyed) {
var itemTraces = {
“tracking_data”: {}
var tempTrace = new StonehearthDataTrace(response.tracker, itemTraces)
.progress(function (response) {
radiant.each(response.tracking_data, function (uri, item) {

and then accessing item (and finding the one I was looking for), but item can’t call .get(…). It seems to have all the item’s data, and “references” to the object://game/1234 item instances, but those are just strings.

So in the unit frame, behind the scenes there is a trace on the hearthling entity that loads up the data for the components specified in components like {"stonehearth:commands": {"commands": {}},}. Try passing in the object://game/number from the item to

(new RadiantTrace()).traceUri(ITEMOBJECTSTRING, {"stonehearth:commands": {"commands": {}}}) .progress(function(data) {});

and then see if the data object has the commands

1 Like

Awesome, thanks! I got the functionality I wanted working. I’m sure it’s an inefficient way of doing it, but it’s a way that works. I look forward to an eventual API/wiki!