Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

renetta96

Add 'droppedtarget' event

Recommended Posts

renetta96    9

 Hi devs,

I noticed that there is no good way to track a combat target is dropped in Don't Starve. In Don't Starve Together, a event 'droppedtarget' is fired, however in the same function in Don't Starve, it just sets target to nil. 

Could you add this event please?

Share this post


Link to post
Share on other sites
CarlZalph    5340
3 hours ago, renetta96 said:

I noticed that there is no good way to track a combat target is dropped in Don't Starve. In Don't Starve Together, a event 'droppedtarget' is fired, however in the same function in Don't Starve, it just sets target to nil. 

Could you add this event please?

You can make your own event by creating a proxy metatable hook for the combat component.

comp = {}
comp.target = 25

local proxy = {}
mt = {
  __index = function(t, k)
    if k == "target"
    then
      t = proxy
    end
    return rawget(t, k)
  end,
  __newindex = function(t, k, v)
    if k == "target"
    then
      t = proxy
      if v == nil and rawget(proxy, k) ~= nil
      then
        print("Dropped target!", rawget(proxy, k), v)
      end
    end
    rawset(t, k, v)
  end,
}

proxy.target = comp.target
comp.target = nil
setmetatable(comp, mt)

comp.target = 52
comp.target = nil
comp.target = nil
comp.target = 23
comp.target = 23
comp.target = nil

Outputs:

Dropped target!	52	nil
Dropped target!	23	nil

To make it more robust with other mods that also using metatables, then you'll have to make sure that you get the metatable and use pre/post function hooks to keep things safe for mod conflicts.  But I'll leave this up to you to do.

Share this post


Link to post
Share on other sites
renetta96    9

Looks nice, thanks! This is a bit advanced in Lua for me. In my case I just need to check dropped target on some prefabs and don't require real time event, so I just do a periodic task of 1 sec to check target is nil :p Anyway, really appreciate the knowledge!

Share this post


Link to post
Share on other sites