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.

very simple question but i can't find the answer

how do i make local variables within the ruby script. i just can't seemed to get it

whe i initialize my variable and assign a value to it (integer) and then when i use it in that same script it gives me an error that this variable has not been defined

i know i'm missing something but i don't know what it is

example
01: class MyClass
02: var1 = 100
03: def initialize
04: super(var1,var1, 100,100)
05: end
06:...and so on...

i get an error that var1 is "undefined local variable" at line 4.

can some one tell me what i'm missing

thx
 

Anonymous

Guest

Is your example the exact code you inserted into the RGSS editor?

Try putting spaces after the commas
(var1, var1, 100, 100)
or
(100, 100, 100, 100)

Not knowing what the code is for it's hard to tell what the problem is, as it doesn't look like there is much of a problem but then again it's late and my eye's hurt.
 

khmp

Sponsor

'super' is the keyword used in calling the parent's methods. Example where super could be used:
Code:
class MyClass < Parent_MyClass
  def intialize
    super # super will call Parent_MyClass's initialize
  end
end

Second instantiating a variable outside a function in or outside of a class is the same as creating a constant. To create a local variable meaning it loses scope outside the function its used in, just stick the declaration inside a function.

Code:
class MyClass < Parent_MyClass
  var1 = 100 # This is a constant variable
  def intialize
    var2 = 100 # This is local variable
    super # super will call Parent_MyClass's initialize
  end
end

Hope that clears things up for ya. Good luck with it!

I recommend you give http://www.phylomortis.com/html/rgss.html a shot and run through the syntax section if you have any other questions on keywords.
 
this is my exact code:
class MyCustomWindow < Window_Base
#----------------------------------------------------------------------
# * Object Initialization
#----------------------------------------------------------------------


var1 = 10

def initialize
super(var1, var1, 100, 200) <-this is where the error is
self.contents = Bitmap.new(100- 32, 200- 32)
refresh # Calls the refresh function in this class
end

def refresh
item = $data_items[1]
self.contents.clear
self.back_opacity = 150
self.back_opacity = 100
self.contents.font.size = 20
self.draw_item_name(item, 40, 50)
end

end


so i don't know whats wrong with it
 
I think your problem is that the var1 'is' a local variable which can only exist WITHIN a def or method. So, you'd really need it to be more like this:
Code:
def initialize
  var1 = 10
  super(var1, var1, 100, 200)
After that, the value doesn't exist in any other method. Even if you create another var1 value in another method in MyCustomWindow, it won't be the same.
 

khmp

Sponsor

Capitalize var1, making it Var1 instead and I think it should be fine. Constants need a capital letter beginning I guess. And make sure this class is located below Window_Base in the scripts listing. But DerVVulfman is right as well. And I think in this case a constant isn't truly necessary so you could just use a local variable like how DerVVulfman has it above.
 
thanks,

Using capital (Var1) fixed it.
And puting it iside the "def ininialize" (using var1) also works

and yeah, i thought about using it as global but that is an overkill, since i use the same names for variables in all the similar classes

thank you very much :thumb:
 

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