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.

Calling data from data class?

I have this data class
Code:
#===================================================================================

#    Bakugan Data Structure

#===================================================================================

 

#==============================================================================

# ** Bakugan Module

#------------------------------------------------------------------------------

# This Module Holds Classes the Bakugan System Uses

#==============================================================================

module Bakugan

  

  #===========================================================================

  # ** Player

  #===========================================================================

  Player = Struct.new(:b1, :b2, :b3, :g1, :g2, :g3, :a1, :a2, :a3)

  class Player

    attr_accessor :id

    alias_method :player_init, :initialize

    def initialize(*struct_args)

       # Calls our struct initialize method

       player_init(*struct_args)

       # Set @id to be the size of our $data_bakugan list

       @id = $data_players.size

       # Put our object automatically into the list

       $data_players[@id] = self

     end

  end

 

  $data_players = [nil]

  #===========================================================================

  # ** Bakugan

  #===========================================================================

  Bakugan = Struct.new(:name, :gpower, :attribute)

  class Bakugan

    attr_accessor :id

    alias_method :bakugan_init, :initialize

    def initialize(*struct_args)

       # Calls our struct initialize method

       bakugan_init(*struct_args)

       # Set @id to be the size of our $data_bakugan list

       @id = $data_bakugans.size

       # Put our object automatically into the list

       $data_bakugans[@id] = self

     end

  end

 

  $data_bakugans = [nil]

  Bakugan.new('monster', 450, 3)

 

  

  #===========================================================================

  # ** Gate Card

  #===========================================================================

  Gate_Card = Struct.new(:name, :action, :pyrus, :aquos, :subterra, :haos, :darkus, :ventus)

  class Gate_Card

    attr_accessor :id

    alias_method :gcard_init, :initialize

    def initialize(*struct_args)

       # Calls our struct initialize method

       gcard_init(*struct_args)

       # Set @id to be the size of our $data_gcard list

       @id = $data_gcards.size

       # Put our object automatically into the list

       $data_gcards[@id] = self

     end

  end

  

  $data_gcards = [nil]

  Gate_Card.new('gate', 1, 100, 100, 100, 100, 100, 100)

  

  #===========================================================================

  # ** Ability Card

  #===========================================================================

  Ability_Card = Struct.new(:name, :action, :pyrus, :aquos, :subterra, :haos, :darkus, :ventus)

  class Ability_Card

    attr_accessor :id

    alias_method :acard_init, :initialize

    def initialize(*struct_args)

       # Calls our struct initialize method

       acard_init(*struct_args)

       # Set @id to be the size of our $data_acard list

       @id = $data_acards.size

       # Put our object automatically into the list

       $data_acards[@id] = self

     end

  end

  

  $data_acards = [nil]

  Ability_Card.new('ability', 1, 100, 100, 100, 100, 100, 100)

end

What i want to be able to do i use
Code:
Player = Struct.new(:b1, :b2, :b3, :g1, :g2, :g3, :a1, :a2, :a3)
well just the b1 b2 and b3 to call just the idea from the Bakugan Data Class if that possible and if so how would i do it? Basicly and eventully the player will pick 3 different bakugan objects from a menu to use in a battle but i cannot do that tile i know how to call it. Any help is greatly appreciated. Thank you.
-SoBe
 

Zeriab

Sponsor

That wouldn't work berka.

Struct is a class which is used for creating other classes.
The idea is probably to have something similar to structs in C++
Here is an example of how you can do it:
[rgss]Player_Class = Struct.new:)b1, :b2, :b3, :g1, :g2, :g3, :a1, :a2, :a3)
player_object = Player_Class.new(1, 5, 42, nil, nil, nil, nil, nil, nil)
p player_object.b1, player_object.b2, player_object.b3
[/rgss]



I can see you have created a global variable called $data_players. I suggest you use that to get a reference to a player.
You do not create any players in what you show us, but that's alright.
I personally prefer creating class methods which you can use to retrieve the players rather than using global variables.

I admit I am not really sure what you have problem with.
You have no problems creating Bakugan and card instances. It's the same procedure for the player.
Is it that you want to change the values at a later point?

*hugs*
 
Sorry... I misread
I believed he had used struct as:

Struct.new("Player_Class",b1...)

Structs is a good way for your system, but this is more simple:

Code:
class Player

  def initialize(*args)

    self.class.class_eval("attr_accessor :#{args.join(", :")}")

  end

end

About your $data_player you should use instances:

Code:
$data_player ||= {}

instance=Player.new("id","atk","def")

instance.id=251

$data_player[instance.id]=instance


berka
 

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