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.

Game_Infamy

Okay, I made myself a nice little class for my Infamy system called Game_Infamy, and in there, I made all the values.

I call it in Scene_Infamy though and I get an error.

the first variable mentioned in the scene is


Code:
    $arrests += 1

And I get the error:

'Undefined method '+'

And yet, in Game_Infamy I have

Code:
attr_accessor :arrests

And later on

Code:
$arrests = 0

Can anybody help?
 
First you don't need the attr because it is a Global variable. if you want to you the Attr. you should do.
Code:
class Game_Infamy
  attr_accessor :arrests
  def initialize
    @arrests = 0 #this must be a class variable to use the attr
  end
end

class Scene_Infamy
 def initialize
   $game_infamy = Game_infamy.new
  #whatever more you have
  end
  def whatever
    $game_infamy.arrests += 1 #this is used because of the attr_accessor
   #whatever more
  end
end


Few tips:

$variable => is a global variable, because of that you can't use Attr. When you use this variable, you can make $variable += whatever in all class you want without needing to define it.
@variable => class variable, this is a variable use only on that class, you can "transport it to other class with,
attr_accessor,
attr_reader.
with the get method:
Code:
class whatever
 def initialize
   @variable = 0
 end
 def variable
   return @variable
 end
end

class other_class
  def update
     whatever,variable
  end
end
 
Great, thanks!

I think I saw it somewhere, but when I tried with another variable a while back, the variables reset to 0 every time you save, close, and reopen the game. I think when I tried with something else, I also had the problem that the variable did not change if you left one save and loaded another.

Are these easily fixed things? If not, never mind.
 
I think you need to save it in the Scene_Save...
But I never used.
I think it is something like
$Game_Infamy.dump
But I don't know. Try looking in the Scene Save, and Scene_load.
 
OK, thanks man :D

EDIT: Is it possible to assign a word / sentence to a variable? I noticed in some game classes it was, but when I call

$game_infamy = Game_Infamy.new
$game_infamy.blah = "Insert text here"

The text doesn't appear, and it stays blank as it is in the Game class.

How do I sort this?
 
Chaosg1 said:
@variable => class variable, this is a variable use only on that class, you can "transport it to other class with,
attr_accessor,
attr_reader.

actually class variables begin with two @ and attr_(whatever) doesn't work for those as well

what attr_accessor does is creates these methods
Code:
def variable=(obj)
  @variable = obj
end

def variable
  return @variable
end
attr_reader creates the second above method and attr_writer creates the first above method

Types of variables
$ => global variable (should limit its use more memory used than other types of variables)
accessiible anywhere
@@ => class variable (shared by instances of a class)
@ => instance variable (each instance has its own copy)
nothing => local variable exists only in the executing method and removed afterwards
capital letter => constant value cannot be changed

but really you can create a method that acts like attr_(accessor, reader, writer) it would probably look something like this (psuedo-code,untested)

Code:
class Module
  private
  def class_accessor(*args)
    args.each do |symbol|
      eval("def #{symbol.to_s}; return @@#{symbol.to_s}; end")
      eval("def #{symbol.to_s}=(obj); @@#{symbol.to_s}; end")
    end
  end
end

And what this does is when this is called it creates those methods for you, based on the Symbol sent to it, its also a private method so you can't call this method outside the class. Also class Class (sounds funny, a class of Classes) inherits from class Module so you can use this for class definitions as well
 
Chaosg1 said:
Few tips:

$variable => is a global variable, because of that you can't use Attr. When you use this variable, you can make $variable += whatever in all class you want without needing to define it.
@variable => class variable, this is a variable use only on that class, you can "transport it to other class with,
Variables that start with @ are instance variables, not class variables. Class variables start with @@.

EDIT: I didn't realize that Trickster had already said that.
 

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