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.

[resolved] copy and edit objects...

I'm having a problem. I'm trying to create a scene that will load a data object and edit it similar to the Database editor, however I'm adding things to the class, so I need a different editor. I'm just running into a problem detecting if a change has been made. as seen in my example below, $actor != $actor_copy will always return false.

How do I copy an object and edit the copy without changing the original? for example:

[rgss]$actor = RPG::Actor.new
$actor_copy = $actor
$actor_copy.id = 1
p $actor.id, $actor_copy.id #=> returns [1, 1]. I want it to be [0, 1]
[/rgss]

And before you suggest it, $actor_copy = $actor.clone or $actor_copy = $actor.dup does the same as the example, except $actor != $actor_copy will always return true, even if I don't make a change to either.
 
Thank you, but I have tried those. the issue is, whenever i make a change to the copy, it changes the original as well so they are always going to be the same.

I'm not looking for a way to compare them, but a way to make changes to the copied object while leaving the original one alone.

If this can't be done, I might have to make my apply button "save and continue" and my OK button "save and quit" ... actually, now that I think about it, that might be best in the long run anyway. I'd still like to know how to do this though, either way. Or if it is even possible to do.
 
Ha Ha! I think I figured it out.

I created my own Alias module, now i would like some feedback on how i might improve it.. As it is, i have to add a def copiable to any class I want to be able to "copy" with it. here it is:

[rgss]module Alias
  def self.object(obj)
    if obj.respond_to?:)copiable)
      copy_obj = obj.class.new
      for i in 0...obj.instance_variables.size
        copy_obj.instance_variables = obj.instance_variables
      end
      return copy_obj
    else
      print "Invalid argument"
      exit
    end
  end
  def self.array(ary)
    copy_ary = []
    for x in 1...ary.size
      copy_ary 
 

Zeriab

Sponsor

Why not just use .clone or .dup?
Both of them creates shallow copies which is essentially what you are doing with your Alias.object method. (There are some differences between what other properties are copied such as the frozen and tainted state)

If you want a two-level cloning of an array I suggest you add a method that clones each object in the array. It can more generally be put in the Enumerable module.

Getting a deep copy is more tedious which requires you to add a deep copy method in all the objects you use unless you want to use hacks such as Marshal.load(Marshal.dump(obj))

*hugs*
 
Because, when I used .clone or .dup, and then changed .id, or even .name on either the original or the clone/duplicate, it changed on both. However, with the way my method works is instead of being a clone or a duplicate, it is a completely different object but should be equal to the original if tested. that way, any changes i make to the new object does not reflect on the original and vice versa.
 

Zeriab

Sponsor

This works perfectly fine:
[rgss]$actor = RPG::Actor.new
$actor_clone = $actor.clone
$actor_clone.id = 1
$actor_copy = $actor.dup
$actor_copy.id = 2
p $actor.id, $actor_clone.id, $actor_copy.id
[/rgss]

If you have a bunch of actors in an array then you should make a method which clones itself and each of the objects it contains.

*hugs*
 
ok. it does work now. why didn't it work before?

And i just tested the comparison if I used my method and it works just like .clone, .dup: it's always false.

I suppose instead of trying to load the Actors.rxdata into $actors(etc) then copying it into @actors, making my scene make changes to @actors and test to see if @actors is not equal to $actors in order to tell if changes were made and if so, activating the Apply button; and when the Apply button is pressed copying @actors back to $actors, only using the OK button to save $actors or @actors back to Actors.rxdata and quit, I will just cut out the middle man(@actors) and edit $actors directly, leaving the Apply button always active and have it save to Actors.rxdata and have the OK button also save to Actors.rxdata but then quit.

This makes it so my editor doesn't work exactly like the rmxp database editor, but it will suffice, and might actually be better since If you click apply the (X) out of the game.exe it will not actually have saved anything using the previous methods but with the latter methods, as long as you click Apply every now and then, or information will be saved.

Thank you all for your help.
 

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