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.

Variables ...

Future

Member

Now i been searching the forums for some basic help on variables and its enoying... ;) I been scanning around the existing scripts and that and i have become confused... I would like if someone could just quickly set my straight.

Okie i looked through the script and it looks quite similar to php (he says)
now what is with the @ and $, whats the difference?

Erhm what i wanted to do for my game was do some custom exp script,
that simply does this:

if Skill_Exp = 1000
then Skill_Level =+ 1
then say Well done you have gained a level.

Can someone quickly help me !! :S
Sorry if i have posted in the wrong forum... :-/
 

Jared

Member

local variables begins with a lowercase letter
example: name, _EXP, i
(but I would ever write local variables completly in lowercase lettern)
They work only in the code block, where they are created. Local variables are also used as arguments.
example:

Code:
def test
  i = 5 #i is a local variable
  i += 1 #i is now 6
end
test #call the method test
print(i) 
#this i is not the i in the method test, because this are two different code blocks
#so the output would be NIL, because there exist no local variable i in this block

instance variaböes begins with @
example: @Test, @object, @exp
(Here I would ever write in lowercase lettern, too. Also not @Test, but @test)
Instance variables works only in the object where they are created. When the object change his class, the instance variables will be erased in the cache.
Code:
class Hero
  def initialize
    @exp = 0
  end
end

Now the variable @exp is IN an object of the class hero.
To get extern access to this variable use the module-method attr_accessor

Code:
class Hero
attr_accessor(:exp)
  def initialize
    @exp = 0
  end
end
alex = Hero.new
alex.exp = 500
print(alex.exp)

Class variables beginns with @@. They works for a class and for all objects of the class.
example: @@counter, @@last_id
They are defined in the class, not in a method.

Code:
class Hero
  @@counter = 0
   def initialize
     @@counter += 1
   end
   def self.counter?
      p("There are " + @@counter.to_s + " heroes")
end
alex = Hero.new
judy = Hero.new
Jannis = Hero.new
Hero.counter #output: There are 3 heroes

global variables begins with $
example: $game_player, $game_map
They works global, also overall in the script. And every object has access to them.

Code:
$test = "Hello"
class A
  def initialize
    $test += " how"
  end
end
class B
  def initialize
    $test += " are"
  end
  def next_word
    $test += " you?"
  end
end
a = A.new
b = B.new
b.next_word
p $test #output: "Hello how are you?"

At least there are constants. They begin with a uppercase lettern.They can be defined everywhere but they can not be changed. When they are defined in a class or module, you get access through modulename::constantname
constants are usefull for class- and modulnames and for every value which you often need but which is ever the same.
example

Code:
module Constants
  MY_NAME = 'Jared'
end
p Constants::MY_NAME #output: 'Jared'

For your EXP-Script you should use a instance variable in the $game_actor object.

Code:
class Game_Actor
  attr_reader(:skill_exp, :skill_level)
  GAIN_SKILL_LEVEL = 'Well done you have gained a level!'

  def initialize(actor_id) #original script
    super() #original script
    setup(actor_id) #original script
    @skill_exp = 0 #new script
    @skill_level = 0 #new script
  end

  def add_exp(value)
    @skill_exp += value
    if @skill_exp >= 1000 then @skill_level += 1; p(GAIN_SKILL_LEVEL); @skill_exp = 0; end
  end
end

Of course that´s only an example. Instead of the p-method you should work with bitmaps and the draw_text method.
 

Future

Member

Thankyou so much i will try and absorb this ^^

Would be quite nice if u can explain what "def initialize" means and some other things, just so i know what you are talking about :)
 

Jared

Member

def initialize is a special method of every object which is called when the object is created.

Code:
class Test
  def initialize
    print("This method is called now!")
  end
end
my_object = Test.new  #output: "This method is called now!"
The method new creates a new object and give him the variable (also the adress) my_object. After this the method initialize is called. In initialize methods you can define the start_conditions of an object. Other methods musst be called with object_name.method_name
example:
Code:
class Test
  def initialize
    print("This method is called first!")
  end
  def write!
    print("This method musst is called when I wish it.")
  end
end
object = Test.new #output:"This method is called first!"
object.write! #output:"This method musst is called when I wish it."
 

Future

Member

Okay ill do some test and then i come back :)
Thankyou for replying tho ;P

Okay... Another question is how can i call this using the script event?
It would seem you cant simply say "object.write!" in the script event?!

EDIT: Sorry for Double Post
 

Jared

Member

When you have access to this event, yes.

Example: The gold-value of your party is an instance variable of the object $game_party. Because $game_party is global, you have overall access to it and because the attr_reader:)gold) method in the Game_Party class give you reading-access to this gold object, you can read the gold value everywhere.

(example in a CallRubyScript)
Code:
print($game_party.gold)

But from CallRubyScript you has only access to global variables (and their instance objects, if they are accessible by attr_accessor, attr_writer or attr_reader), local variables IN the Call Ruby Script window, Constants, Classes (and their class-variables) and instance variables in the Interpreter-Class (because CallRubyScript is a method in the Interpreter-Class).

When you create a local object in a CallRubyScript, this object will be erased when CallRubyScript is executed. Is it a instance_variable, the object will be erased when you leave the map. Is it a global variable, the object survived so long you wish.
 

Future

Member

Personaly i think you should be promoted ^^ You are realy helpful :)
Anyways ill try it out :p do u have msn?! i would like to chat to you somemore :) not just about Scripting :)

But anyways i dont understand how to run Object.write! im just trying things with the script you have supplied :p
 

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