Accessing a RUBY class through events

I apologise in a advance for vagueness I would like to get answers without giving away too much, but if I am too vague ask specifics if you think you can help.

Alright well I am using Ace and I am not a complete code noob but my lack of knowledge of both RPG maker and RUBY itself has caught up with me

I have a ruby class to represent entites I would like to have a map presence I figure the best way to go about this is is using events:

For arguments sake lets say my class is an NPC (

I plan to have a max of these of about 10 and the "Active" number will change when they are not active :

So these NPCs will each have their own stats and one of the is a bool called "active" when active is false I want him to not display.

So a few questions I need help address is in correlation to the engine itself and my code:

Should I just init these 10 "NPCs" in a custom game manager class (which I have built to track score already) all at once or should I use the events and self flags (if the later can an example of a plausible way to do that) so that they are each initialized as needed. If I do either one of those ways will I face a likelyhood that they will reinit at strange times. (my game manager inits at game run and then again at new game).

I tired to figure a way to do this looking through the engine scripts and coul not figure how objects with their own class exsisted within it (This is where I think its a lack of RUBY know how)  the best i can decern is they are in a enum? and not an array? ........

I went with my own "NPC" class becuase I dont want to have my hands tied with the event system, but ultimately should I just build each "NPC" inside the event using script rather than having my class?

Maybe I am just rambling and trying to clear my head, maybe I have legitimate questions in there, but any help, guidance, suggestions, critcism  will not at all upset me.
Hi, I'm retired, but I think I can help you out here a bit.

Well, you could try linking an Event to an NPC class?

Just create events like you normally would.

Create a new Event Page with a script command.

npc = NpcManager.create_npc(npc_id)event = get_character(@event_id) event.npc = npc# or something of that natureOf course You'll need to have something like an NpcManager module/class something along the lines of:

module NpcManager def self.create_npc(npc_id) NPC.new(npc_id) endendOh and you need to add an accessor to the Game_Event for an npc object

class Game_Event attr_accessor :npcend Then when you need something from the event's NPC object you could call

event = get_character(@event_id)event.npcNow I have no idea why you would want this anyway, it seems like an interesting idea though.

EDIT:

*facepalm*

I make a come back, and then discover the topic is over 14 days old.

THANKS A LOT FENIX, THANKS ALOT!
It's ok, thats good info to know for anyone who didn't know.
Its a bit hackish and fragmented though.

I've decided to put it all together into a usable script (I haven't tested it...)

Spoiler
Code:
## npc_demo_script.rb#   dc 11/11/2013#   dm 11/11/2013#   by IceDragon ([URL="http://forums.rpgmakerweb.com/index.php?/user/36-icedragon/"]http://forums.rpgmakerweb.com/index.php?/user/36-icedragon/[/URL])#   vr 1.0.0# This script is a demonstration of how one would go about adding an Object# to an Event during runtime.### Heart of the Npc system this class doubles as the originalclass Npc  ##  # @return [Integer]  attr_reader :id  ##  # @return [String]  attr_accessor :name  ##  # @return [Hash<String, String>]  attr_accessor :meta  ##  # @return [Hash<Integer, Integer>]  attr_accessor :variables  ##  # @return [Hash<Integer, Boolean>]  attr_accessor :switches  def initialize(id)    @id = id    @name = ''    @meta = {}    @variables = {}    @switches = {}    @switches.default = false    @variables.default = 0  end  @npcs = {}  class << self    alias :org_new :new    ##    # does this npc exist?    # @param [Integer] id    def exist?(id)      @npcs.key?(id)    end    ##    # Basic CRUD functions, without the "U"    ##    # Creates and returns a new Npc object    # if a block is provided, the new Npc object will be yieled    # NOTE* this function does not check for existing npc's    # use cin if you unsure if the Npc was already created    # @param [Integer] id    def create(id)      npc = @npcs[id] = org_new(id)      yield npc if block_given?      return npc    end    ##    # retrieve    # A KeyError is raised if the coresponding Npc cannot be found    # @param [Integer] id    def find(id)      @npcs.fetch(id)    end    ##    # Create if nil    # this is a personal function, which behaves like the short circuit equal    # (||=)    # @param [Integer] id    def cin(id)      exist?(id) ? find(id) : create(id)    end    ##    # delete, removes an Npc by id    # @param [Integer] id    def delete(id)      @npcs.delete(id)    end    ##    # @param [Hash<Symbol, Object>] contents    def make_save_contents(contents)      contents[:npc] = @npcs    end    ##    # @param [Hash<Symbol, Object>] contents    def extract_save_contents(contents)      @npcs = contents[:npc] || {}    end    ##    # so instead of Npc.create, we can use Npc.new, which will register    # the newly created Npc object.    # if you don't want it to be registered, call Npc.org_new instead    alias :new :create  endend### open the DataManager module for editing# The DataManager is reponsible for the game's save data, so we add# a bit of content for the (contents) of the save data from our Npc class.module DataManager  # open the DataManager's singleton class for editing  class << self    ##    # alias the original make_save_contents and extract_save_contents    alias :no_npc_make_save_contents :make_save_contents    alias :no_npc_extract_save_contents :extract_save_contents    ##    # fills (contents) with save data    # @param [Hash<Symbol, Object>] contents    # @return [Void]    def make_save_contents(contents)      no_npc_make_save_contents(contents)      Npc.make_save_contents(contents)    end    ##    # Retrieves the save data from the given (contents)    # @param [Hash<Symbol, Object>] contents    # @return [Void]    def extrat_save_contents(contents)      no_npc_extract_save_contents(contents)      Npc.extract_save_contents(contents)    end  endend### Nothing too fascinating here, I've simply added the npc attribute# to Game_Eventclass Game_Event  ##  # @return [Npc]  attr_accessor :npc  ##  # does this event have an npc?  # @return [Boolean]  def npc?    !!@npc  endend%Q(  Now what?  well you can create a new Event, and add a [Script Command] with:    ev = get_character(@event_id)    ev.npc = Npc.cin(1)  This will create a, or retrieve Npc #1 and set it for the current event.  Now further down you can call another [Script Command] with:    ev = get_character(@event_id)    npc = ev.npc    npc.name = "Some Guy"    npc.variables[1] = 100    npc.switches[10] = true    npc.meta["some_meta_data"] = "value"  It should be noted that the meta data doesn't actually enforce any String  Type(s), so its perfectly fine to use Symbol(s) or Integer(s) or whatever.  Now, I don't think this is useful in anyway :x  Maybe someone could adapt it and make it a full fledged script or something.)
def npc?


!!@npc


end

Is there something unique about !! ?
! = not


!! = buffer/(not not)


its equivalent to something like


Boolean(obj)


or obj.to_bool


After all, I don't want the #npc? method returning the actual Npc
Oh, that's a convenient way to do things. I always ended up using a separate instance variable to store a boolean value.


Seems like something I would avoid though just because someone looking at the script might also be wondering what's going on lol
Well by convention: *? methods should return a Boolean.

🙂 By knowing that you could figure out that whatever in the method evaluates to some Boolean value.

But if its too complicated it doesn't hurt to add a bit of rdoc/yard markup at the top of the method:

@return [boolean]

but if !! looks too hackish you could add to_bool to Object

Code:
class Object  def to_bool    !!self  endendclass Game_Event  ##  # does this event have an npc?  # @return [Boolean]  def npc?    @npc.to_bool  endend
or Boolean to Kernel
Code:
module Kernel    def Boolean(obj)    !!obj  endendclass Game_Event  ##  # does this event have an npc?  # @return [Boolean]  def npc?    Boolean(@npc)  endend
Or just check if its not nil?
Code:
class Game_Event  ##  # does this event have an npc?  # @return [Boolean]  def npc?    [email protected]?  endend
But !! looks a bit cleaner in my opinion, 😛 but that's just me, I change styles by the season.

Original thread: https://forums.rpgmakerweb.com/threads/accessing-a-ruby-class-through-events.18997/