How to share a variable between two classes?

Status
Not open for further replies.

Hyperly

i like rocks
Veteran
Joined
Jun 25, 2016
Messages
72
Reaction score
11
First Language
English
Primarily Uses
RMXP
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.
 

KK20

Just some XP Scripter
Veteran
Joined
Oct 11, 2018
Messages
351
Reaction score
136
First Language
English
Primarily Uses
RMXP
Could you share some code so I get an idea what you're trying to do?
 

Hyperly

i like rocks
Veteran
Joined
Jun 25, 2016
Messages
72
Reaction score
11
First Language
English
Primarily Uses
RMXP
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
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
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.
 

KK20

Just some XP Scripter
Veteran
Joined
Oct 11, 2018
Messages
351
Reaction score
136
First Language
English
Primarily Uses
RMXP
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
Code:
LiTTleDRAgo::SMOOTH_FACTOR = X
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.
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
Now you just do $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
Yes, the to_f is quite important for better smoothing.
 

Hyperly

i like rocks
Veteran
Joined
Jun 25, 2016
Messages
72
Reaction score
11
First Language
English
Primarily Uses
RMXP
Oh wow, well that worked thank you. I forgot about saving too and assumed it would've just worked.
 

slimmmeiske2

Little Red Riding Hood
Global Mod
Joined
Sep 6, 2012
Messages
8,272
Reaction score
5,450
First Language
Dutch
Primarily Uses
RMXP

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.

 
Status
Not open for further replies.

Latest Threads

Latest Posts

Latest Profile Posts

You know you've been working on RPGMaker for a while when you go to move your projects to a 16 GB USB drive and get told not enough space to do this operation.
The latest post on TheDazzlingsAreBack...
besides itch and occasionally here where are people doing game jams at y'all? (plz do not cross reference this with my previous status lol)
Go out all day!
★No work★
Just want to sleep when I get home Zzzzz
just went through 50 issues of Fables, only to left kind of disappointed by it, I guess I have to wait for season 2 of TWAU instead.

Forum statistics

Threads
110,442
Messages
1,053,320
Members
143,509
Latest member
AfterDarkPro
Top