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.

Assigning values to Arrays?

I'm pretty green behind the ears when it comes to coding, so I came across a problem when I was working on this little side project of mine. What I want to occur here is for an array to be created when the game starts up and for there to be creatable objects called Squads which have an ID so that I can do "Squad[1].str = 50" for instance.

The problem is that I'm not quite sure how to accomplish that. I've got Squads creating now with their stats being properly initialized as far as I can tell but (as I'm sure you'll see) I'm not sure how to start as I'm very unfamiliar with Arrays. I assumed I might accomplish it by doing a for loop (checking to see if an index in the array was empty and, if it was, assigning the Squad to that number) but I wasn't sure how to do that properly here.

The end result I'm trying to achieve here with this snippet is being able to do Squad.new and the Class creating a new Squad and assigning it to a free ID slot; the intention being the first created Squad will be Squad[1], the second being Squad[2], etc. but I'm not sure how many Squads the player might have so I'd like to keep it more open than my initial Array creation implies.

So: What do you suggest, HBGames?
Code:
class GameStart

  SQUAD_ID = [nil, nil, nil, nil]

  print "Created 4 Squads"

end

 

class Squad

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

# * Public Instance Variables

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

  attr_accessor :STATUS                   # Squad Status

  attr_reader   :ID                       # Squad Number

  attr_accessor :MORALE                   # Squad Morale

  attr_accessor :HP                       # Squad Health

  attr_accessor :ATK                      # Squad ATK Value

  attr_accessor :DEF                      # Squad DEF Value

  attr_accessor :AGI                      # Squad AGI Value

  attr_accessor :STR                      # Squad Strength

  attr_accessor :CRITFAIL                 # Squad has Critically Failed

  attr_accessor :CRITSUCCESS              # Squad has Critically Succeeded

  attr_accessor :ASSIGNED                 # Squad has been Assigned to a Task

  

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

# * Object Initialization

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

  def initialize

    # Squad Status

    @ID = []

    @STATUS = []

    # Unit Stats

    @MORALE = 0

    @HP = 0

    @STR = 0

    # Combat Stuff

    @ATK = 0

    @DEF = 0

    @AGI = 0

    # Task Assignment Stuff

    @CRITFAIL = false

    @CRITSUCCESS = false

    @ASSIGNED = false

    print "Squad Created Succesfully"

  end

  

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

  # * Get Squad ID (ID)

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

  def ID

    @ID = $SQUAD_ID

    print self.index

    return [self.index]

  end
 
A NoMethodError isn't what I would expect from a 'return' and it stopping processing that particular method I wouldn't expect a game crash from. I placed the Print request before it returned the value and I still got a NoMethodError.

What I meant in the above post is that for some reason, even though "[]" is defined as a method of the class Squads, I can't call it as written as it returns with 'no method defined'.
 
The line "print "SquadsSetup was called!" " isn't needed at all, the module shouldn't throw any error message (if you didn't modify it after you posted the last version here).

BTW I'd prefer to put Squad class before Squads class just to make sure it'd find the former.

[rgss]  def [](squad_id)
    return @squads[squad_id]
    print "Returned ", @squads[squad_id], "!"
  end
[/rgss]

This method should include an return - if statement just in case the ID isn't included in @squads array. Normally it should return nil if it doesn't exist, but in some calculations that might be an invalid return value...
 

MicKo

Member

Firgof Umbra":18nb6svd said:
A NoMethodError isn't what I would expect from a 'return' and it stopping processing that particular method I wouldn't expect a game crash from. I placed the Print request before it returned the value and I still got a NoMethodError.

What I meant in the above post is that for some reason, even though "[]" is defined as a method of the class Squads, I can't call it as written as it returns with 'no method defined'.
Firgof Umbra":18nb6svd said:
My current instructions:
Squads.new
Squads[0]
Well, Squads[0] doesn't exist yet in your example so it will bring this error. I tried to make it a little simpler here:
[ruby]#==============================================================================
# ** SquadsSetup
#------------------------------------------------------------------------------
#  Contains all initialized informations of squads and squad members.
#==============================================================================
module SquadsSetup
  # Squad Name = {Squad ID => "Squad Name", ...}
  SquadName = {0 => "RandomSquadName",
               1 => "RandomSquadName2"}
  # MaxHP = {[Squad ID, Member ID] => MaxHp, ...}
  MaxHP = {[0, 0] => 5,
           [0, 1] => 3,
           [1, 0] => 10}
end
 
#==============================================================================
# ** Squads
#------------------------------------------------------------------------------
#  Contains all squads
#==============================================================================
module Squads
  @squads = []
  #--------------------------------------------------------------------------
  # * Add Squad
  #--------------------------------------------------------------------------
  def self.add_squad
    @squads << Squad.new(@squads.size)
  end
  #--------------------------------------------------------------------------
  # * []
  #   squad_id : Squad ID
  #--------------------------------------------------------------------------
  def self.[](squad_id)
    return @squads[squad_id]
  end
end
 
#==============================================================================
# ** Squad
#------------------------------------------------------------------------------
#  Contains Squad information
#==============================================================================
class Squad
  attr_reader :members, :name
  #--------------------------------------------------------------------------
  # * Object Initialization
  #   id : Squad ID
  #--------------------------------------------------------------------------
  def initialize(id)
    @id = id
    @name = SquadsSetup::SquadName[id]
    @members = []
  end
  #--------------------------------------------------------------------------
  # * Add Member
  #--------------------------------------------------------------------------
  def add_member
    @members << SquadMember.new(@id, @members.size)
  end
  #--------------------------------------------------------------------------
  # * Remove Member
  #   member_id : Member ID (in Squad)
  #--------------------------------------------------------------------------
  def remove_member(member_id)
    @members.delete_at(member_id) unless @members[member_id] == nil
  end
end
 
#==============================================================================
# ** SquadMember
#------------------------------------------------------------------------------
#  Contains Squad Member information
#==============================================================================
class SquadMember
  attr_reader :maxHP
  #--------------------------------------------------------------------------
  # * Object Initialization
  #   squad_id : Squad ID
  #   member_id: Member ID (in squad)
  #--------------------------------------------------------------------------
  def initialize(squad_id, member_id)
    @maxHP=SquadsSetup::MaxHP[[squad_id, member_id]]
  end
end
[/ruby]
So you don't need @something = Squads.new. Just start by creating a new squad (Squads.add_squad) and then call Squads[n].
So for example you can call this :
[ruby]    Squads.add_squad # => Create Squads[0]
    Squads.add_squad # => Create Squads[1]
    Squads[0].add_member # => Create Squads[0].members[0]
    Squads[0].add_member # => Create Squads[0].members[1]
    Squads[1].add_member # => Create Squads[1].members[0]
    p "Member 0 of #{Squads[0].name}'s Max HP : #{Squads[0].members[0].maxHP}"
    p "Member 1 of #{Squads[0].name}'s Max HP : #{Squads[0].members[1].maxHP}"
    p "Member 0 of #{Squads[1].name}'s Max HP : #{Squads[1].members[0].maxHP}"
[/ruby]
 
@Firgof Umbra
Man, you should really post what do you want to do and post it from the scratch, or at least an answer to following questons:

- How are they going to be initialized?
- How their status is going to be changed?
- How are they damaged?
- What types of characters are stored into Squads? Characters from the database with their atk, def or your type you made? If it's second then post how they are going to look. If squad members are from that class SquadMember you made above then post all you want to be able to do with them?
- When they are destroyed?

If you don't at least produce some explanation kyo, silver wind, Micko, me and everyone else who wants to help you will just keep guessing and trying to fix it instead of giving you helpful advice on how to make them.

I understand you want to keep your script a secret and stuff (I'm guessing that's the reason) but this way it will just end up getting everyone annoyed.
 

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