When to use and when to not use a prototype to add a function to a class in RPG Maker and JavaScript projects overall? I have used it with BattleManager in my RPG Maker MV project, and it didn't work. I used it like this: BattleManager.prototype.MyFunc, and it didn't work. However, I have used BattleManager.MyFunc, and it has worked. Maybe because BattleManager isn't an RMMV class? If not, what is it?
MV When to use and not to use a prototype when adding a function to an object?
Learning JavascriptA quick and the simplest explanation would be like this
BattleManager is not saved in a savefile when you save the game, but
Game_Player is saved
You use BattleManager directly by using
But you don't use Game_Player directly. Instead, you use $gamePlayer.
BattleManager is not saved in a savefile when you save the game, but
Game_Player is saved
You use BattleManager directly by using
BattleManager.functionName()But you don't use Game_Player directly. Instead, you use $gamePlayer.
I don't think it has anything to do with what is being saved. Maybe it has something to do with it not being a class.
I see creating a Game_Player class by using Game_Player rather than $gamePlayer. $gamePlayer is an object.
I see creating a Game_Player class by using Game_Player rather than $gamePlayer. $gamePlayer is an object.
It doesn't have anything directly to do with it - @TheoAllen didn't say "BattleManager doesn't use prototypes because it doesn't get saved" - but it functionally is related.
We're not sure how technical you want your answer, because if you want the full-blown technical reasons it's easy to Google "JavaScript prototype" and read the documentation. So you seem to want a simpler explanation than that, and we're trying to provide it.
Another simplified way to think about it is - do you have multiple instances of that type of object in the game?
You can have several/many actors, so the Game_Actor objects use prototypes for their methods.
There is only one BattleManager, it is not prototyped.
A medium level of technicality is: BattleManager is a static class (you can even see that if you go to where it's defined in rpg_managers.js, line 2133). Static classes do not use prototypes.
You're not creating any Objects of the BattleManager type in your RPG Maker game - there's no line that creates a variable and sets it equal to a
new BattleManagerYou're just calling all of its methods and referencing its properties directly.
@ATT_Turan yes, you've explained it well to me now! I see BattleManager is a static class and static classes do not get instanced and do not have prototypes. Thanks!
Original thread: https://forums.rpgmakerweb.com/threads/when-to-use-and-not-to-use-a-prototype-when-adding-a-function-to-an-object.159081/