Skip to main content
gamedev.net
🔒 Locked

[.net] Cloning objects

Started by Barguast 5 replies 1.5k views
Original Post
Barguast
Barguast
Can someone tell me how I can create a copy of a object whose class does -not- implement a Clone method? I'm creating a DirectInput wrapper and need to keep a record of the previous KeyboardState, but since this doesn't implement ICloneable::Clone, I can't find any way to do this. Thanks
Using Visual C++ 7.0
ernow
ernow
<<>>

Why don't you store a reference to the old keyboardstate and read the new keyboardstate into another reference?
Pseudo:

newkeystate = GetKeystate();
if(oldkeystate != null)
{
compare(newkeystate, oldkeystate)
}
oldkeystate = newkeystate;


No cloning needed this way.

Cheers

[Edited by - ernow on October 28, 2004 9:55:41 AM]
Axiverse
Axiverse
err... i don't know if this will work at all...

but if you can inherit it then you can make a clone function and clone using the object function MemberwiseClone
Barguast
Barguast
@ernow - That's what I tried to do last time, but for some reason obtaining the new KeyState reference would also change the old KeyState reference. There is a good chance I was simply doing it wrong so I will try again.

@Axiverse - I did consider doing that, but I wasn't sure if it was such a good idea. I'll try it if the above method doesn't work.

Thanks
Using Visual C++ 7.0
Talonius
Talonius
If the new variable is changing the old variable then you're not making a deep copy of the variable, you're making a shallow copy -- which is basically just a pointer.

That is, this isn't effective...
KeyboardState oldState = currentState;


You've created a new way to reference currentState -- you haven't copied the data from currentState.

Since the class doesn't have a setter you'll need to keep manual track of the state.

Below is some code -- I did not write it -- taken from http://axiomengine.sourceforge.net/modules.php?name=Forums&file=viewtopic&p=1174. Here you can see he had the same problem you're having with a shallow vs deep copy, and wrote his own class to keep track of the old keyboard state.

public class myKeyboardState{   private string[] keyNames;   private bool[] keyStates;   private int keyCount;   public myKeyboardState()   {      keyNames = Enum.GetNames(typeof(DInput.Key));      keyCount = keyNames.Length;      keyStates = new bool[keyCount];   }   public bool this[DInput.Key key]   {        get      {         string s = Enum.GetName(typeof(DInput.Key),key);         return keyStates[Array.IndexOf(keyNames,s)];      }   }   public void CopyState(KeyboardState keyboardState)   {      for(int i = 0; i < keyCount; i++)      {            keyStates = keyboardState[(DInput.Key)Enum.Parse(typeof(DInput.Key),keyNames)];      }   }}


For more information about copying and cloning check this OnDotNet article. It's worth the read and the time to understand what's going on. It will save you tremendous heartache later on.

Hope that helps.
..what we do will echo throughout eternity..
ernow
ernow
Whoops! I just used Reflector to find out what GetCurrentKeyboardState does. It just returns a reference to an internal Keystate object that first is updated.

So... The fact that my code 'works' is just because of the way I rely on it... arg!

Back to the drawingboard!

We live and learn, such is life and it's getting such-er and such-er...

Cheers

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.