will this dangerous?? (dupping class then switching between them)

for example i create a class:

class Myclass

blablabla

end

then i dup it using

Default_Myclass = Myclass.dup

then i do edit to Myclass

class Myclass

another blablabla

end

then i dup it using

Another_Myclass = Myclass.dup

then i want in game to switch Myclass between Default_Myclass and Another_Myclass using:

Myclass = Default_Myclass.dup

or

Myclass = Another_Myclass.dup

it's working nicely... but the question is.... will this dangerous??

i'm concerned about the garbage collection. because i didn't destroy the class. (any way to destroy the class cleanly?)
Why are you duping classes?


"Destroy classes"? What does that even mean?
I think he's afraid of overloading the game engine during play and wants to know how to keep the processing of his data clean so that doesn't happen. Like using "sprite.bitmap_dispose" to clear an image that's no longer in use (though this is obviously far more complex than that).

However... I'm no scripting wiz, so I really can't be of much help.
http://stackoverflow.com/questions/20159411/what-does-it-mean-to-destroy-a-class


In any case, it looks like garbage collector takes care of classes as well.


If you're not sure, just dupe a million classes and see whether you get a memory leak.
The only thing I can think of, off hand, that might be dangerous, is having something in your save file that's referencing something that you've duped, but is NOT in the save file. Game would crash when the save file is reloaded. Maybe.
Some more links on classes and garbage collection


http://stackoverflow.com/questions/2264930/ruby-class-object-garbage-collection


The notion of creating a new `Class` instance is kind of interesting.


Thought about it for awhile and I still can't think of any reason why anyone would need to do this in the first place. Of course there may be legitimate use cases as I'm not a programming guru or anything and my opinions are biased since I'm not relatively new to programming.


1. Why would you need to switch classes at all


2. Why would you need to dup the class, when the definition is already loaded. It's a constant; just because you don't use it doesn't mean it's garbage collected
thanks for the answer hime. (and some useful link)

i'll try duping many classes many times. and see would it dangerous.

the concept is simple. i create a script to 'reset' the module and class to 'default' rtp.

first i create the

Default_Class/Module = Class/Module.dup

before any modification to default script happens.

it work successfully for scene/module that don't have instance object.

still learning to handle instance object like Game_Actor, etc (might need to read Zalerinian object reinitializer script).

i even can add prefix and make it store something like this

Victor_Scene_Battle

Yanfly_Scene_Battle

from above... the idea can be expanded to encapsulated battle system and switch it easily.

so after putting all the script needed for Victor Animated Battle. dup all the class to Victor_class/modulename

then reset the script to default.

then add script needed for Yanfly battle system. dup all the class again.

then in game will able to switch between those two. that's in theory. i know there will be lots of problem in implementation. 😀 .

only some idea i would like to play with 😀 . to improve myself on coding.
That sounds like an incredibly terrible design. Un-defining and then re-defining everything during the game?


Then again, this is based on what others have told me, and I don't have any actual constructive opinions on why it's actually a bad idea.


Maybe it isn't as bad as I make it seem.
maybe not redefining everything. just some classes and module that got changed by battle system. we can define hash to know what class/module that need to redefine when switching from A battle system to B battle system.

actually the concept more like defining everything at the start. then choose what we want to use in game. and making the class/module that used by ACE as Dynamic class and module.

i also thought it's a bad design 😀 . since there might be error when redefining things. 😀 . and so many things to considered. like instance object. and i currently have a bit trouble saving and restoring RPG::BLABLABLA::BLABABLA:BLABALAB class. so i have to create a special method for that. (outside the normal method i use earlier)

but if the process went smoothly. it would be easier to switch things around. rather than merging several battle system script and add switch/condition to know what battle system to use... (although it might be easier if only combining two battle system. but there might be something that goes wrong too...)

and this is learning subject for me i guess. 😀 .
Cloning the classes is not in and of itself dangerous, it simply creates another copy of that class with methods that can be independently modified or overwritten (think of it as "aliasing a class"). As already stated, the classes themselves are simply objects held within constants.


Of course, the best option in all of this would be to have the scripts written for seamless branching in the first place, but since we are talking about separating and encapsulating two already completed different (and very large) bodies of work without having to completely change them and being able to branch into either one at runtime, some manipulation like this may be necessary.


If I understand the intent of the latest post correctly, I actually did something similar a few years back as a proof of concept for switching between the default battle system and Tankentai battle system for VX. While I can't find that post, I believe the method I employed was some cloning of classes (Scene_Battle, Spriteset_Battle) that were later to be modified by Tankentai, before Tankenti touched them, so I would have a clean copy of the base battle system that could be later used. All of these definitions would take place on initial run of the program; when the game is actually in progress you simply choose which battle system you run, or which code branch you want to take.


There was not a whole lot of interest in it and I never rigorously tested it myself, but the point of the proof of concept was to insert some code without modifying either original script directly that would allow the two to be switched between during runtime. However, since Tankentai modified several classes (including some with active instances, such as Game_Actor), this presented a problem, as was also pointed out. The only way I could accomplish it at the time was to alias these methods AFTER Tankentai was installed, then overwrite them with the original code that would conditionally branch back into the Tankentai methods. I dropped it at this point, since needing to have to copy or be intimately familiar with code from either script would not allow me to arbitrarily encapsulate two different, and already completed (which would by default overwrite each other), code paths and simply branch into one or the other depending on the runtime conditions.


While this is a perfectly valid way of doing things, if you need to have more than a basic knowledge of the scripts you are trying to work around, you are better off just making the separate scenes into subclasses of Scene_Battle (and Spriteset_Battle, Sprite_Battler where necessary) and branching at battle start where necessary. That way, you still have "clean" copy of the default battle system, the two custom battle systems would not intersect, and you would only need to alter a few class definitions in the custom scripts to accomplish this (plus the method that triggers the battle scene and the methods that create the respective sprites/spritesets). Since there are only two entry points in the default code to call Scene_Battle and only one to create the Sprites/Spritesets, there is little effort in managing a few branches here rather than manipulating the namespace to make sure the correct branches are taken. If there were many more branches to manage than things that could change the conditions required to branch, I might be more inclined to manipulating the namespace, but I also recognize this would reduce the clarity of the code, should it need further revisions (you would technically be changing the values of constants here, which, while possible, is generally frowned upon).


You would still need to patch through those areas where they run through classes with active instances on a case by case basis. Replacing these classes completely with new instances every time you need to branch back into another battle system seems a bit wasteful, and wouldn't necessarily any easier than simply branching inside the individual methods. The main problem here is if there is a point where these classes with active instances (eg., Game_Actor) are different modified enough that you would need to completely replace the instances just to use the code bits contained within them, there is no guarantee that the instance variables will all be of the same type (simply transferring over instance variables may become problematic). Again, you also have the problem of reducing code clarity.
@mithran: thanks a lot for detailed sharing... the post really help me a lot.

i do still have problem with class that have active instance object. especially if the active instance object have something that can dynamically changed.

for creating subclass for scene_battle. actually i already try that method (combined with hijacker method i experimented).

example of hijacker method i experimented

#shopclass Game_System  attr_accessor :shoptypeendclass Scene_Shop < Scene_MenuBase  class << self    alias :est_hijacker_new :new  end  def self.new(*args)    return eval("#{self.to_s}_#{type}.new(*args)") rescue nil if type && self != eval("#{self.to_s}_#{type}") rescue false    est_hijacker_new(*args)  end  def self.type    $game_system.shoptype  endendclass Scene_Shop_2 < Scene_Shop  def selling_price    @item.price * 1000  endend #battleclass Game_System  attr_accessor :battypeendclass Scene_Battle < Scene_Base  class << self    alias :est_hijacker_new :new  end  def self.new(*args)    return eval("#{self.to_s}_#{type}.new(*args)") rescue nil if type && self != eval("#{self.to_s}_#{type}") rescue false    est_hijacker_new(*args)  end  def self.type    $game_system.battype  end  endclass Scene_Battle_2 < Scene_Battle#do overwrite or alias method here. usually to change the window to window subclass too.endbut then i got some problems. first problem module is not having instance object. thus i have problem with BattleManager module 😀 .

so i still have to add the switching in module manually 😀 . and also problem with Game_Actor. (instance object). since i cannot switch it because it's already active in party. 😀 .
Yeah, unfortunately you can't really plan for every eventuality when coding "around" things like this, eventually you have to make some kind of assumption about the modules you are trying to wrap and sticking with that. Since Ruby is a very dynamic language, it would be near impossible to predict everything everyone could do to complete a simple task, but it is (usually) safe to operate on the assumption that scripts created to work in RPG Maker are designed to work with the RPG Maker scripts.


Anyway, I don't see any real reason why your hijacker script wouldn't work except for the view instances in the code where it checks if the current scene is Scene_Battle. This is because BattleManager uses instance_of? to test scene classes, which doesn't include subclasses. You could easily change this to is_a? without breaking any of the default scripts, since none of the default scripts actually call a scene that is a superclass. You could also redefine the instance_of? for Scene_Battle to instead return is_a?, since the distinction would be irrelevant for any of the scripts we are working with.


Of course, these can also be used as examples of the very things that make scripts nearly impossible to arbitrarily separate like that; if the distinction became necessary in some other script that was added later, that would be another problem that would either have to be dealt with by using some practical coding, or another one of these out of the box workarounds. So while a few of them are fine, and can potentially save tons of time, if you constantly employ coding practices like this your code will eventually become practically unreadable.


For example, while I do like the clever use of 'hijacking' new to create a different object of a different class (something I have used myself on at least one occasion), it may not be optimal in this situation since the default code only has two entry points for calling Scene_Battle - through a step encounter and through the event manager - and it is not really logical that there would be many more entry points. If you did need more (lets say you wanted to enter battle through a scripted "arena" interface, or a script that completely bypasses or overwrites Game_Map#update_encounter), it would be a simple one-line patch at a later date. On the flipside, if you needed to conditionally replace, say, a certain sprite or window class, the hijack approach would prove very useful as instances of these classes can have multiple different entry points and are very commonly used in custom scripts.


As for the objects that have instances throughout (specifically Game_Actor), you don't have to worry about them being "in the party", the only place the actor objects are actually stored in instance variables (outside of temporary uses for windows, scenes, etc.) is within the data array in Game_Actors. If you needed to, you could replace all the members of this array with brand new instances and copy in the instance variables of their previous counterparts when necessary so long as these other references don't currently exist (eg., right before entering Scene_Battle). You would still run into the other issues that have been pointed out, though.
thanks for the instance_of? tips. i think i can hijack it too if the class have self.type value then use is.a?? 😀 .

one problem solved.

another problem that i encounter with BattleManager using that technique is because some battle system modify the BattleManager.

usually for displaying sprites. so if calling Scene_Battle_2 that i want as front view BS. it will display sideview sprite because of the BattleManager work.

but it won't be hard to make the compatibility patch between battle system i think. i only need to add some conditional to overwritten or aliased method.

really thanks for the tips you gave me. it help a lot.

Original thread: https://forums.rpgmakerweb.com/threads/will-this-dangerous-dupping-class-then-switching-between-them.20259/