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
 
If I understand, the problem is the array size is unknown.
You can start with an empty array (create it in Scene_Title, command_new_game). use array.push(squad) whenever you create a new squad. Why do you use constants, just use normal variables?
[rgss] 
# in scene Title do $Game_Squads = []
class Squad
  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
    @critsucess = false
    @assigned = false
   
    # save squad in the array
    @index = $Game_Squads.size
    $Game_Squads.push(self)
   
  end
end
[/rgss]
 
The other part is 'how do I plug variables into the empty slots'?

They're constants because I'm using them for calculations later. Their initial maxHP might be boosted by, say, Armor later on. :)

It'll be a totally new battle system and so forth for the squads; the Commander will use the default system.
 
$Game_Squads is a global variable, so you can use $Game_Squads to get squad #i from anywhere you want.
use normal variables and set them to attr_reader (if you also need to change them from outside of the class, use attr_accessor).
Don't use constants, stuff like hp will change during the game and you'll get an error for changing a constant.
The other part is 'how do I plug variables into the empty slots'?
line 21 inserts every new squad into $Game_Squads.
There won't be any empty slots if you do it this way.
 
Squad, as written, is meant to perform the creation of a Squad object with the attributes I listed in the initialization step.

I thought, since I am attempting to instance an object that I am creating from what I am attempting to make a template, that I would need to use constants in the initialization step of the constructing class (I did not paste the full script) so that, because I haven't declared any access rights, similarly named variables would not step on each other's toes.

The instanced squads created from this, of course, would have normal variables as they would need to be changed often and don't need to be specially protected like their constructor.

Am I misunderstanding how Ruby works?
 
Zeriab has shown me the light of using a global variable instead of killing myself making an extravagantly updating self-resizing array that needs to be instanced carefully with each new instance of the Squad

Problem Resolved.

D'oh!
 
well better than a global variable, is a class Variable :) so everything is nicely protected

something like this

[rgss] 
class Foo
   @@foos = [] # empty at creation
   
   attr_reader :index
 
   def initialize
      # do stuff
      @index = @@foos.size
      @@foos << self  # push the current instance (being created) into the class variable array.
   end
 
  def self.get(index) # to be called like this -> Foo.get(index_value)
     @@foos[index] # return the instance at that index
  end
end
 
[/rgss]
 
That does work, though I am a bit mystified on how to alter data from the Squad now that I can retrieve it.
Here's where I'm at.
Ruby:
 

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

# "SIEGE" by James Woodall

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

# Version Number: 0.2 [Internal Alpha]

# Created July 31, 2010

# Released to the Public Domain

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

 

class Squad

  @@SQUAD = []  # Where to store all the Squads in the game

  @@maxHP=255 # Global HP ceiling

  @@maxSTR=99 # Global STR ceiling

  @@maxMORALE=255 # Global MORALE ceiling

  @@maxATK=255 # Global ATK ceiling

  @@maxDEF=255 # Global DEF ceiling

  @@maxAGI=255 # Global AGI ceiling

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

# * Public Instance Variables

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

  attr_reader   :index                    # Squad Index

  attr_accessor :damage

  attr_accessor :STATUS                   # Squad Status

  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

    @STATUS = []

    # Unit Stats

    @MORALE = [@MORALE,@maxMORALE].min

    @HP = [@HP,@maxHP].min

    @STR = [@STR,@maxSTR].min

    # Combat Stuff

    @ATK = [@ATK,@maxATK].min

    @DEF = [@DEF,@maxDEF].min

    @AGI = [@AGI,@maxAGI].min

    # Task Assignment Stuff

    @CRITFAIL = false

    @CRITSUCCESS = false

    @ASSIGNED = false

    # Set the maximums for this unit respective to game's settings

    @maxHP=@@maxHP

    @maxSTR=@@maxSTR

    @maxMORALE=@@maxMORALE

    @maxATK=@@maxATK

    @maxDEF=@@maxDEF

    @maxAGI=@@maxAGI

    # INDEXING/LISTING

    @index = @@SQUAD.size

    @@SQUAD << self

  end

  

  def self.get(index)

    @@SQUAD[index]

    print "Got", @@SQUAD[index], "from @@SQUAD!"

  end

  

  def self.hurt(index, damage)

    @@SQUAD[index]

    @damage = damage

    index(HP) -= @damage

    print "Squad ", @@SQUAD[index]," took ", damage, " damage! ", @HP, " HP remains!"

  end

 

I'm not understanding something but I am not sure what.
 
you can't use something like... @victim(HP) -= @damage

it should be @victim[HP] -= @damage if HP is a constant. If not, then it should be @victim.hp -= @damage if it's accessible from your piece of code. If it isn't (and you get an error message), then you should go to the Game_Actor class or the class you're modifying by assigning a new value to it's hp (with = or +=) and create a new accessor like this...

class ModifiedClass
attr_accessor :hp
# any code already there
end
 
Hm. I thought I had tried that but apparently I had not. Now it has complained that I cannot convert from nil to integer.

Apparently when I decided to try and get HP to auto-calculate during initialization things did not go as planned and HP did not actually get calculated properly for the object.

EDIT:

you can't use something like... @victim(HP) -= @damage

it should be @victim[HP] -= @damage if HP is a constant. If not, then it should be @victim.hp -= @damage if it's accessible from your piece of code. If it isn't (and you get an error message), then you should go to the Game_Actor class or the class you're modifying by assigning a new value to it's hp (with = or +=) and create a new accessor like this...

class ModifiedClass
attr_accessor :hp
# any code already there
end

I'm creating an entirely new class. I'm following the template of one of the classes at the top just so that I decrease the likelihood that I'll screw up my coding and because there are similarities. The trouble is that I've already set that HP above, but the way I set it up doesn't work logically. I'm looking at it right now to figure out why.

@victim[@HP] ought to be the way this one is done. @HP should be an instanced variable that was both initialized and set when the object was created; the trouble is that it's currently nil. If I can resolve that then that hurt function ought to work as I intended it to. I shuffled around the variables a bit to make sure it wasn't ordered incorrectly but that had no impact so now I'm thinking it has to be the logic that I'm using to set it up.

Here's the current code:

Ruby:
 

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

# "SIEGE" by James Woodall

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

# Version Number: 0.2 [Internal Alpha]

# Created July 31, 2010

# Released to the Public Domain

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

 

class Squad

  @@SQUAD = []  # Where to store all the Squads in the game

  @@maxHP=255

  @@maxSTR=99

  @@maxMORALE=255

  @@maxATK=255

  @@maxDEF=255

  @@maxAGI=255

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

# * Public Instance Variables

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

  attr_reader   :index                    # Squad Index

  attr_accessor :damage

  attr_accessor :STATUS                   # Squad Status

  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

    # Set the maximums for this unit respective to game's settings

    @maxHP=@@maxHP

    @maxSTR=@@maxSTR

    @maxMORALE=@@maxMORALE

    @maxATK=@@maxATK

    @maxDEF=@@maxDEF

    @maxAGI=@@maxAGI

    # Squad Status

    @STATUS = []

    # Unit Stats

    @MORALE = [@MORALE,@maxMORALE].min

    @HP = [@HP,@maxHP].min

    @STR = [@STR,@maxSTR].min

    # Combat Stuff

    @ATK = [@ATK,@maxATK].min

    @DEF = [@DEF,@maxDEF].min

    @AGI = [@AGI,@maxAGI].min

    # Task Assignment Stuff

    @CRITFAIL = false

    @CRITSUCCESS = false

    @ASSIGNED = false

    # INDEXING/LISTING

    @index = @@SQUAD.size

    @@SQUAD << self

  end

  

  def self.get(index)

    @@SQUAD[index]

    print "Got", @@SQUAD[index], "from @@SQUAD!"

  end

  

  def self.hurt(index, damage)

    @@SQUAD[index]

    @damage = damage

    index[@HP] -= @damage

    print "Squad ", @@SQUAD[index]," took ", damage, " damage! ", @HP, " HP remains!"

  end

 
 
@HP would never be a constant but an instance variable due to the fact that you can see a @ prepended on its name.

Have you read any Ruby book, especially some chapter about variable scopes?

@maxHP = @@MAXHP

Actually that doesn't make any sense, it'd be better if you write this:

@maxHP = 225
 
Yeah, sorry, I mis-wrote.

Have you read any Ruby book, especially some chapter about variable scopes?

@maxHP = @@MAXHP

Actually that doesn't make any sense, it'd be better if you write this:

@maxHP = 225
It's apparently a bleh night for me and programming. @@MaxHP should be $MaxHP as it's supposed to be a global-scope variable so that I can have a global hard-limit on maximum HP when I introduce things that can increase the maximum HP of a Squad. That might explain why it was a nil and not the number I was expecting.


EDIT2: I was incorrect, that was not the reason why it was a nil. Still encountering problems.
Most current:
Ruby:
 

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

# "SIEGE" by James Woodall

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

# Version Number: 0.2 [Internal Alpha]

# Created July 31, 2010

# Released to the Public Domain

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

 

# Set up all the global game settings before Squads are created

class GameSettings

  $maxHP=255

  $maxSTR=99

  $maxMORALE=255

  $maxATK=255

  $maxDEF=255

  $maxAGI=255

end

 

class Squad

  @@SQUAD = []  # Where to store all the Squads in the game

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

# * Public Instance Variables

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

  attr_reader   :index                    # Squad Index

  attr_accessor :damage

  attr_accessor :STATUS                   # Squad Status

  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

    # Set the maximums for this unit respective to game's settings

    @maxHP=$maxHP

    @maxSTR=$maxSTR

    @maxMORALE=$maxMORALE

    @maxATK=$maxATK

    @maxDEF=$maxDEF

    @maxAGI=$maxAGI

    # Squad Status

    @STATUS = []

    # Unit Stats

    @MORALE = [@MORALE,@maxMORALE].min

    @HP = [@HP,@maxHP].min

    @STR = [@STR,@maxSTR].min

    # Combat Stuff

    @ATK = [@ATK,@maxATK].min

    @DEF = [@DEF,@maxDEF].min

    @AGI = [@AGI,@maxAGI].min

    # Task Assignment Stuff

    @CRITFAIL = false

    @CRITSUCCESS = false

    @ASSIGNED = false

    # INDEXING/LISTING

    @index = @@SQUAD.size

    @@SQUAD << self

  end

  

  def self.get(index)

    @@SQUAD[index]

    print "Got", @@SQUAD[index], "from @@SQUAD!"

  end

  

  def self.hurt(index, damage)

    @@SQUAD[index]

    @damage = damage

    index[@HP] -= @damage

    print "Squad ", @@SQUAD[index]," took ", @damage, " damage! ", @HP, " HP remains!"

  end

 

EDIT3: I've tried swapping some of the variables with magic numbers but to no avail. I've got to be calling for and editing the object incorrectly, which doesn't surprise me as I expected just saying 'index[@HP]' to fail as programming is not magical and I bet that Ruby wouldn't understand what I meant (the Index you grabbed a few moments ago). But I also don't know how to call for that information either. I don't think it's stored as an array inside that array, it's not a defined method, and I don't think I could ask for it as a parameter.

I suppose the correct way to do this might be to just calculate @HP in a defined method and then ask for it like:
Code:
@@SQUAD[index].HP -= @damage

I have no idea what the proper way to do that is so I guess I can work that in temporarily at least so that I can see how parts of Squads function. (And of course, later I find that doing that leads to a Stack too Deep error)
 
Well first... if class GameSettings is not going to be changed during game and remain constant i suggest you make it like this:

[rgss]module GameSettings
  MAX_HP =255
  MAX_STR =99
  MAX_MORALE =255
  MAX_ATK =255
  MAX_DEF =255
  MAX_AGI =255
end
[/rgss]

This way you can each of the constant like this

GameSettings::MAX_HP - gets the max hp from the GameSettings
GameSetttings::MAX_STR - get the max str from GameSettings
and so on...

if it IS going to be changed during game then you should do it like this:

[rgss] 
class GameSettings
  attr_accessor :max_hp
  attr_accessor :max_str
  attr_accessor :max_morale
  attr_accessor :max_atk
  attr_accessor :max_def
  attr_accessor :max_agi
 
  def initialize
   @max_hp = 255
   @max_str = 99
   @max_morale = 255
   @max_atk = 255
   @max_def = 255
   @max_agi = 255
  end
end
 
[/rgss]

And you put all of this into just one global variable for example: $game_settings = Game_Settings.new
and when you need to call it just do:

$game_settings.max_hp - gets the max hp
$game_settings.max_hp = value - sets the max hp to desired value
and so on. :)

Second:

Since you're making Squad structure here's a few things how I would have done it.

@@SQUAD = []

i would change into $game_squads = [] and push there all the squads for the game

next this is how I would have made the Squad data structure class:

When your not using constants it is considered good practice (but not required, to use small letters)

[rgss] 
module GameSettings
  MAX_HP =255
  MAX_STR =99
  MAX_MORALE =255
  MAX_ATK =255
  MAX_DEF =255
  MAX_AGI =255
end
 
class Game_Squad
#--------------------------------------------------------------------------
# * Public Instance Variables
#--------------------------------------------------------------------------
  attr_accessor :damage
  attr_reader   :name                     # Squads Name
  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(str, atk, def, agi, morale, name)
    # Having squads name makes it easier do make 2 distinct squads
    @name = name
 
 
    # Unit Stats
    @morale = [morale, GameSettings::MAX_MORALE].min
    @hp = GameSettings::MAX_HP
    @str = [str,GameSettings::MAX_STR].min
 
    # Combat Stuff
    @atk = [atk, GameSettings::MAX_ATK].min
    @def = [def, GameSettings::MAX_DEF].min
    @agi = [agi, GameSettings::MAX_AGI].min
 
    # Task Assignment Stuff
    @critfail = false
    @critsuccess = false
    @assigned = false
 
    @members = []
   
    # Dont know why you need this, but I'll leave it initialized
    @damage = 0
 
  end
 
  def status(index)
   return @members.states
 
  def push(member)
    @members << member
  end
 
  def []=(i,value)
    @members = value
  end
 
  def [](value)
    return @members[value]
  end
 
  def size
    return @members.size
  end
 
  # If index is not specified example Squad.hurt(10) then all members of the squad will get their hp decreased
  def hurt(damage,index = nil)
    if index != nil
     @damage = damage
     @members[index].hp -= @damage
     @members[index].hp = 0 if @members[index].hp < 0
    else
     for i in 0..@members.size - 1
       @members.hp -= @damage
       @members.hp = 0 if @members[index].hp < 0
     end
    end
  end
 
[/rgss]

Now you can do stuff like this for example

Putting all squads into one $game_squads

If you have like 5 different squads

for i in 0..4
$game_squads << Game_Squad.new(10, 15, 10, 20, 60, "Squad" + i.to_s)
end

This will get $game_squads to have 5 different squads but each of them will have same starting stats.

Will you do it like this or you will create different way it's up to you....

I'll let myself think that $game_squads will have charactes just like $game_party so to add members to a specific squad just do this

test_squad = Game_Squad.new(10, 15, 10, 20, 60, "TestSquad")

To add a member:
test_squad.push($game_actors[0]) - this will put the first character in your database in the squad party.

To get that member's status:
test_squad[0].hp
test_squad[0].sp

To change them?
test_squad[0].hp = value
test_squad[0].sp = value

To get how much members are in test_squad
test_squad.size

And you can also use those stuff of yours hp, critfail, critsuccess, morale just call them normally
test_squad.morale

See? This way it's behaving much like a normal array with some added features.

Other cool stuff:

When you added squads you can get each member of each different squad like this (considering you're using $game_squads)

$game_squads[squad_index][squad_member_index].hp (should work like this... didn't test though)
 
That script you have there has a syntax error where you defined the initialization step according to the game (I have no idea what it is), but yes: that's pretty much what I was wanting to do (the way you're polling and editing data in the objects created from the templates). Though I won't be using any of the default actors and et cetera from RPG Maker XP with the exception of a particular kind of battle while under siege (when your commander gets attacked). I'm pretty much writing an entirely new battle system to boot for squads.

Strength is the number of 'units' in a squad, if that makes it a bit more understandable on why I'm approaching things the way I am. The power of squads isn't in the units inhabiting them, but the overall power and morale and so forth; it's a more strategic viewpoint due to the game being much more strategic.

I keep thinking that Ruby doesn't allow you to do more than one parameter when you define stuff; I've always had that problem whenever I try to set something up which had more than one parameter. It's partly why what I'm trying to do here is a real head-scratcher for me.
 
Well that's what I get when I don't have a way to test them. I was writing completely in notepad so I didn't test anything through, sorry.

its the problem that I used a keyword for a variable, that's why the syntax error appears... here I fixed it + did that thing with the strength you mentioned:

[rgss] 
 
module GameSettings
  MAX_HP =255
  MAX_STR =99
  MAX_MORALE =255
  MAX_ATK =255
  MAX_DEF =255
  MAX_AGI =255
end
 
class Game_Squad
#--------------------------------------------------------------------------
# * Public Instance Variables
#--------------------------------------------------------------------------
  attr_accessor :damage
  attr_reader   :name                     # Squads Name
  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 :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(atk, defense, agi, morale, name)
    # Having squads name makes it easier do make 2 distinct squads
    @name = name
 
 
    # Unit Stats
    @morale = [morale, GameSettings::MAX_MORALE].min
    @hp = GameSettings::MAX_HP
    @str = [str,GameSettings::MAX_STR].min
 
    # Combat Stuff
    @atk = [atk, GameSettings::MAX_ATK].min
    @def = [defense, GameSettings::MAX_DEF].min
    @agi = [agi, GameSettings::MAX_AGI].min
 
    # Task Assignment Stuff
    @critfail = false
    @critsuccess = false
    @assigned = false
 
    @members = []
   
    # Dont know why you need this, but I'll leave it initialized
    @damage = 0
 
  end
 
 def str
  return @members.size # If you need a special modifier like * 2 or something add it
 end
 
  def status(index)
   return @members.states
 
  def push(member)
    @members << member
  end
 
  def []=(i,value)
    @members = value
  end
 
  def [](value)
    return @members[value]
  end
 
  def size
    return @members.size
  end
 
  # If index is not specified example Squad.hurt(10) then all members of the squad will get their hp decreased
  def hurt(damage,index = nil)
    if index != nil
     @damage = damage
     @members[index].hp -= @damage
     @members[index].hp = 0 if @members[index].hp < 0
    else
     for i in 0..@members.size - 1
       @members.hp -= @damage
       @members.hp = 0 if @members[index].hp < 0
     end
    end
  end
 
[/rgss]

There may be some other error but I doubt it, sorry don't currently have a way to test it.

Btw. Syntax error happens when you write something which isn't correct to write in programming grammar.
It's like I write:
He is the baddest
Instead of:
He is the worst.


Maybe if you say how you will calculate atk, def, morale, etc. maybe I can show you how to make it better.
 
MicKo and I went back and forth on the IRC for a bit to figure out an optimal solution to my problem.

Yeah, I know what Syntax errors are. They're just enough of a general error that it could have been a wide variety of problems. I assumed that it was the number of parameters as I changed everything else and as long as I had more than one, it error'd.

Here's what we came up with:

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}

end

 

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

# ** Squads

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

#  Contains all squads

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

class Squads

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

  # * Object Initialization

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

  def initialize

    @squads = []

  end

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

  # * Add Squad

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

  def add_squad

    @squads << Squad.new(@squads.size)

  end

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

  # * []

  #   squad_id : Squad ID

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

  def [](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

end

 

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

# ** SquadMember

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

#  Contains Squad Member information

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

class SquadMember

  attr_reader :maxHP

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

  # * Object Initialization

  #   squad_id : Squad ID

  #   id       : Member ID (in squad)

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

  def initialize(squad_id, id)

    @maxHP=SquadsSetup::MaxHP[[squad_id, id]]

    @maxSTR=0

    @maxMORALE=0

    @maxATK=0

    @maxDEF=0

    @maxAGI=0

    @morale = 0

    @hp = 0

    @hp2 = 0

    @str = 0

    @atk = 0

    @def = 0

    @agi = 0

  end

end

 
 
The syntax error in Drago's script is caused by a missing 'end' (should be at line 63), and at line 61 'i' should be 'index'.
I suggest you first create & bug-test your 'squad actor' class, as you'll need members to test the Squad class.
 
@silver wind
well I knew there was something, damn I've gotten so used to debugger. XD

And one more hint... There is a difference between attr_reader, attr_writer and attr_accessor

attr_reader as the name says allows only reading.
attr_writer for only write
attr_accessor for both
 
Oh fie!

Well, it doesn't appear to understand that '[]' was defined in the class of Squads. Or maybe I'm just calling it incorrectly.
My current instructions:
Squads.new
Squads[0]


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}

  print "SquadsSetup was called!"

end

 

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

# ** Squads

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

#  Contains all squads

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

class Squads

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

  # * Object Initialization

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

  def initialize

    @squads = []

    print "@squads initialized!"

  end

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

  # * Add Squad

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

  def add_squad

    @squads << Squad.new(@squads.size)

    print "New Squad pushed into @squads!"

  end

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

  # * []

  #   squad_id : Squad ID

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

  def [](squad_id)

    return @squads[squad_id]

    print "Returned ", @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 = []

    print "Squad ", @id, " initialized!"

  end

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

  # * Add Member

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

  def add_member

    @members << SquadMember.new(@id, @members.size)

    print "Added Squad ", @id, "of size", @members.size, " to @members!"

  end

end

 

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

# ** SquadMember

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

#  Contains Squad Member information

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

class SquadMember

  attr_reader :maxHP

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

  # * Object Initialization

  #   squad_id : Squad ID

  #   id       : Member ID (in squad)

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

  def initialize(squad_id, id)

    @maxHP=SquadsSetup::MaxHP[[squad_id, id]]

    @maxSTR=0

    @maxMORALE=0

    @maxATK=0

    @maxDEF=0

    @maxAGI=0

    @morale = 0

    @hp = 0

    @hp2 = 0

    @str = 0

    @atk = 0

    @def = 0

    @agi = 0

    print "Successfully initialized new SquadMember!"

  end

end

 
 
look at line 42. Your message will not be printed if it comes after 'return'.
'return' is used to stop the method you're in.
for example:
Code:
def divide(x,y)

   return if y==0   # stop. don't divide x by 0

   return x/y

end
 

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