Hime-Works / Requests

Bug reports and requests that may require longer discussions and is not suitable to leave on the blog
http://himeworks.com/
GNU General Public License v2.0
7 stars 9 forks source link

Shop Callbacks #16

Open HimeWorks opened 11 years ago

HimeWorks commented 11 years ago

This feature will be added to the Shop Manager http://himeworks.wordpress.com/2013/02/22/shop-manager/

"Shop Callbacks" allow you to assign custom logic to a variety of "triggers" that occur in the shop scene. You can assign methods to each trigger and the method will be called when a trigger is fired. A list of triggers include

This is based on "event driven programming" where I provide a number of events (which I call "triggers" to avoid confusing it with RM's notion of an event) and devs that are proficient with ruby can define methods and assign them to each trigger. For example, buying an item might set an instance variable to true in the calling event, which allows you to use it for conditional checking later on after the shop scene closes and the event resumes execution.

Roguedeus commented 11 years ago

This is really slick. I started to learn to code with an interactive merchant system I wrote for Never Winter Nights, way way way back... It used a C like scripting language called NWScript.

I have spent the last 4 days diving head first into Ruby and RGSS3, primarily Yanfly's Battle Engine and Popups, in support of that Element EX script... I have nearly re-written a fifth of the thing. And learning TONS in the process.

Ruby is good stuff! I am beginning to get why so many people like coding in Ruby. The way you can just throw together an array piecemeal and spit it back out on the fly is so freeing compared to Java and C#... Shame its so performance sluggish though.

Anyway, I hope to make good use of your 'deeper' scripts, like this one, as I get further into my project.

HimeWorks commented 11 years ago

I've decided to go with "Shop" and "Shop Good" callbacks. You can assign handlers for each good individually, as well as the shop itself.

For example, if you want a shop to do something when you buy an item, you would assign a method to the shop's "buy handler". Alternatively, if you want the shop to do something when you buy a specific shop good, you would assign a method to the shop good's "buy" handler.

The plan is to make script calls like

set_shop_handler(:buy, :method_name)
set_good_handler(:buy, :method_name)

Here's a little demo of how the callbacks will work (just run it in an interpreter)

module Callback

  def self.set(method)
    @handler = method
  end

  def self.call
    @handler.call
  end
end

class Test

  def initialize
    @val = 0
  end

  def increment
    @val += 1
  end

  def run
    Callback.set(method(:increment))
    Callback.call
    p @val
    Callback.call
    p @val
  end
end

a = Test.new
a.run

This demo shows how you can have an object pass a reference of its own method to another object and have the other object call it.