How to implement a custom observer?

I’m trying to implement a test observer. Its not registering for some reason, and I can’t figure out why. Here’s the test code for the observer:

`local ArmorBatteryObserver = class()
--Called first every time
function ArmorBatteryObserver:initialize()
	-- list all the saved variables
	self._sv.entity = nil
end
--Called once on creation
function ArmorBatteryObserver:create(entity)
	self._sv.entity = entity
end
--Always called. If restore, called after restore.
function ArmorBatteryObserver:activate()
   self._entity = self._sv.entity
  self._battery_listener = radiant.events.listen(self._entity, 'stonehearth:combat:battery', self, self._on_battery)
  self._battery_listener = radiant.events.listen(self._entity, 'stonehearth:combat:entitykilled', self, self._on_battery)
end
function ArmorBatteryObserver:_on_battery(e)  
            r_entity.destroy_entity (e)
  	end
end
function ArmorBatteryObserver:destroy()
  	if self._on_battery then
  	   self._on_battery:destroy()
  	   self._on_battery = nil
  	end
end
return ArmorBatteryObserver`

I have it set up so this observer is in an ai_pack like so:

`{
   "type": "ai_pack",
   "observers" : "<mod_name>:observers:armor_battery_observer"
   ]
}`

along with the mixin for base_human

and I also have the manifest set up with the proper alias’s and mixins:

{ "info" : { "name" : "<mod_name>", "version" : 2 }, "default_locale": "en", "client_init_script" : "file(<mod_name>_client)", "aliases" : { "ai_pack:<mod_name>" : "file(ai/packs/<mod_name>_ai_pack.json)", "armor:makeshift_stone_armor": "file(entities/armor/makeshift_stone_armor/makeshift_stone_armor.json)", "data:gm_index": "file(data/gm/gm_index.json)", "kingdoms:<mod_name>" : "file(data/<mod_name>_population.json)", "male": "file(entities/<mod_name>/male/male.json)", "female" : "file(entities/<mod_name>/female/female.json)", "footman:stone_sword_talisman" : "file(/entities/weapons/stone_sword/stone_sword_talisman.json)", "furniture:crude_stone_bed" : "file(entities/furniture/crude_stone_bed/crude_stone_bed.json)", "furniture:kingly_stone_bed" : "file(entities/furniture/kingly_stone_bed/kingly_stone_bed.json)", "furniture:luxurious_stone_bed" : "file(entities/furniture/luxurious_stone_bed/luxurious_stone_bed.json)", "furniture:pleasant_stone_bed" : "file(entities/furniture/pleasant_stone_bed/pleasant_stone_bed.json)", "jobs:index" : "file(jobs/index.json)", "jobs:blacksmith" : "file(jobs/blacksmith/b_blacksmith_description.json)", "jobs:carpenter" : "file(jobs/carpenter/b_carpenter_description.json)", "jobs:cook" : "file(jobs/cook/b_cook_description.json)", "jobs:farmer" : "file(jobs/farmer/b_farmer_description.json)", "jobs:footman" : "file(jobs/footman/b_footman_description.json)", "jobs:herbalist" : "file(jobs/herbalist/b_herbalist_description.json)", "jobs:mason" : "file(jobs/mason/b_mason_description.json)", "jobs:potter" : "file(jobs/potter/b_potter_description.json)", "jobs:shepherd" : "file(jobs/shepherd/b_shepherd_description.json)", "jobs:trapper" : "file(jobs/trapper/b_trapper_description.json)", "jobs:weaver" : "file(jobs/weaver/b_weaver_description.json)", "jobs:worker" : "file(jobs/worker/b_worker_description.json)", "b_camp_standard": "file(entities/gizmos/b_camp_standard)", "b_camp_standard_ghost": "file(entities/gizmos/b_camp_standard/b_camp_standard_ghost.json)", "skeletons:humanoid:male": "file(data/rigs/bastioneer/bastioneer.json)", "skeletons:humanoid:female": "file(data/rigs/bastioneer/bastioneer.json)", "weapons:stone_sword": "file(entities/weapons/stone_sword/stone_sword.json)", "worker:outfit:2": "file(jobs/worker/worker_outfit_2)" }, "mixintos" : { "stonehearth:playable_kingdom_index" : "file(data/b_playable_kingdom_index.json)", "stonehearth:data:gm_index" : "<mod_name>:data:gm_index", "stonehearth:farmer:initial_crops" : "file(data/b_initial_crops.json)", "stonehearth:mixins:base_human": "file(data/mixins/base_human.json)" }, "controllers" : { "observers:armor_battery_observer" : "file(ai/observers/armor_battery_observer.lua)" } }

assuming no typos, what am I missing here?

Well this is way beyond me, but perhaps @Drotten would be able to help?

1 Like

Were observers changed in alpha 14? Or are there any primers on observers besides the reference docs in the stonehearth mod?

yes they were. i dont know anything about code, so i’m not much help here :sweat:, i would suggest waiting for @Drotten to reply…

1 Like

Hi @Wharp!

In the observer script, specifically the _on_battery function, has one end too many.

Also (in the same function) it’s gonna have some trouble finding something concerning r_entity, as there’s nothing with that name that exists (assuming you didn’t define something with that name in your client init script). All you have to do, though, is to change it into radiant.entities and it’s all good!

So, in short the _on_battery function should look more like this:

function ArmorBatteryObserver:_on_battery(e)
   radiant.entities.destroy_entity(e)
end

For the ai pack that you set up, it’s supposed to have a [ in there for observers. I.e. so it looks like:

{
   "type": "ai_pack",
   "observers": [
      "<mod_name>:observers:armor_battery_observer"
   ]
}

That’s all I can see from a first glance, if it still doesn’t work, do let us know! :+1:

2 Likes

Thank you! That’s definitely an issue, my real issue is that the observer is not even being loaded at all. That code is just boilerplate so that I know when it actually runs, so thanks for fixing it :slight_smile:

Odd, my ai pack does have the opening [, exactly how you have it. Dunno where that went in the post :frowning: Sorry this is such a messy issue btw, I really appreciate your help

Also, Im getting no errors, if that provides any data, so it seems to not recognize my observer is there at all

Are you using the debug tools that Radiant has up on their public github?

With that you can inspect entities and see if they have the observer on them or not (the object browser is the one with the blue lego piece as its icon). You’d also have to toggle on the private variables view to see all the observers on the entity.

1 Like

Oooo I didn’t have the updated version (where yang added the ability to view private variables) :smiley: I got that and it is indeed the case that the observer isnt loaded. How exactly do you make an observer attach to all citizens at their creation?

You should already be doing that when you mixinto your own base_human. Just for clarification, the file looks something like this?

{
   "entity_data": {
      "stonehearth:ai_packs": {
         "packs": [
            "<mod_name>:ai_pack:<mod_name>"
         ]
      }
   }
}

Yes, it does. <mod_name> is the actual name of the mod though (I don’t want to release that info yet).

That’s alright, just trying to figure out why it’s not working.

Oh wait… in the manifest and under mixintos, I wonder if you can use the alias of the file that you want to override. I haven’t actually done that myself, could you instead try to use the file’s path instead?

For base_human it would be something like this:

   "stonehearth/mixins/base_human/base_human.json": "file(data/mixins/base_human.json)"

Yes, so I tried each of these lines, one at a time:

“stonehearth/mixins/base_human/base_human.json”: “file(data/mixins/base_human.json)”
“file(stonehearth/mixins/base_human/base_human.json)”: “file(data/mixins/base_human.json)”
“file(…/stonehearth/mixins/base_human/base_human.json)”: “file(data/mixins/base_human.json)”
“stonehearth:mixins:base_human”: “file(data/mixins/base_human.json)”

I noticed in the stonehearth manifest, alias “stonehearth:mixins:base_human” points to file (mixins/base_human) not mixins/base_human/base_human.json

Hi there,
Try:
"/stonehearth/mixins/base_human/base_human.json": "file(data/mixins/base_human.json)"
It has to start with a / for something outside of your mod.

This one should work too:
“stonehearth:mixins:base_human”: “file(data/mixins/base_human.json)”

You can check to see if it is working with debugtools. Select a hearthling and then click on their uri. It will then show you the json for that hearthling and you can see if your ai pack is showing up in the list.

Thanks
-Yang

Hey Yang :slight_smile: Okay, so I tried that yet it’s still not working. I’m at my wit’s end and I’m pretty sure its easier to diagnose if you have the mod, so here it is:
mod.zip (2.9 MB)

The observer lua file is a bit of a mess atm, as I’ve was trying to find some way to know if it’s present or not (before I had the update debugtools)

Hi @Wharp
The file ai/observers/armor_battery_obeserver.lua
has an extra “e” in it
should be ai/observers/armor_battery_observer.lua

-Yang

2 Likes

Also, you can probably use the “stonehearth:mixins:base_human”: “file(data/mixins/base_human.json)” now
It makes it so you don’t have to specify a long path and also will work even if we change what file “stonehearth:mixins:base_human” points to in the future.
-Yang

1 Like

:sob: :sweat_smile: :joy: Thanks so much guys, I feel so silly now, I overlooked that so many times!

1 Like

the worst enemy of coders/programmers is and always will be typo’s :wink:

2 Likes