Help - Search - Members - Calendar
Full Version: Making a global variable
RPG RPG Revolution Forums > Scripting > Script Development and Support > RGSS2
Adrien.
we all know you can do $someName = SomeClass.new()

But what if you had:

CODE
def class SomeClass

def initialize
end

#bunch of methods and code

end


and wanted to make it so that instead of going SomeClass.new all the time or $someName = someClass.new in script calls or scripts you extend or create you would go $someName.new or $someName.someMethodCall

Where in the above code example would I create the global variable?
Turkwise
QUOTE (Adrien. @ Aug 31 2011, 08:38 PM) *
we all know you can do $someName = SomeClass.new()

But what if you had:

CODE
def class SomeClass

def initialize
end

#bunch of methods and code

end


and wanted to make it so that instead of going SomeClass.new all the time or $someName = someClass.new in script calls or scripts you extend or create you would go $someName.new or $someName.someMethodCall

Where in the above code example would I create the global variable?


Well, as an example of something I did in RMXP, I created a class called "Class_Control" to control changes to a character when their class changes.

In game_system I placed this code:

$game_cc = Class_Control.new

And then, I can, for example, call this script from an event:

$game_cc.class_up($game_variables[2], $game_variables[4])

I'm not sure what changes are in RGSS2 but it's still in ruby. So make a global instance of the class in the system script (or any similar script..Scene_Title perhaps), then you can use that wherever you'd like.

Hope that helps.
Adrien.
rgss2 in scene_title has a method called create_game_objects which creates all the global variables. Could I alias this method to add $someName = SomeClass.new to it?

Ya I am having issues doing this. using the example code in the OP, how would I add $someName to that code so that I can just have the player in game make script calls using the global variable?
Turkwise
QUOTE (Adrien. @ Aug 31 2011, 09:26 PM) *
Ya I am having issues doing this. using the example code in the OP, how would I add $someName to that code so that I can just have the player in game make script calls using the global variable?


Can't be done. Any code you add to that class will not be used until an instance of the class is initialized outside of that class. The code to create a global instance must be placed elsewhere.

I've looked through the RMVX scripts. It should be as simple as adding:

$someName = SomeClass.new

into Game_System in the initialize method.

Like this:

CODE
def initialize
    @timer = 0
    @timer_working = false
    @save_disabled = false
    @menu_disabled = false
    @encounter_disabled = false
    @save_count = 0
    @version_id = 0
    $someName = SomeClass.new
  end
Adrien.
so then im my class I could go:

CODE
alias :newinit :Initialize
def initialize
    @timer = 0
    @timer_working = false
    @save_disabled = false
    @menu_disabled = false
    @encounter_disabled = false
    @save_count = 0
    @version_id = 0
    $someName = SomeClass.new
    newInit
  end



I think >_>. I need this code inside MY class. so when the puts the script in their game all they have to do is call $someName
Night5h4d3
Calling self is generally unwise and redundant unless you're trying to update a value before continuing, which even then can be done otherwise.
$someName is a variable, variables in ruby always start out as undefined, a class on the other hand, follows by its own set of rules;
It seems as if you want to use $someName as if it were a class, which cannot be done until it is initialized. If you type in your editor someClass.new ruby would run the code in that class, and then be done, but if you write $someName = someClass.new then ruby assigns the variable $someName to the object someClass and then runs def initialize, and then def main. Later on you can do $someName.someMethodCall.

I'm not really sure what you're looking for, but you can also put $someName = someClass, and then $someName is the class object someClass, and you can use it like that without putting .new
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2013 Invision Power Services, Inc.