Lord Vectra

Master Eventer
Veteran
Joined
Dec 6, 2015
Messages
300
Reaction score
390
First Language
English
Primarily Uses
RMVXA
I’m learning XP scripting. I’m creating a level up system, or I guess I should say, I am very heavily editing an existing level up system created by someone else because the whole scene/window relation is a bit confusing (but I’m slowly learning as I’m editing this script). I was in the help section of the RMXP scrolling through the Ruby XP Universe, and I have a series of questions because the help section isn’t clear enough for me.

1. If you have an array like $Jesus = [1, 2, 3], and I do $Jesus += 3, from what the help section said, it sounds like they’re saying the 3 is added so $Jesus then becomes [1, 2, 3, 3] correct? It says it turns non-arrays to “to_ary” in this instance which I assume means if I don’t add another array to the existing array, the program automatically turns it into an array to be added, right?

2. In continuation of Question 1, if I did another array like $Christ = [4, 5, 6], I assume the order in which I add them together determines the order of the numbers? So I could either have [1, 2, 3, 4, 5, 6] or [4, 5, 6, 1, 2, 3], right?

3. How do attribute readers work?

4. A part of my script was very program-intensive meaning the blinking of the cursor was starting to blink very slowly due to all the refreshing. I have sense fixed it, but I have a Geoforce RTX 3090 so let’s pretend I didn’t fix it. Would it blink slower if on a weaker computer or is it a burden only the program itself endures?

5. Similar to Question 4, this isn’t scripting, but if I were to have 20 Parallel processing common events going on at the same time, does the PC endure that (so the weaker, the slower) or is that just the program?

6. Similar to Question 3, I noticed that local variables can be used in other scripts. If I made a local variable called @Bob, can I just run attribute reader in any script to “link” @Bob? Is there only certain scripts I can do that?

7. In the help section under “String”, it has something called “clone” but literally says nothing on what it does. I’m thinking maybe clone a string (as the name suggests), but it feels like a wasted feature since it's not like you can ONLY use a string once in a script, or am I missing something?

8. Lastly... what are you doing for Christmas? I’m going to be hanging out with my Dad.
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
5,776
Reaction score
5,577
First Language
English
Primarily Uses
RMMZ
I’m learning XP scripting. I’m creating a level up system, or I guess I should say, I am very heavily editing an existing level up system created by someone else because the whole scene/window relation is a bit confusing (but I’m slowly learning as I’m editing this script). I was in the help section of the RMXP scrolling through the Ruby XP Universe, and I have a series of questions because the help section isn’t clear enough for me.

1. If you have an array like $Jesus = [1, 2, 3], and I do $Jesus += 3, from what the help section said, it sounds like they’re saying the 3 is added so $Jesus then becomes [1, 2, 3, 3] correct? It says it turns non-arrays to “to_ary” in this instance which I assume means if I don’t add another array to the existing array, the program automatically turns it into an array to be added, right?

2. In continuation of Question 1, if I did another array like $Christ = [4, 5, 6], I assume the order in which I add them together determines the order of the numbers? So I could either have [1, 2, 3, 4, 5, 6] or [4, 5, 6, 1, 2, 3], right?

3. How do attribute readers work?

4. A part of my script was very program-intensive meaning the blinking of the cursor was starting to blink very slowly due to all the refreshing. I have sense fixed it, but I have a Geoforce RTX 3090 so let’s pretend I didn’t fix it. Would it blink slower if on a weaker computer or is it a burden only the program itself endures?

5. Similar to Question 4, this isn’t scripting, but if I were to have 20 Parallel processing common events going on at the same time, does the PC endure that (so the weaker, the slower) or is that just the program?

6. Similar to Question 3, I noticed that local variables can be used in other scripts. If I made a local variable called @Bob, can I just run attribute reader in any script to “link” @Bob? Is there only certain scripts I can do that?

7. In the help section under “String”, it has something called “clone” but literally says nothing on what it does. I’m thinking maybe clone a string (as the name suggests), but it feels like a wasted feature since it's not like you can ONLY use a string once in a script, or am I missing something?

8. Lastly... what are you doing for Christmas? I’m going to be hanging out with my Dad.
1. No, this doesn't work. If you try it you'll get a "can't convert Fixnum into Array" error. You can do $Jesus += [4, 5, 6] but not $Jesus += 1. To add a single value that isn't enclosed in array brackets, you'd have to use the push method.

2. Yes, they concatenate in the order you add them.

3. In encapsulation, you protect internal variables by making them private and offering an external interface for other code to access them. So let's say you have an instance variable called myVar:

Ruby:
def initialize
  @myVar = 0
end

def setMyVar=(value)
  @myVar = value
end

def getMyVar
  @myVar
end

These are methods called getters and setters. The idea is that if you ever change the internal implementation of @myVar, code that calls the getter/setter need not be changed. What an attr_reader does is creates the getter for you, so you can just use object.myVar externally and it won't cause an error because the property is inaccessible. In the same vein, attr_writer creates a setter, and attr_accessor creates both.

4. More powerful computers can compensate for frame rate drops better than slower ones can, but there are also ways to set up events that are inefficient enough that they'll slow down even powerful PCs. It's a little from both columns, really.

5. As above

6. The variables beginning with @ are instance variables, which are unique on instances of a class. For example, if I have a class called Dog and it has an instance variable called @Name, each Dog I create will have its own @Name. However, it won't do anything on its own without a Dog instance to be attached to.

7. The thing with strings in Ruby is that they are objects, and defining the same string twice creates a separate object with its own memory address, even if it contains the same characters. This is the reason symbols exist. I imagine cloning the string is a way to ensure reference to the same object rather than creating a new one, but I've never used .clone on a string so this is mainly guesswork on my part.
 

Lord Vectra

Master Eventer
Veteran
Joined
Dec 6, 2015
Messages
300
Reaction score
390
First Language
English
Primarily Uses
RMVXA
1. No, this doesn't work. If you try it you'll get a "can't convert Fixnum into Array" error. You can do $Jesus += [4, 5, 6] but not $Jesus += 1. To add a single value that isn't enclosed in array brackets, you'd have to use the push method.

2. Yes, they concatenate in the order you add them.

3. In encapsulation, you protect internal variables by making them private and offering an external interface for other code to access them. So let's say you have an instance variable called myVar:

Ruby:
def initialize
  @myVar = 0
end

def setMyVar=(value)
  @myVar = value
end

def getMyVar
  @myVar
end

These are methods called getters and setters. The idea is that if you ever change the internal implementation of @myVar, code that calls the getter/setter need not be changed. What an attr_reader does is creates the getter for you, so you can just use object.myVar externally and it won't cause an error because the property is inaccessible. In the same vein, attr_writer creates a setter, and attr_accessor creates both.

4. More powerful computers can compensate for frame rate drops better than slower ones can, but there are also ways to set up events that are inefficient enough that they'll slow down even powerful PCs. It's a little from both columns, really.

5. As above

6. The variables beginning with @ are instance variables, which are unique on instances of a class. For example, if I have a class called Dog and it has an instance variable called @Name, each Dog I create will have its own @Name. However, it won't do anything on its own without a Dog instance to be attached to.

7. The thing with strings in Ruby is that they are objects, and defining the same string twice creates a separate object with its own memory address, even if it contains the same characters. This is the reason symbols exist. I imagine cloning the string is a way to ensure reference to the same object rather than creating a new one, but I've never used .clone on a string so this is mainly guesswork on my part.
Thank you!

For 3, about writer. If writer is the setter, is that the same as saying it creates the variable?

And actually, about the reader, let's say I have a reader of myVar which I assume would look like "attr_reader :myVar". If I use it in the same area (I don't know the term) I have an another @myVar, how do I tell the program which is which or is it one of those "just don't do that."

Side Note: I now know a new word... concatenate.
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
5,776
Reaction score
5,577
First Language
English
Primarily Uses
RMMZ
Thank you!

For 3, about writer. If writer is the setter, is that the same as saying it creates the variable?

And actually, about the reader, let's say I have a reader of myVar which I assume would look like "attr_reader :myVar". If I use it in the same area (I don't know the term) I have an another @myVar, how do I tell the program which is which or is it one of those "just don't do that."

Side Note: I now know a new word... concatenate.
Yes, the writer creates the variable. Well, to be more accurate it creates a method that allows you to set the variable's value from outside the class.

If you create an attr_reader called myVar, anywhere you use @myVar within the class will refer to the same instance variable, you can't have a duplicate that exists separately.
 

Lord Vectra

Master Eventer
Veteran
Joined
Dec 6, 2015
Messages
300
Reaction score
390
First Language
English
Primarily Uses
RMVXA
Yes, the writer creates the variable.

If you create an attr_reader called myVar, anywhere you use @myVar within the class will refer to the same instance variable, you can't have a duplicate that exists separately.
Then last question, What's the benefit of accessor? One creates a variable and the other gets a variable from somewhere else. How can one create a variable and get the same variable from somewhere else or did I miss something?
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
5,776
Reaction score
5,577
First Language
English
Primarily Uses
RMMZ
Then last question, What's the benefit of accessor? One creates a variable and the other gets a variable from somewhere else. How can one create a variable and get the same variable from somewhere else or did I miss something?
It's just shorthand that saves you from having to create the getter and setter methods manually.

Let's see if I can explain this more clearly...again we'll go with the Dog class example. It has an instance variable called @fur_colour.

Now in the main program, if I do something like

Ruby:
$fido = Dog.new

$fido's @fur_colour will have whatever value I set in the Dog class's constructor (the initialize method). But I can't do

Ruby:
$fido.fur_colour = "black"

because fur_colour is protected. In order to write to it, I have three options:

1. Write a setter in the Dog class:

Ruby:
def fur_colour=(colour)
  @fur_colour = colour
end

2. Create an attr_writer called :fur_colour

3. Create an attr_accessor called :fur_colour

Option 2 will allow you to write to the property but not read it (so using $fido.fur_colour in the program will also error out), and option 3 will allow writing and reading.

It may further clarify matters if I explain that the main purpose of attr_reader, attr_writer and attr_accessor is to save you some time and effort for cases where the *only thing* your getter and setter methods do is set the value of the variable or get its current value. If you want it to do something else (like call a method after setting that does some checks on the value and converts it to something else under certain conditions) you'd write the setter manually because an attr_writer/accessor can't do anything that sophisticated.
 

Lord Vectra

Master Eventer
Veteran
Joined
Dec 6, 2015
Messages
300
Reaction score
390
First Language
English
Primarily Uses
RMVXA
It's just shorthand that saves you from having to create the getter and setter methods manually.

Let's see if I can explain this more clearly...again we'll go with the Dog class example. It has an instance variable called @fur_colour.

Now in the main program, if I do something like

Ruby:
$fido = Dog.new

$fido's @fur_colour will have whatever value I set in the Dog class's constructor (the initialize method). But I can't do

Ruby:
$fido.fur_colour = "black"

because fur_colour is protected. In order to write to it, I have three options:

1. Write a setter in the Dog class:

Ruby:
def fur_colour=(colour)
  @fur_colour = colour
end

2. Create an attr_writer called :fur_colour

3. Create an attr_accessor called :fur_colour

Option 2 will allow you to write to the property but not read it (so using $fido.fur_colour in the program will also error out), and option 3 will allow writing and reading.

It may further clarify matters if I explain that the main purpose of attr_reader, attr_writer and attr_accessor is to save you some time and effort for cases where the *only thing* your getter and setter methods do is set the value of the variable or get its current value. If you want it to do something else (like call a method after setting that does some checks on the value and converts it to something else under certain conditions) you'd write the setter manually because an attr_writer/accessor can't do anything that sophisticated.
Okay, that all makes sense. Thank you!
 

kyonides

Reforged is laughable
Veteran
Joined
Nov 17, 2019
Messages
704
Reaction score
315
First Language
English
Primarily Uses
RMXP
Actually, attr_accessor is the default method to create setter and getter methods.
attr_reader and attr_writer are just shortcuts to only create a setter or a getter. At the end, they both call the internal function that's linked to attr_accessor method.
If any of you take a look at CRuby code, you'd get what I'm talking about here at once.

4. and 5. actually depend on the internal implementation of Ruby through RGSS. (Newer versions of Ruby would have taken care of large numbers of parallel processes, and they would still run smoothly.)
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
5,776
Reaction score
5,577
First Language
English
Primarily Uses
RMMZ
Actually, attr_accessor is the default method to create setter and getter methods.
attr_reader and attr_writer are just shortcuts to only create a setter or a getter. At the end, they both call the internal function that's linked to attr_accessor method.
If any of you take a look at CRuby code, you'd get what I'm talking about here at once.
That's...what I said though? I said attr_accessor creates both a getter and setter and attr_reader/attr_writer create only or the other. How does that differ from what you've said?
 

kyonides

Reforged is laughable
Veteran
Joined
Nov 17, 2019
Messages
704
Reaction score
315
First Language
English
Primarily Uses
RMXP
That's...what I said though? I said attr_accessor creates both a getter and setter and attr_reader/attr_writer create only or the other. How does that differ from what you've said?

Didn't I mention that it's all about the internal function linked to the attr_accessor?
In CRuby there's no separate attr_reader or attr_writer function they're just wrappers of attr_accessor.
It's important to keep that in mind if you want them to get processed as fast as possible (in ms).

There's another advantage of using any of those three methods. Ruby finds the variables faster, if they were created that way (mainly because they're located in memory) than the typical lines of code like def my_method.
 

Zeriab

Huggins!
Veteran
Joined
Mar 20, 2012
Messages
1,359
Reaction score
1,561
First Language
English
Primarily Uses
Other
Interesting. Do you have an data on the performance difference?

@Lord Vectra
CPU single-core performance tends to be what matters for game performance. GDI+ is used by the RGSS Player to render the game. (Yeah, it's being software rendered)
 

Lord Vectra

Master Eventer
Veteran
Joined
Dec 6, 2015
Messages
300
Reaction score
390
First Language
English
Primarily Uses
RMVXA
Interesting. Do you have an data on the performance difference?

@Lord Vectra
CPU single-core performance tends to be what matters for game performance. GDI+ is used by the RGSS Player to render the game. (Yeah, it's being software rendered)
How would I go about getting that data? Would be nice if I could do some approximates without needing to actually find a weaker PC to test it on lol. My other PC has a GPU that's 11yrs old (so probably not a good representation lol).

To give you an idea on the performance difference, in the script, in a specific part of the level up proccess, is refreshing 2 Windows at once and both are coding-intensive having a ton of conditions, indexes, large refresh (and update) method, and both sharing many of the same Global variables so if one changes, the other one changes too.

For example, if you go to the skills window, you can rapidly press right or left, and the window will keep up with you. Well, mine was so intensive that it couldn't do that. It'd read your input, but if you tried to press it at a fast pace, it won't read the second input. Then, if you look at the cursor itself (default cursor), it was slow blinking and wasn't blinking at the normal pace it does. Thankfully, it's all fixed since I capped one of the windows to refresh only once and only refreshes again if you do something that would require a refresh.
 

kyonides

Reforged is laughable
Veteran
Joined
Nov 17, 2019
Messages
704
Reaction score
315
First Language
English
Primarily Uses
RMXP
Have you ever tried using Graphics.frame_rate = 60 by any chance?

@Zeriab Nope, not at hand at least.
 

Lord Vectra

Master Eventer
Veteran
Joined
Dec 6, 2015
Messages
300
Reaction score
390
First Language
English
Primarily Uses
RMVXA
Have you ever tried using Graphics.frame_rate = 60 by any chance?

@Zeriab Nope, not at hand at least.
I have not. I know it obviously forces the frame rate to be 60, but how does that help me?

Also, another question about Arrays/Indexes, let's say I want to add 1 - 10 in the array, is there a quick way to do that or do I have to say myArray = [1, 2, 3, etc]
 

KK20

Just some XP Scripter
Veteran
Joined
Oct 11, 2018
Messages
473
Reaction score
192
First Language
English
Primarily Uses
RMXP
You can create a Range and convert it to an array, i.e.
Code:
(1..10).to_a
 

Latest Threads

Latest Posts

Latest Profile Posts

Now that I have linklists on large trees and cars, is there any theme that would interest you?
Debugging multiplayer issues... I'm having fun, right?
yay_streaming.jpg

For just starting streaming on the 10th... Definitely not the worst. :LZScheeze:

Granted I need 333.34 hours a month on average to achieve money. So I don't really expect that, but it sure has helped my game dev go much faster.
Proud on how this map turned out.

FnmhDLIXgAAOvIC
I just finally fixed a huge problem that wouldn’t let my game load and would crash it.. I thought that I was gonna have to trash the project and start over lol

Forum statistics

Threads
128,328
Messages
1,193,648
Members
168,813
Latest member
jasper1hu
Top