Jump to content

Detect that the player is using an item


GargamelLeNoir

Recommended Posts

I've been searching a lot but I just can't find how to react to the player using an item. No event for that. I resigned myself to do it the ugly way and create a function reacting to OnTick that would check if the player's using an item, but even that way I failed. There is  a function "getUsedItemsOn" that may give me the used item, but no "isItemBeingUsed", so I get a heap of errors instead of results.

 

tl;dr I can't find something like an OnItemUsed event or isItemUsedOn() function

 

My use case is that I want to see that the player is reading a newspaper, magazine or such and display a fluff related text that I would randomly pick in a file.

 

Thanks in advance for any help!

Link to comment
Share on other sites

You can hook the ISReadABook timed action (which I believe is used for all literature reading.)

 

Here are some examples:

-- Basic hook example, multiple mods can hook the same function this way and potentially still be compatible-- Future proofed against argument and return value changeslocal oldISReadABook_perform = ISReadABook.perform;function ISReadABook:perform(...)    print("My hooked code here");    return oldISReadABook_perform(self, ...);end-- Hook example where the original function is called firstfunction ISReadABook:perform(...)    local ret = oldISReadABook_perform(self, ...);    print("My hooked code here");    return ret;end-- Hook example where arguments are checkedif table.pack == nil then    table.pack = function(...)        return { n = select("#", ...), ... }    endendfunction ISReadABook:perform(...)    local ret = oldISReadABook_perform(self, ...);        local args = table.pack(...);    if args[1] == "foobar" then        print("My hooked code here");    end    return ret;end

You might want update() instead of perform(), depending. I just used that as an example. If you want specific to newspaper/etc results then you can check self.item

 

Clarification for anyone confused about where self comes from: function foo:bar() in lua is just syntax sugar for function foo.bar(self) and so you're saving a copy of the old function, then overriding it with a new function, and then calling the old function and passing along self to keep the pseudo-OOPness working.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...