Adding custom commands to messages

Fiona

Rookie
Member
Joined
Nov 5, 2017
Posts
9
Hiya, I've got a little script that injects some behaviour into the PokeBattle_Trainer class.

All good, but I wanted to add some behaviour to the messaging system. I dug into the Kernel.pbMessageDisplay method and found all the regex that does the command replacement. I added my additional behaviour and it's all cool. But I don't like that I had to edit the Essentials code. (I can't share it easily!)

Is there a mechanism I'm missing to add my own commands to the messaging system so my script can be kept siloed?

I suppose I could alias pbMessageDisplay, do the substitution and pass it back along to the original. But I think that would only work for my script and I couldn't do the same thing for any additional scripts. If I'm wrong about this please tell me, I only started using Ruby yesterday.

Thanks for any help.
 

Luka S.J.

Wastage of Time
Member
Joined
Mar 27, 2017
Posts
97
It all really depends on what behaviour you want to add to Kernel.pbMessageDisplay. If it's something simple, like passing through a scene update, the function accepts blocks that you can give it
Code:
Kernel.pbDisplayMessage(*args){ var = "string" }
For more complex stuff, you will have to alias it or completely overwrite the function, if you want to make it standalone.
 

Fiona

Rookie
Member
Joined
Nov 5, 2017
Posts
9
I want to add additional substitutions to the message text. I don't think passing it a block would help.

Yeah I said I suppose I could alias it and add the functionality as a sort of text pre-processor, but I'm not sure how to achieve that in such a way that it wouldn't be overriden by doing the same trick in a different script. I think maybe I don't fully understand function aliases in Ruby.

[edit]

Okay I just tried experimenting with alias_method and I'm getting errors about Kernel not being a class. So I guess I now have a new question: How do I successfully alias a method on the Kernel class without directly overriding it in it's entirety?
 
Last edited:

Luka S.J.

Wastage of Time
Member
Joined
Mar 27, 2017
Posts
97
It goes a little something like this:
Code:
# reference to the Kernel module
module Kernel
  # references self as class
  class << self
    # doesn't alias if already aliased
   unless Kernel.respond_to?(:pbMessageDisplay_Old)
      # aliases to the new name
      alias pbMessageDisplay_Old pbMessageDisplay
      # creates new functionality
      def pbMessageDisplay(*args)
        args[1] = "I like shorts! They're comfy and easy to wear!"
        # reference back to the original function
        return pbMessageDisplay_Old(*args)
      end # def end
    end # unless end
  end # class end
end # module end
This code is a bit bloated though to avoid double aliasing and "Stack level too deep" errors (which mainly pop up when soft-resetting).
 

Fiona

Rookie
Member
Joined
Nov 5, 2017
Posts
9
Thanks a lot for that, that "class << self" singleton thing is a fun little bit of Ruby magic. It's a shame it can't be re-aliased but meh, it'll be good enough for now. :)

I reallly appreciate your help!
 

Marin

Administrator
Administrator
It's a shame it can't be re-aliased

If with this you mean not being able to alias this same method again -- you can. Just make sure to call the second alias something different. You'll end up with a Stack level too deep otherwise.
 
Top