I looked this up already and I'm still pretty confused on how it works. I some-what already solved this by just using a global variable outside of all the classes, but that's probably not what most people would recommend. I'm trying to share a variable called camera_smoothing to another class, but i'm unsure how to go about this.
How to share a variable between two classes?
RPG Maker XP SupportCould you share some code so I get an idea what you're trying to do?
Yeah. This is a part of the option scene where you control the camera's motion smoothing from LittleDrago's motion smoothing script.
in the class Scene_Options
And in LittleDrago's class ModPlayer, i have this little snippet im sharing
sf = $camera_smoothing
Currently the camera smoothing is a global variable, but I was hoping there was a way to do this without needing a global variable. Especially for future reference. I'm never been great with understanding code so I'm still learning.
in the class Scene_Options
Code:
when 'Camera Smoothing'
if Input.repeat?(Input::LEFT)
$camera_smoothing -= 1
if $camera_smoothing < 1
$camera_smoothing = 16
end
@command_window.refresh
elsif Input.repeat?(Input::RIGHT)
$camera_smoothing += 1
if $camera_smoothing > 16
$camera_smoothing = 1
end
@command_window.refresh
end
sf = $camera_smoothing
Currently the camera smoothing is a global variable, but I was hoping there was a way to do this without needing a global variable. Especially for future reference. I'm never been great with understanding code so I'm still learning.
You're talking about this script, right?
So the idea is you're allowing the player to customize the SMOOTH_FACTOR through your game's options scene.
While there isn't anything inherently wrong with using a global here, you could've just done the script call
because (at least in 1.8) Ruby will allow you to modify constants during run-time.
The issue though is that your options aren't being saved. They will reset as soon as the player starts up the game again. A common solution is to add an attribute to Game_System, which is saved when the player saves their game.
Now you just do
Yes, the to_f is quite important for better smoothing.
So the idea is you're allowing the player to customize the SMOOTH_FACTOR through your game's options scene.
While there isn't anything inherently wrong with using a global here, you could've just done the script call
Code:
LiTTleDRAgo::SMOOTH_FACTOR = X
The issue though is that your options aren't being saved. They will reset as soon as the player starts up the game again. A common solution is to add an attribute to Game_System, which is saved when the player saves their game.
Code:
class Game_System
attr_accessor :camera_smoothing
alias init_for_camera_smoothing initialize
def initialize
init_for_camera_smoothing
@camera_smoothing = 16
end
end
$game_system.camera_smoothing = X. You also want to update the line in Drago's script to look like
Code:
sf = $game_system.camera_smoothing.to_f
Oh wow, well that worked thank you. I forgot about saving too and assumed it would've just worked.
This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.
Original thread: https://forums.rpgmakerweb.com/threads/how-to-share-a-variable-between-two-classes.132578/