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
345
Reaction score
128
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
345
Reaction score
128
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,116
Reaction score
5,367
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

Does anyone know of NPC sprites for RMMZ? I know of Flirion's RMMV recolours, but RMMZ's sprites are slimmer and more detailed, so I'm a bit on the edge to use Flirion's im RMMZ...
I can't believe that after all those years, KH Union X Dark Road is closing the services.

Due to a situation in which I thought plugin adaptations had stopped for the QPlugins, I "ghetto" converted these 7 over yesterday. They appear to work perfectly as long as you use old style plugin commands. I am uncertain if I will have a lot of free time in the future to work on this, but if you'd like them, here they are.
Was there a thread that you could share a screenshot of your game? I feel like I found it once but can't locate it again.

I was being lazy and just copied a map to test a new parallax, but I forgot to delete all the events. This was the result. :o

Forum statistics

Threads
108,818
Messages
1,039,788
Members
141,276
Latest member
acoolusername
Top