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,281
Reaction score
5,457
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


Swag collecting in the new randomly generated area. Now the mini map also shows the randomly generated features... And even illustrates the whole "bubble" system" that I made to manage memory... Though that part's inadvertent. We'll just call that "the fog of war"... Yes, that's what we'll do. Nobody will be the wiser! :kaopride:
not me being not lazy for 5 seconds and drawing another char portrait (her name is hazel)
its_hazel.png
Been a while since the last time I made a song remix. Had a blast making this
One thing those jp developer still get wrong about boob physic is that... men's muscly hard pecs can jiggle too.
Definitely glad I haven't released a few of my plugins that are technically done. As I learn more, I find ways to make my code more efficient, and in one case so far, discovered an existing function that did EXACTLY what I created my own function to do.

Forum statistics

Threads
110,452
Messages
1,053,424
Members
143,541
Latest member
Neikoku
Top