Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Help with the Cursor_Rect

Star

Sponsor

So I'm using the cursor_rect under the Window_Selectable class and I am having problems keeping it at a certain indexed position.

I tried making the index match the actor_index of the equip menu, for when you switch between characters.

So if you were on actor[2] you'd see the box hover over the 3rd actors sprite

I'm using set_cursor_rect (32 * @actor_index, 32, 48)
The problem with that is that @actor_index returns to nil when it does a Scene = Scene_equip.new(@actor_index, @right_window_index)

I can't figure out what to use in order to see what actor it's viewing. :crazy:
 
If it's simply shifted by 1 (which usually should happen the other way round O_o ), just subtract 1 from @actor_index, like so:
Code:
set_cursor_rect(@actor_index - 1 * 32, 32, 48)

For your nil problem: The line you provided is just passing the variables, it doesn't ensure that there will be a @actor_index present in the called class. Watch the initialize method, it should look similar to this:
Code:
def initialize(actor_index, equip_index)

  @somethingsomething = actor_index

  @anothersomething = equip_index

end
These instance variables (@) are what you can actually reference to within the class, nothing else.
 

Star

Sponsor

Thank you Bluescope. It's starting to make a little more sense now, With your help I got it to work perfectly.

I hope you don't mind but I'm gonna ask you a few more questions just so I can learn a little more...

So an instance variable or @ is what's called in a local class that only works in that said class and cannot be used anywhere else without referencing them in that said class with Initialization and calling new classes?

But a global variable or $ can be used in any class. Is it possible to turn a instance variable into a global variable. Like lets say.. @actor_index = $actor_index, and then use $actor_index in another class? Or will that still bring up a nil error?

Thanks for your help, I hope I will learn a lot from this.
 
I recommend that instead of making a global variable, you find another way to do what you need, such as making an attribute accessor or an attribute reader. The syntax would be as follows:

[rgss]attr_reader   :variable_1
attr_accessor :variable_2
[/rgss]

The difference between these is that the reader only allows the instance variable to be read by another object, while the accessor allows another object to have full control over the instance variable. To access the variable, all you would have to do is object.variable, just like if you were calling a method from the object.
 
Let's not forget about attr_writer, even though you'd normally use attr_accessor instead...


@Star: There's a grave logical error in your post, regarding variable assignment. If you want to set a variable, that variable needs to be before the equal sign, while the value you set it to comes after. It might be confusing for you because it's two variables with about the same name, but just look at this line and you'll see that it makes no sense:
Code:
23 = @foo # totally false

As far as variable types go, let's have a look...

[rgss]variable = 0 # local variable that is accessable within the current method only and gets unset when the method's ran through
@variable = 0 # instance variable that can be used within your whole class and won't be unset
@@variable = 0 # class variable that can be addressed by not only the current class, but all classes within the same superclass
$variable = 0 # global variable that can be addressed anywhere, however is very resource-intensive and should be used only if crucially necessary (which is almost never the case)
[/rgss]
In general, as the types of variables go, you'd choose them in order of necessity. In other words, while you could use a global variable for everything, if you just need to pass values within the method, you'd stick to a local variable instead. If you realize that won't be enough, use an instance variable, and so forth.

As the use of attributes can be a bit confusing, let's see a very simple example class...[rgss]class Game_Example
  attr_reader   :test_variable
  def initialize
    @test_variable = 20
  end
end
[/rgss]Let's assume you called this class from your imaginary Scene_Map class, for example like this, you could return the value of the variable given above as seen in the update method:[rgss]class Scene_Map
  def start
    @example = Game_Example.new
  end
  def update
    print @example.test_variable # this would return an undefined variable error without putting the attr_reader in Game_Example
  end
end
[/rgss]Now if you also want to change the variable from the class, you'd need to change the attr_reader into attr_accessor, so you could do this:[rgss]  def update
    @example.test_variable = 40 # this would return an error without attr_accessor
  end
[/rgss]Last but not least, note that now that you only modify and not try to read the variable, attr_writer would be sufficient. However, noone really uses it, and it's quite pointless anyway (even though you can't always just use attr_accessor, for example for some more advanced stuff).

You should see that these attributes are quite awesome, as you can keep every classes variables neatly by itself and access them from other classes with ease. This is also how you can access some variables from Game_Character, for example, that are quite useful for scripting - go in there and compare the initialized variables with the ones that are actually attributed.
 

Star

Sponsor

Thanks for the help BlueScope, I promise I'll use every last bit you taught me. I completely understand everything you just said, so that should be some hope for me. Now scripting should be a whole heck of a lot easier :shades: !
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top