Listening to other events

Hi,

currently, I am trying to listen to other events. For example'stonehearth:blueprint_added' but it doesn’t work. The only event I can listen to is 'radiant:init' like so:

analyzer = {}

radiant.events.listen(analyzer, 'radiant:init', function(e)
    radiant.log.write('analyzer', 0, 'init')
end)

return analyzer

within the console I can then see a message:

 | server |  0 |                             mod analyzer | init

But with any other event, nothing happens. For example, I also wanted to use the hourly event but how do I call it properly, I have tried 'stonehearth:time:hourly', 'stonehearth:calendar:hourly', 'stonehearth:hourly' and 'radiant:hourly' because inside the docs it’s not pretty clear how to call the specific event. Also

triggered every in-game hour by the calendar service

Inside the calendar service, I can’t find any function which triggers the event. What do I missing?

Regards,
LifeArtist

set_interval(reason, duration, fn, repeat_duration)

reason is a string describing the timer.
duration is a time in string format, like “1h” for 1 hour or “1d22m” for 1 day and 22 minutes.
fn is the function that will be called on that interval.
repeat_duration is an unused argument, simple repeat the duration argument for the sake of not being empty.

For example, on my season mod, I used this

self._sv.timer = stonehearth.calendar:set_persistent_interval(
  "SeasonModComponent update",
  "30d",
  radiant.bind(self, '_on_check_season'),
  '30d'
)

It calls my function _on_check_season() every 30 days.

Great Thanks! Worked like a charm.
This is my line:

local timer = stonehearth.calendar:set_persistent_interval("analyzer mod", "1h", update_analyzer, "1h")

So my guess would be that the event hourly doesn’t even exists.

One question which I still have is what is the entity or also called object inside the function:
function events.listen(object, event, instance, fn, opt_description)

  radiant.events.listen(entity, 'stonehearth:buff_added', function(e)
        --e has the arguments passed into the trigger
        if e.uri == uri then
           return cb()
        end
     end)

Because I still can’t listen to other events except for radiant:init.
In a lua script I have seen that the first parameter was the class from the lua file so for me it would be Analyzer. But in your mod you are using radiant :sweat:. The second parameter is just the name of the event. The third is an instance what I don’t understand because in the example which I have quoted above they don’t even have referenced an instance. I know this should be the instance of the class if you have one but what if not? Yeah, and the rest is self-explanatory.

Regards,
LifeArtist

There is multiple ways to use the same function.

For in-game objects (mobs, items) we use:
radiant.events.listen(entity, event, function)

First argument is the entity related to the event (the same event could be fired for another, undesired entity, I guess that’s why you need to specify one)
Second is the event itself. Like "stonhearth:kill_event"
Third is the function called when it happens.

I’m not sure which mod you looked. In the archipelago, I have one that is this:
radiant.events.listen_once(radiant, ‘radiant:required_loaded’, archipelago_biome, archipelago_biome._on_required_loaded)
Basically, the object we are listening, the event, then my class, and my function to run when that happens.

Ah okay, that cleared things up. This event works.

So when I now try to listening to when the party size changed. I would write something like this radiant.events.listen_once(stonehearth, 'stonehearth:party:size_changed', Analyzer, Analyzer.storage_changed). But this doesn’t work. And I think because of the entity which in my case I have used stonehearth.

The event get triggered with this line: radiant.events.trigger_async(self._entity, 'stonehearth:party:size_changed', {size = self._sv.party_size}) so for a working listening, I would need to replace stonehearth with the value of self._entity but what is that? I can’t see an assignment within the party_component.lua file (...\mods\stonehearth\components\party\party_component.lua)

sorry for so much trouble -.-"

I once needed to know if the player had soldiers or not, I started trying to listen for that event too. I also failed at that.

What I did that worked for my case (not sure if it will help in your case) was use this code:

local population = stonehearth.population:get_population(ctx.player_id)
local party = population:get_party_by_name('party_1')
local party_component = party:get_component('stonehearth:party')

return party_component:get_party_size()

With this I can have the number of military units that player (or even other factions like goblins) have, in that specific party. Not sure if there was an easier way to do it.

I think there is not a better way of doing it. I found a snippet inside population_faction.lua inside stoneheard mod which listen the party size_changed event by looping through all parties and binding the listener onto each party.

  for party_name, party in pairs(self._sv.parties) do
     local party_component = party:get_component('stonehearth:party')
     self._sv.party_member_counts[party_name] = party_component and party_component:get_party_size() or 0
     self._party_listeners[party_name] = radiant.events.listen(party, 'stonehearth:party:size_changed', self, function(self, e)
           self._sv.party_member_counts[party_name] = e.size
           self.__saved_variables:mark_changed()
        end)
  end

later I will try to bind like this to the event and see if this function.