So, I finally purchased MV last night and I feel like I'm a newb at scripting agian! :(
I have gotten pretty proficient at scripting in Ruby. But Javascript is like a different planet right now. I've spent much of the past 15 hours studying Javascript from tutorial websites and watching videos. But I have a few questions regarding how it compares to Ruby.
First, if I'm understanding this correctly, 'this' is basically the same as 'self' in Ruby. Is that right?
Secondly, the accessors of Ruby, 'attr_accessor', 'attr_reader', 'attr_writer', provide a way for you to make variables within a class accessible from outside the class itself. What's the equivalent in Javascript?
Thirdly, what about the Ruby Hash? Is there something similar in Javascript? I like the way the Ruby hash keys can be anything, string, integer, array, class, etc. They are very useful. So I was wondering is there an equivalent? Or is must I just use JS Arrays?
Fourthly, the 'module' of Ruby. Do I need to just set constant variables in the class I'm working in? How would this be used by the script's users? One of the cool things about Ruby is that you could provide an easily accessible area for the user to adjust things to their settings in the module.
Finally, 'aliasing'. What's the deal on that? Do I just have to overwrite methods that I plan to alter?
I hope this isn't too much to ask. I really want to start coding with MV and I'm trying to grasp the concepts of Javascript as it relates to Ruby. The syntax, I know, will just come with time and practice. So any help you can provide would be greatly appreciated.
Finding Ruby/Javascript Similarities
● ARCHIVED · READ-ONLY
-
-
First, if I'm understanding this correctly, 'this' is basically the same as 'self' in Ruby. Is that right?
In short, yes.
Secondly, the accessors of Ruby, 'attr_accessor', 'attr_reader', 'attr_writer', provide a way for you to make variables within a class accessible from outside the class itself. What's the equivalent in Javascript?
They are uncessary. You can access every function and property of an object unless you set restrictions on that. You can also just make a function to access the variable.
Class.prototype.getVariable = funciton() {
return this._variable;
}
Thirdly, what about the Ruby Hash? Is there something similar in Javascript? I like the way the Ruby hash keys can be anything, string, integer, array, class, etc. They are very useful. So I was wondering is there an equivalent? Or is must I just use JS Arrays?
Javascript objects works simiary to hash, even when defining them. Although the keys should be strings as it will stringfy anthing that is not a string.
for example: var object = {id: X, value: Y} ,
You can access the property 'id' by either using object.id or object['id'].
Fourthly, the 'module' of Ruby. Do I need to just set constant variables in the class I'm working in? How would this be used by the script's users? One of the cool things about Ruby is that you could provide an easily accessible area for the user to adjust things to their settings in the module.
Making a module with constants for the user to setup is also uneccessary. RM MV comes with the Plugin Parameter. That you can set on the plugin header and allows the user to input values that will affect the plugin. If you wish, the user don't need to lay a finger on the code to make all the setup they need (provided you gave them all the options needed with the plugin parameters).
Finally, 'aliasing'. What's the deal on that? Do I just have to overwrite methods that I plan to alter?
You can alias functions.
Code:_aliasFunction = Class.prototype.dothis; Class.prototype.dothis = function(args) { _aliasFunction.call(this, args); } -
They are uncessary. You can access every function and property of an object unless you set restrictions on that. You can also just make a function to access the variable.
So, in other words, you must create functions that both set and get the values of a variable. There's no simple modifying the variable with a:
class.variable = x
var object = {id: X, value: Y} ,
So this creates an object on the fly? Automatically setting the values of 'id' and 'value'? In that regard, you'd be creating objects inside other objects and saving them as variables. Is that right?
_aliasFunction
I noticed that a lot of the scripts I've seen in JS have this underscore + name thing going on. Is that a rule, or just preference? -
So this creates an object on the fly? Automatically setting the values of 'id' and 'value'? In that regard, you'd be creating objects inside other objects and saving them as variables. Is that right?
Yes.
So, in other words, you must create functions that both set and get the values of a variable. There's no simple modifying the variable with a:
class.variable = x
No, javascript is far more flexible on that.. you can access and change the values of variables as you want, although people says that it not a good practice to access/change private variables outside of their class (I don't know the reason).
For example. On ruby, to get a actor sprites array data (wich was stored on the Spriteset_Battle @actor_sprites local valriable) you would need to create an accessor/reader/method for it on the Spriteset_Battle, then you need to you needed to create an accessor/reader/method on the Scene_Battle for the variable "@spriteset" , Only then you could use 'SceneManager.scene.spriteset.actor_sprites' to get the actor sprites data.
On javascript, just use "SceneManager._scene._spriteset._actorSprites" and you will have access to the actor sprites array data.
I noticed that a lot of the scripts I've seen in JS have this underscore + name thing going on. Is that a rule, or just preference?
Preference. Javascript don't make a distinction for some types of variables and objects as ruby does (@ for local variables, for example), so people use the undesrcore as a way to identify private variables. But it's not mandatory and don't impose any restrictions as the lack/presence of '@' do in ruby. But it's good to do give a visual clue of what is going on. -
Be very careful treating `this` like `self`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this
-
For example. On ruby, to get a actor sprites array data (wich was stored on the Spriteset_Battle @actor_sprites local valriable) you would need to create an accessor/reader/method for it on the Spriteset_Battle, then you need to you needed to create an accessor/reader/method on the Scene_Battle for the variable "@spriteset" , Only then you could use 'SceneManager.scene.spriteset.actor_sprites' to get the actor sprites data.
On javascript, just use "SceneManager._scene._spriteset._actorSprites" and you will have access to the actor sprites array data.
Yes! I have had to do that before in a script I made.
No, javascript is far more flexible on that.. you can access and change the values of variables as you want, although people says that it not a good practice to access/change private variables outside of their class (I don't know the reason).
I've heard people say that too. In fact, in my earlier days of scripting, I always had to create accessors because I was dependent on them for changing private variables. But as I progressed, I found and utilized the 'get' and 'set' methods of Ruby more often. It just means there's a little more coding, but I think it makes code a little cleaner. I think when Ace came out they added the 'show' and 'hide' methods of windows, for example. That made it so you didn't have to keep setting the 'visible' variable of windows. I know that in large window-based scenes, it can get irritating to open and close, activate and deactive windows. I know this is beside the point, but it led me to create this Ruby script for quick window operations for RPGMakerVX:
SpoilerSpoiler
Code:
class Scene_Base
#-----------------------------------------------------------------------------
# Switch Window Control
#
# old_window : window that is being deactivated, pass 'nil' to skip deactivation
# new_window : window that is being activated, pass 'nil' to skip activation
# hide_windows : windows that should be hidden
# show_windows : windows that should be shown
# refresh_windows : windows that should be refreshed
# set_index : index of the new window
#-----------------------------------------------------------------------------
def switch_window_control(old_window, new_window, hide_windows = [],
show_windows = [], refresh_windows = [], set_index = nil)
unless old_window.nil?
old_window.active = false
end
unless hide_windows.empty?
for i in 0...hide_windows.size
hide_windows.visible = false
end
end
unless refresh_windows.empty?
for i in 0...refresh_windows.size
refresh_windows.refresh
end
end
unless show_windows.empty?
for i in 0...show_windows.size
show_windows.visible = true
end
end
unless set_index.nil? || new_window.nil?
new_window.index = set_index
end
unless new_window.nil?
new_window.active = true
end
end
end
The code cut through all the window operations and consolidated it to one method, 'switch_window_control'.
so people use the undesrcore as a way to identify private variables.
Okay. I guess it replaces the '@'. This would definitely help to make code more understandable. I'll try to maintain this preference.
Be very careful treating `this` like `self`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this
Admittedly, I didn't read all of this, Lemur. But if I get the gist of what it's saying, it says that basically, if you are monitoring the state of an object in several different places within the code, and you haven't 'updated' the object, you could actually be gathering values from an object's previous state. Am I understanding that correctly?
Thanks for all the help! :)