How do I call this lua function from javascript?

In services/server/player/player_service.lua there are a number of functions. One of these functions, get_host_player_id_command is called by ui/root/js/stonehearth/stonehearth_client.js as follows:

radiant.call_obj('stonehearth.player', 'get_host_player_id_command')
            .done(function(e) {
               self.gameState.hostPlayerId = e.player_id;
            });

I try to do something similar for a different function in there, but I can’t seem to do it:

radiant.call_obj('stonehearth.player', 'are_player_ids_hostile', playerID, self._playerID)
                .done(function (e) {
                    self._isHostile = e.response;
                });

Looking more closely at the lua, I can see the function I’m calling should simply be returning a boolean, while the other function includes a response parameter that then gets set in the function, rather than directly returning a value: response:resolve({ player_id = _radiant.sim.get_host_player_id() })

Does radiant.call_obj(...) only work for functions that are set up in this particular way? It wants to return a deferred object that, in the case of my function, never gets resolved (I set a breakpoint on the relevant line, and it never breaks).

So does this mean I have to create my own lua function that calls this lua function and packages the result in a way that radiant.call_obj(...) can understand? If so, how do I do that (i.e., namespace, manifest, mixin, etc.)? And if not, how do I proceed?

1 Like

are_player_ids_hostile is not “whitelisted” in the stonehearth/manifest.json and therefore (likely) not available for calling via call_obj. Not sure if a mixinto in the manifest would help here. I’d go with a self-made method callback and just radiant.call.

See also Radiant.call versus radiant.call_obj?

3 Likes

So I’ve tried doing that now, but still not getting a response on my function call (and when stepping through the js, I verified my arguments, playerID and self._playerID, are valid strings). Here’s the js:

radiant.call('unitframeplus:are_player_ids_hostile', playerID, self._playerID)
                .done(function (e) {
                    self._isHostile = e.are_hostile;
                });

Here’s the lua:

local UnitFramePlusCallHandler = class()

function UnitFramePlusCallHandler:are_player_ids_hostile(session, response, party_a, party_b)
	response:resolve({ are_hostile = stonehearth.player:are_player_ids_hostile(party_a, party_b) })
end

And here’s the function reference in my manifest:

"functions": {
    "are_player_ids_hostile": {
      "controller": "file(call_handlers/unit_frame_plus_call_handler.lua)",
      "endpoint": "server"
    }
  }

Is there something obviously wrong there that I’m not seeing?

If that’s your whole file, you’re missing the return UnitFramePlusCallHandler at the end.

Check the log - it should probably complain about not finding the function.

1 Like

That did it, thank you very much! I’m glad it was just some obvious thing that I couldn’t see due to inexperience. I hope that they expand the modding guide to include scripting soon.

3 Likes