- Joined
- Jun 21, 2015
- Messages
- 1,441
- Reaction score
- 680
- First Language
- Polish
- Primarily Uses
- Other
How they work?
As far as I know it's like @instance_variable, but it's accessible in the whole game.
Because instance variables are stored in the class etc. then I assume they're getting removed (nil-led) upon terminating/disposing. But what about global variables?
They're stored in the 'game' itself and can be accessed anywhere, which is sometimes extremely handy. They're getting removed after shutting down the game, which is even better.
So my question is:
Is is safe to use many of these variables? Will that cause any issues when I'd be using them in various places and the player would play for hours triggering many of them?
For example in one scene I'd be using few $vars, in another scene even more $vars etc. etc. ~ and what if player will open all these scenes and will have tons of $vars stored in the game, because he will be not leaving for some time? Should I nil them after using them? (in terminate methods etc.)
Example:
Generally I want to use these variables as a really temporary ones.
A great example would be a minigame:
Let's say I'm making a scene with a minigame.
In the start method I'm putting stuff like
$score = 0
$lives = 3
And later I'm using another variables to store player's position for example:
def update_player
#some stuff
$player_position_x = @player.x
$player_position_y = @player.y
end
It would come in handy if I'd be making 'homing' objects like missiles.
So generally it would be only temporary and would allow me to freely refer to variables and it would be reset upon re-entering the scene, so save/load game wouldn't be an issue at all.
That's all ~ a simple temporary variable, that would be accessible anywhere.
And as you can see ~ they're used within a scene ~ let's say in a single run ~ but if I'd have multiple minigames, then some variables would be reset and reused which is nice, but some of them may be not needed and will be floating in game's memory when getting to many scenes with stuff like this.
so I could even set them all to nil in the terminate method if that would help.
Y can't u use instance variables?
Because there are situations, where using global ones would be much better. I know I can transfer instance variables to other classes/objects, but sometimes it would require caution and patience + coding all over the place.
Thank you!
As far as I know it's like @instance_variable, but it's accessible in the whole game.
Because instance variables are stored in the class etc. then I assume they're getting removed (nil-led) upon terminating/disposing. But what about global variables?
They're stored in the 'game' itself and can be accessed anywhere, which is sometimes extremely handy. They're getting removed after shutting down the game, which is even better.
So my question is:
Is is safe to use many of these variables? Will that cause any issues when I'd be using them in various places and the player would play for hours triggering many of them?
For example in one scene I'd be using few $vars, in another scene even more $vars etc. etc. ~ and what if player will open all these scenes and will have tons of $vars stored in the game, because he will be not leaving for some time? Should I nil them after using them? (in terminate methods etc.)
Example:
Generally I want to use these variables as a really temporary ones.
A great example would be a minigame:
Let's say I'm making a scene with a minigame.
In the start method I'm putting stuff like
$score = 0
$lives = 3
And later I'm using another variables to store player's position for example:
def update_player
#some stuff
$player_position_x = @player.x
$player_position_y = @player.y
end
It would come in handy if I'd be making 'homing' objects like missiles.
So generally it would be only temporary and would allow me to freely refer to variables and it would be reset upon re-entering the scene, so save/load game wouldn't be an issue at all.
That's all ~ a simple temporary variable, that would be accessible anywhere.
And as you can see ~ they're used within a scene ~ let's say in a single run ~ but if I'd have multiple minigames, then some variables would be reset and reused which is nice, but some of them may be not needed and will be floating in game's memory when getting to many scenes with stuff like this.
so I could even set them all to nil in the terminate method if that would help.
Y can't u use instance variables?
Because there are situations, where using global ones would be much better. I know I can transfer instance variables to other classes/objects, but sometimes it would require caution and patience + coding all over the place.
Thank you!
Last edited by a moderator:

