Submit Your Article


 
RPG Maker

Welcome Guest ( Log In | Register )


  Games Resources RPG Maker VX RPG Maker XP Scripts Tutorials Downloads

 
Reply to this topicStart new topic
> Making a global variable, not as easy as I though
Adrien.
post Aug 31 2011, 06:38 PM
Post #1


Bet Mapper
Group Icon

Group: Banned
Posts: 1,632
Type: Mapper
RM Skill: Advanced




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?


__________________________



Games I am working on | Each image is a link to the game
Go to the top of the page
 
+Quote Post
   
Turkwise
post Aug 31 2011, 06:51 PM
Post #2


Level 5
Group Icon

Group: Member
Posts: 64
Type: Musician
RM Skill: Intermediate




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.


__________________________
My music thread:

http://www.rpgrevolution.com/forums/index....showtopic=51371

Please feel free to listen, comment, and download. You can use of tracks that are downloadable in your games if you wish.

Go to the top of the page
 
+Quote Post
   
Adrien.
post Aug 31 2011, 07:26 PM
Post #3


Bet Mapper
Group Icon

Group: Banned
Posts: 1,632
Type: Mapper
RM Skill: Advanced




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?

This post has been edited by Adrien.: Aug 31 2011, 07:56 PM


__________________________



Games I am working on | Each image is a link to the game
Go to the top of the page
 
+Quote Post
   
Turkwise
post Sep 1 2011, 09:30 AM
Post #4


Level 5
Group Icon

Group: Member
Posts: 64
Type: Musician
RM Skill: Intermediate




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


This post has been edited by Turkwise: Sep 1 2011, 09:31 AM


__________________________
My music thread:

http://www.rpgrevolution.com/forums/index....showtopic=51371

Please feel free to listen, comment, and download. You can use of tracks that are downloadable in your games if you wish.

Go to the top of the page
 
+Quote Post
   
Adrien.
post Sep 1 2011, 09:35 AM
Post #5


Bet Mapper
Group Icon

Group: Banned
Posts: 1,632
Type: Mapper
RM Skill: Advanced




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


__________________________



Games I am working on | Each image is a link to the game
Go to the top of the page
 
+Quote Post
   
Night5h4d3
post Sep 1 2011, 09:57 AM
Post #6


The past tense
Group Icon

Group: +Gold Member
Posts: 1,199
Type: Scripter
RM Skill: Undisclosed




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


__________________________
Got 30 minutes? Then you've enough time to play this awesome game:

- potentially promising project page
- thanks holder
My growing space of user-bars:

about me:







I made the following!





Go to the top of the page
 
+Quote Post
   

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 21st May 2013 - 01:00 PM
RPG RPG Revolution is an Privacy Policy and Legal
eXTReMe Tracker