Question about call_handler luas

Is it possible to mixinto/overide these?

Say, for example, I wanted to change the farming_call_handler, specifically:

This function

function FarmingCallHandler:choose_new_field_location(session, response)
stonehearth.selection:select_designation_region(stonehearth.constants.xz_region_reasons.NEW_FIELD)
:set_max_size(11)
:use_designation_marquee(Color4(55, 187, 56, 255))
:set_cursor(‘stonehearth:cursors:zone_farm’)

:set_find_support_filter(stonehearth.selection.valid_terrain_blocks_only_xz_region_support_filter({
grass = true,
dirt = true
}))

  :done(function(selector, box)
        local size = {
           x = box.max.x - box.min.x,
           y = box.max.z - box.min.z,
        }
        _radiant.call('stonehearth:create_new_field', box.min, size)
                 :done(function(r)
                       response:resolve({ field = r.field })
                    end)
                 :always(function()
                       selector:destroy()
                    end)
     end)
  :fail(function(selector)
        selector:destroy()
        response:reject('no region')
     end)
  :go()

end

More specifically, this part:

:set_find_support_filter(stonehearth.selection.valid_terrain_blocks_only_xz_region_support_filter({
grass = true,
dirt = true
}))

to

:set_find_support_filter(stonehearth.selection.valid_terrain_blocks_only_xz_region_support_filter({
rock = true,
grass = true,
dirt = true
}))

To, theoretically, be able to place a farm on stone if you’re using the mod that adds this functionality.

Should work fine overriding it. You need to copy the whole file though, not only the part that is changed.
A mixinto would not work as that is only for json files.

1 Like

Unless you’re monkey patching it, which would probably be a better thing to do.

1 Like

Reading through your posts on monkey patching was what inspired me to look into / try it, though I’m not very code / lua savy, so not sure I would trust my ability to try to do it

The basics are always the same:


function mod:patch()
   local victim = radiant.mods.require('stonehearth.path.to.file.using.periods')
   victim.method = function(self, args)
      -- Code goes here.
   end
end

radiant.events.listen(radiant, 'radiant:required_loaded', mod, mod.patch)

The signature if patch needs to match your original method’s function, plus the another, new argument at the first position called self (because of the . <-> : difference). You could use function victim:method(args) instead too, but I prefer the assignment. Not sure if there’s actually any difference, I’d expect the non-assignment way to be simple syntax sugar.

But your method signature would be function(self, session, response). Put it in your mod’s server_init-file and make sure that the patch method is a member of whatever class you have in there. Might even work without that class stuff and just a method; it’s been too long since I’ve coded for SH.

1 Like
Like this?
   local victim = radiant.mods.require('stonehearth.call_handlers.farming_call_handler')
   victim.method = function(self, args)
	function FarmingCallHandler:choose_new_field_location(session, response)
	   stonehearth.selection:select_designation_region(stonehearth.constants.xz_region_reasons.NEW_FIELD)
		  :set_max_size(11)
		  :use_designation_marquee(Color4(55, 187, 56, 255))
		  :set_cursor('stonehearth:cursors:zone_farm')
		  :set_find_support_filter(stonehearth.selection.valid_terrain_blocks_only_xz_region_support_filter({
				rock = true,
				grass = true,
				dirt = true
			 }))

		  :done(function(selector, box)
				local size = {
				   x = box.max.x - box.min.x,
				   y = box.max.z - box.min.z,
				}
				_radiant.call('stonehearth:create_new_field', box.min, size)
						 :done(function(r)
							   response:resolve({ field = r.field })
							end)
						 :always(function()
							   selector:destroy()
							end)
			 end)
		  :fail(function(selector)
				selector:destroy()
				response:reject('no region')
			 end)
		  :go()
	end
   end
end

Nope, not like that at all. Please format your code (using three backticks: ```) next time so it’s more readable… what I can decipher:

  • You’re not overriding the method. victim.choose_new_field is what you want, not… that. victim.choose_new_field = function(self, session, response). The next line can be omitted. You need to re-define the function (literally assign another one).

Sorry about that!

Ohhh… sorry, @RepeatPan, I’ve only just started learning lua. Should learn to start simpler >.< Thank you for your patience!

   local victim = radiant.mods.require('stonehearth.call_handlers.farming_call_handler')
   victim.choose_new_field = function(self, session, response)

[new code to replace the stuff I am changing here?]

   end
end

radiant.events.listen(radiant, 'radiant:required_loaded', mod, mod.patch)

^ So more like this?

1 Like

Yepp. You can throw in a log statement or two to see if it’s working as expected.