Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Create new RPG Class and Add Objects.

Recently I have an idea to create a new RPG Class, to make a script, so I wonder how to make a new RPG Class, taking the example of the RPG::Item class.

I want to create attributes for this new class, exactly like the RPG::Item.

An idea about how to make this?
 
mephisto":sa0h9smr said:
Recently I have an idea to create a new RPG Class, to make a script, so I wonder how to make a new RPG Class, taking the example of the RPG::Item class.

I want to create attributes for this new class, exactly like the RPG::Item.

An idea about how to make this?

It all depends on exactly what you want to do. I mean, you can do all sorts of stuff to mimic that. If you check out the game time class in the World Settings kit I'm working on (lookie at my signature), you'll see that I essentially replicated the ruby Time class, with a few differences. All you have to do is create it so that the information you want to be accessible is accessible in the same way as it was in the original.
 
I mean, to create a new fully RPG Class, only for read stuffs, for example, i want to create a new Rpgclass called Treasures.

Code:
 

module RPG

  class Treasure

    attr_accessor :id

    attr_accessor :name

    def initialize

      @id = 0

      @name = ""

    end

  end

end

 

but I want to know how to add the objects to this new class, and after, how to read them.
 
We typically put attr_accessor at the top of the class.

You don't really add objects "to" a class, you create objects based on a class... (I assume that's what you meant.)

In the scene, or section of code where you need a treasure object...

Code:
treasure1 = RPG::Treasure.new

then, after you initialize the object, you can access it's parameters with:

Code:
 treasure1.id

treasure1.name

If you need to access it outside of the method where you created it, but within the same object, use @treasure1 for both the initialization (instance), and the reference.

make sense?
 
If you are making data structure objects, I suggest making a Struct class (you can add methods if you wish later to this class), creating a $data array to hold this and finally to save time, add save_data/load_data methods like so:
[rgss]Treasure = Struct.new:)name)
 
# This block auto-sets the @id as well as adds the treasure to the $data_treasures list
class Treasure
  attr_accessor :id
  alias_method :treasure_init, :initialize
  def initialize(*args)
    @id = $data_treasures.size
    # Calls struct#initialize and sets other instances
    treasure_init(*args)
    # Saves treasure to $data_treasures
    $data_treasures[@id] = self
  end
  # You can add other methods here
end
 
if true
  $data_treasures = [nil]
  Treasure.new('Treasure 1')
  Treasure.new('Treasure 2')
  # ...
 
  save_data($data_treasures, 'Data/Treasures.rxdata')
else
  $data_treasures = load_data('Data/Treasures.rxdata')
end
[/rgss]

If you wanted to give your Treasure class other instances, just add them on this line:
Code:
Treasure = Struct.new(:name)

Like say you wanted to add a description:
Code:
Treasure = Struct.new(:name, :description)

Then in your Treasure.new('Treasure 1') lines, you just pass another block for description:
Treasure.new('Treasure 1', 'Description 1')



If you would like to see a bigger example of this, it's a snippet from my Triple Triad system (this is incomplete. The complete code is too big for a single post, but this is the general idea)
[rgss]#==============================================================================
# ** TripleTriad::Card
#==============================================================================
 
TripleTriad::Card = Struct.new:)name, :n, :e, :s, :w, :element, :price)
 
class TripleTriad::Card
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_accessor :id
  attr_accessor :deck_id
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  alias_method :seph_tt_ttcard_init, :initialize
  def initialize(*args)
    seph_tt_ttcard_init(*args)
    @id = $data_tt_cards.size
    $data_tt_cards[@id] = self
  end
  #--------------------------------------------------------------------------
  # * Rating
  #--------------------------------------------------------------------------
  def rating
    return TripleTriad.card_rating(self)
  end
  #--------------------------------------------------------------------------
  # * Deck Name
  #--------------------------------------------------------------------------
  def deck_name
    return $data_tt_decks[deck_id].name
  end
  #--------------------------------------------------------------------------
  # * Bitmap
  #--------------------------------------------------------------------------
  def bitmap(suffix = false)
    suffix = suffix ? ' - r' : ''
    fn = "Graphics/Triple Triad Cards/#{deck_name}/"
    return RPG::Cache.load_bitmap(fn, name + suffix, 0)
  end
end
 
#==============================================================================
# ** Triple Triad Preset Deck/Card Builder
#==============================================================================
 
if TripleTriad::Decks::Run_Builder
 
#--------------------------------------------------------------------------
# * Create Deck/Card List
#--------------------------------------------------------------------------
$data_tt_cards = [nil]
 
#--------------------------------------------------------------------------
# * Triple Triad Deck : Final Fantasy VII
#--------------------------------------------------------------------------
if TripleTriad::Decks::Include_Deck_FFVII   = true
 
TripleTriad::Card.new('Cockatrice', 6, 2, 2, 3, 0, 100)
TripleTriad::Card.new('Dorky Face', 2, 5, 2, 4, 7, 100)
TripleTriad::Card.new('Hedgehog Pie', 5, 2, 2, 4, 0, 100)
TripleTriad::Card.new('Mandragora', 4, 2, 3, 1, 0, 100)
TripleTriad::Card.new('Mu', 1, 2, 3, 4, 0, 100)
TripleTriad::Card.new('Roulette Cannon', 1, 2, 6, 2, 0, 100)
TripleTriad::Card.new('Slalom', 2, 6, 2, 1, 0, 100)
TripleTriad::Card.new('Spiral', 6, 2, 1, 1, 0, 100)
TripleTriad::Card.new('Sword Dance', 3, 3, 2, 2, 0, 100)
TripleTriad::Card.new('Touch Me', 2, 2, 3, 3, 0, 100)
TripleTriad::Card.new('Warning Board', 1, 5, 5, 1, 0, 100)
TripleTriad::Card.new('2-Faced', 2, 6, 1, 6, 0, 200)
TripleTriad::Card.new('Bagrisk', 7, 2, 3, 2, 0, 200)
TripleTriad::Card.new('BombGrenade', 1, 4, 5, 5, 1, 200)
TripleTriad::Card.new('Grangalan', 5, 3, 5, 1, 0, 200)
TripleTriad::Card.new('Grunt', 3, 6, 2, 2, 0, 200)
TripleTriad::Card.new('Guard System', 1, 7, 3, 1, 0, 200)
TripleTriad::Card.new('Icicle', 4, 4, 1, 4, 2, 200)
TripleTriad::Card.new('Jayjujayme', 2, 4, 4, 3, 0, 200)
TripleTriad::Card.new('Joker', 2, 3, 3, 7, 0, 200)
TripleTriad::Card.new('Snow', 1, 3, 7, 3, 2, 200)
TripleTriad::Card.new('Unknown', 3, 4, 3, 4, 1, 200)
TripleTriad::Card.new('8-Eye', 2, 6, 3, 6, 0, 350)
TripleTriad::Card.new('Cactuar', 1, 7, 5, 4, 0, 350)
TripleTriad::Card.new('Death Claw', 4, 3, 3, 7, 0, 350)
TripleTriad::Card.new('Golem', 6, 3, 1, 6, 0, 350)
TripleTriad::Card.new('Gremlin', 5, 5, 2, 5, 0, 350)
TripleTriad::Card.new('Ho-chu', 2, 4, 5, 6, 0, 350)
TripleTriad::Card.new('Jemnezmy', 6, 1, 7, 3, 0, 350)
TripleTriad::Card.new('Jumping', 7, 2, 5, 2, 0, 350)
TripleTriad::Card.new('Magic Pot', 5, 7, 3, 1, 0, 350)
TripleTriad::Card.new('Unknown 2', 4, 3, 6, 5, 7, 350)
TripleTriad::Card.new('Zemzelett', 4, 5, 4, 4, 5, 350)
TripleTriad::Card.new('Blugu', 3, 5, 7, 5, 0, 500)
TripleTriad::Card.new('Castanets', 3, 5, 3, 7, 0, 500)
TripleTriad::Card.new('Cripshay', 6, 7, 3, 1, 0, 500)
TripleTriad::Card.new('Doorbull', 6, 2, 6, 3, 0, 500)
TripleTriad::Card.new('Elfadunk', 6, 4, 3, 5, 0, 500)
TripleTriad::Card.new('Gagighandi', 6, 5, 7, 1, 0, 500)
TripleTriad::Card.new('Hungry', 4, 1, 7, 6, 0, 500)
TripleTriad::Card.new('Ice Golem', 7, 1, 6, 4, 2, 500)
TripleTriad::Card.new('Mighty Grunt', 6, 3, 5, 6, 0, 500)
TripleTriad::Card.new('Unknown 3', 7, 3, 4, 4, 7, 500)
TripleTriad::Card.new('Valron', 3, 3, 3, 7, 0, 500)
TripleTriad::Card.new('Allemagne', 4, 5, 6, 6, 0, 750)
TripleTriad::Card.new('Boundfat', 4, 6, 7, 5, 0, 750)
TripleTriad::Card.new('Christopher', 5, 5, 7, 4, 0, 750)
TripleTriad::Card.new('Edge Head', 5, 7, 3, 6, 0, 750)
TripleTriad::Card.new('Ghost Ship', 5, 6, 5, 6, 0, 750)
TripleTriad::Card.new('Gigas', 7, 4, 4, 6, 0, 750)
TripleTriad::Card.new('Iron Giant', 7, 5, 6, 2, 0, 750)
TripleTriad::Card.new('King Behemoth', 6, 7, 1, 7, 0, 750)
TripleTriad::Card.new('Malboro', 7, 1, 7, 4, 7, 750)
TripleTriad::Card.new('Master Tonberry', 5, 5, 5, 3, 0, 750)
TripleTriad::Card.new('Vlakorados', 6, 7, 6, 3, 0, 750)
TripleTriad::Card.new('Bottom Swell', 8, 1, 7, 5, 4, 1000)
TripleTriad::Card.new('Godo', 7, 3, 8, 3, 0, 1000)
TripleTriad::Card.new('Guard Scorpion', 7, 1, 7, 7, 3, 1000)
TripleTriad::Card.new('Heligunner', 6, 6, 2, 8, 0, 1000)
TripleTriad::Card.new('Hundred Gunner', 5, 2, 8, 7, 0, 1000)
TripleTriad::Card.new('Lost Number', 8, 1, 4, 8, 0, 1000)
TripleTriad::Card.new('Materia Keeper', 4, 8, 4, 6, 0, 1000)
TripleTriad::Card.new('Midgar Zolom', 8, 5, 8, 2, 0, 1000)
TripleTriad::Card.new('Motor Ball', 6, 8, 4, 3, 1, 1000)
TripleTriad::Card.new('Palmer', 1, 7, 8, 5, 0, 1000)
TripleTriad::Card.new('Schizo', 6, 7, 2, 7, 0, 1000)
TripleTriad::Card.new('Bizarro Sephiroth', 8, 5, 6, 5, 0, 1500)
TripleTriad::Card.new("Demon's Gate", 8, 3, 8, 1, 0, 1500)
TripleTriad::Card.new('Diamond Weapon', 8, 3, 8, 4, 0, 1500)
TripleTriad::Card.new('Emerald Weapon', 6, 8, 4, 6, 0, 1500)
TripleTriad::Card.new('Gi Nattak', 7, 6, 2, 8, 0, 1500)
TripleTriad::Card.new('Hojo', 7, 7, 7, 4, 0, 1500)
TripleTriad::Card.new('Jenova', 5, 2, 8, 8, 0, 1500)
TripleTriad::Card.new('Proud Clod', 5, 6, 5, 8, 0, 1500)
TripleTriad::Card.new('Red Dragon', 8, 2, 7, 7, 0, 1500)
TripleTriad::Card.new('Ruby Weapon', 6, 8, 1, 8, 0, 1500)
TripleTriad::Card.new('Ultimate Weapon', 6, 8, 7, 3, 0, 1500)
TripleTriad::Card.new('ChocoMog', 2, 9, 5, 9, 0, 2250)
TripleTriad::Card.new('Ifrit', 8, 1, 9, 7, 1, 2250)
TripleTriad::Card.new('Ramuh', 2, 4, 9, 9, 3, 2250)
TripleTriad::Card.new('Shiva', 9, 5, 7, 4, 2, 2250)
TripleTriad::Card.new('Titan', 6, 8, 2, 9, 6, 2250)
TripleTriad::Card.new('Avalanche', 8, 2, 8, 7, 0, 2250)
TripleTriad::Card.new('Elena', 1, 9, 8, 6, 0, 2250)
TripleTriad::Card.new('Reno', 6, 7, 5, 8, 0, 2250)
TripleTriad::Card.new('Rude', 4, 9, 9, 3, 0, 2250)
TripleTriad::Card.new('Tseng', 7, 9, 6, 3, 0, 2250)
TripleTriad::Card.new('Zack', 9, 4, 2, 9, 0, 2250)
TripleTriad::Card.new('Alexander', 6, 6, 4, 10, 8, 3500)
TripleTriad::Card.new('Bahamut', 7, 1, 7, 10, 0, 3500)
TripleTriad::Card.new('Bahamut Zero', 2, 8, 10, 6, 0, 3500)
TripleTriad::Card.new('Hades', 4, 10, 3, 8, 7, 3500)
TripleTriad::Card.new('Kjata', 10, 3, 9, 3, 0, 3500)
TripleTriad::Card.new('Knights of the Round', 10, 1, 10, 3, 0, 3500)
TripleTriad::Card.new('Leviathan', 5, 2, 10, 9, 4, 3500)
TripleTriad::Card.new('Neo Bahamut', 10, 9, 4, 3, 0, 3500)
TripleTriad::Card.new('Odin', 2, 6, 10, 8, 0, 3500)
TripleTriad::Card.new('Phoenix', 5, 10, 4, 6, 1, 3500)
TripleTriad::Card.new('Typoon', 6, 9, 1, 10, 5, 3500)
TripleTriad::Card.new('Aeris', 8, 6, 10, 3, 8, 5000)
TripleTriad::Card.new('Barret', 10, 2, 9, 7, 0, 5000)
TripleTriad::Card.new('Cait Sith', 8, 5, 5, 10, 0, 5000)
TripleTriad::Card.new('Cid', 5, 10, 5, 9, 0, 5000)
TripleTriad::Card.new('Cloud', 10, 7, 6, 6, 0, 5000)
TripleTriad::Card.new('Nanaki', 5, 8, 8, 8, 0, 5000)
TripleTriad::Card.new('Rufus', 5, 6, 7, 10, 0, 5000)
TripleTriad::Card.new('Sephiroth', 6, 10, 6, 6, 0, 5000)
TripleTriad::Card.new('Tifa', 6, 8, 10, 5, 0, 5000)
TripleTriad::Card.new('Vincent', 1, 9, 7, 10, 0, 5000)
TripleTriad::Card.new('Yuffie', 8, 10, 7, 2, 0, 5000)
 
for i in ($data_tt_cards.size - 110)...($data_tt_cards.size)
  $data_tt_cards.deck_id = $data_tt_decks.size
end
 
ln = ['Monsters', 'Monsters', 'Monsters', 'Monsters', 'Monsters',
      'Bosses', 'Bosses', 'Summons & Heros', 'Summons', 'Heros']
cards = []
cards << ($data_tt_cards.size - 110..$data_tt_cards.size - 100).to_a
cards << ($data_tt_cards.size - 99..$data_tt_cards.size - 89).to_a
cards << ($data_tt_cards.size - 88..$data_tt_cards.size - 78).to_a
cards << ($data_tt_cards.size - 77..$data_tt_cards.size - 67).to_a
cards << ($data_tt_cards.size - 66..$data_tt_cards.size - 56).to_a
cards << ($data_tt_cards.size - 55..$data_tt_cards.size - 45).to_a
cards << ($data_tt_cards.size - 44..$data_tt_cards.size - 34).to_a
cards << ($data_tt_cards.size - 33..$data_tt_cards.size - 23).to_a
cards << ($data_tt_cards.size - 22..$data_tt_cards.size - 12).to_a
cards << ($data_tt_cards.size - 11..$data_tt_cards.size - 1).to_a
 
TripleTriad::Deck.new('Final Fantasy VII', ln, cards, 50000)
 
end
 
#--------------------------------------------------------------------------
# * Booster Builder
#--------------------------------------------------------------------------
 
for deck_id in 1...$data_tt_decks.size
  prices = [950, 1980, 3690, 5760, 8550]
  suffix = ['A', 'B', 'C', 'D', 'E']
  cards = {
    0 => {0 => 3, 1 => 2, 2 => 1},
    1 => {0 => 4, 1 => 3, 2 => 2, 3 => 1},
    2 => {0 => 5, 1 => 4, 2 => 3, 3 => 2, 4 => 1},
    3 => {1 => 5, 2 => 4, 3 => 3, 4 => 2, 5 => 1},
    4 => {2 => 5, 3 => 4, 4 => 3, 5 => 2, 6 => 1},
  }
  for i in 0..4
    name = $data_tt_decks[deck_id].name + " #{suffix}"
    b_cards = {deck_id => cards}
    TripleTriad::Booster.new(name, b_cards, prices)
  end
end
 
#--------------------------------------------------------------------------
# * Save Data
#--------------------------------------------------------------------------
save_data($data_tt_cards, 'Data/Triple Triad Cards.rxdata')
save_data($data_tt_decks, 'Data/Triple Triad Decks.rxdata')
save_data($data_tt_boosters, 'Data/Triple Triad Boosters.rxdata')
 
else
 
#--------------------------------------------------------------------------
# * Load Data
#--------------------------------------------------------------------------
$data_tt_cards = load_data('Data/Triple Triad Cards.rxdata')
$data_tt_decks = load_data('Data/Triple Triad Decks.rxdata')
$data_tt_boosters = load_data('Data/Triple Triad Boosters.rxdata')
 
end
[/rgss]

Much more complex, but the structure is the same. (I didn't include the TripleTriad::Deck & TripleTriad::Booster data structure classes, but hopefully this helps a little.)

Its a more advanced way to do this, but the most efficient way I have seen.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top