Sign in to follow this  
CogOfHate

Unity How To Copy Array of Derived Classes Without Pointers

Recommended Posts

Hello Everyone,

 

I have a problem I am currently facing and I'm wondering if the community could help me out.  I find it's probably easier to explain with an example.

struct Base
{
};

struct Derived : public Base
{
    Derived(float newX) : x(newX)
    {
    }
    
    float x;
};

struct Unrelated : public Base
{
    Unrelated(char val) : v(val)
    {
    }
    
    char v;
}
...
 
// How to copy the data that's in mBaseArray into another struct?
// Don't want pointer copy as it's not thread safe.
 
// Below is a very simple example.
 
Module1
-----------
class Module1
{
...
    Array<Base*> mBaseArray;
}; 

 

- mBaseArray is populate in some fashion.  Notice the use of polymorphism here.
 
mBaseArray.push_back(new Derived(10));
mBaseArray.push_back(new Unrelated('c'));  
 
... Some sync point here
... After this point, I want to copy the information that is in mBaseArray into Module2's mWorkingArray.
 
Module2
-----------
class Module2
{
...
    Array<Base*> mWorkingArray;
};

 

- Want to copy the data from mBaseArray into Module2's own array.
- Copied data from mBaseArray without the use of pointers.  Want only data.
- Module2 contains a similar declaration to mBaseArray.
 
Is there a way to accomplish this?  Or is there some other method that will solve this problem?
 
If there are better ways of doing what I'm trying to accomplish, I am more than welcome to suggestions.
 
Thanks.

Share this post


Link to post
Share on other sites

If you really need to keep the polymorphism, one option is to add a virtual Clone method to the Base class. Then it will be the responsibility of each subclass to implement this Clone method by creating a new instance of its type and initializing it accordingly. You can then Clone the objects into the new array.

 

Otherwise, revisit whether you actually need polymorphism.

Share this post


Link to post
Share on other sites

Thanks for the suggestion.  I think you were right and I was thinking about stuff in all the wrong way.  I have since moved away from polymorphism in favour of a better approach.  I think I can achieve what I want through proper message passing instead of the convoluted way I was previously using.

Share this post


Link to post
Share on other sites
class Module1
{
...
    Array<Derived> mDerivedArray;
    Array<Unrelated> mUnrelatedArray;
};
mDerivedArray.push_back(Derived(10));
mUnrelatedArray.push_back(Unrelated('c'));
class Module2
{
    Module2(Array<Derived>& d,Array<Unrelated>& u) :
        mDerivedArray(d),
        mUnrelatedArray(u) {}
    Array<Derived>& mDerivedArray;
    Array<Unrelated>& mUnrelatedArray;
};

How about doing it this way? Would be much more efficient and if you dont add a huge class hierarchy it wouldnt hurt.

You can just treat both arrays the same for some types of work if you like. It may be even easier to process the data if you dont throw away the knowledge of the exact type without being forced to and have similar things sorted together for some other types of work.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Announcements

  • Forum Statistics

    • Total Topics
      628301
    • Total Posts
      2981914
  • Similar Content

    • By muhamad rabee
      My first mobile game made with unity

      iphone: https://itunes.apple.com/us/app/aa-countdown/id1314223584?ls=1&mt=8

      android: https://play.google.com/store/apps/details?id=com.mr.AACountDown

      I appreciate every suggestion
    • By CpawsMusic
      Hey there! My name is Cpaws and I compose music, sound effects and overall sound design for video games and film. I'm looking to work on a horror game mainly for fun and as an addition to my portfolio. I've used Unity & Wwise before for audio implementation.
      If you're interested in working together, don't hesitate to contact me at cpawsmusic@gmail.com
      Here's a few demo reels of my past projects: 
      Here's some snippets of some quick game music I've composed: https://soundcloud.com/cpawsmusic/sets/cpaws-video-game-film-music
      Here's my portfolio/website: https://CpawsMusic.com/
      E-mail: cpawsmusic@gmail.com
    • By Simplepg
      https://play.google.com/store/apps/details?id=simple.gplay.GamesInABox
    • By Simplepg
      https://play.google.com/store/apps/details?id=simple.gplay.GamesInABox
    • By ForgedInteractive


      Who We Are
      We are Forged Interactive, a small team of like-minded game developers with the sole purpose of making games we love! We're a team of artists, animators, programmers, level designers, writers, composers, producers, and other creative minds. We want to make games that you, the modern gamer want to play! We hope to build a community that enjoys our games as much as we love creating them. With your feedback and support we will be able to achieve that.

      About the Game
      GAME NAME is a fun, action-packed army builder with unique characters, challenges and engaging levels. Set forth on an adventure to protect friends, family and countrymen from new adversaries. Once defeated your enemies turn coat and join you in your adventures. Players can enjoy a range of troops and abilities based on their gameplay style which become more important as maps introduce more challenging terrain, enemies and bosses. Strong orc knights, dangerous shamans, and even a dragon are out on the prowl. Knowing when to fight and when to run, and how to manage your army is essential. Your actions alone decide the fate of this world.

      Previous Work by Team
      Although we are working towards our first game as a team, our team members themselves have past experience in the industry.
      This includes members who have worked on titles including:
      Final Fantasy Kingsglaive, FIFA, Xcom 2 and Civilization.

      Who are we looking for? 3D Modellers Concept Artists Marketing Specialists Level Designer

      What do we expect? Reference work or portfolio. Examples what have you already done and what projects you have worked on academic or otherwise. The ability to commit to the project on a regular basis. If you are going on a two-week trip, we don't mind, but it would be good if you could commit 10+ hours to the project each week. Willingness to work with a royalty based compensation model, you will be paid when the game launches. Openness to learning new tools and techniques
      What can we offer? Continuous support and availability from our side. You have the ability to give design input, and creative say in the development of the game. Shown in credits on websites, in-game and more. Insight and contacts from within the Industry.
      Contact
      If you are interested in knowing more or joining. Please email or PM us on Skype. Myself or Colin will reply to you within 48 hours.

      E-mail: Recruitment@ForgedInteractive.com
      Skype: ForgedInteractive

      Regards,
      David and Colin

      Follow us on:

      Facebook: https://www.facebook.com/ForgedInteractive/
      Twitter: @ForgedInteract
      Youtube: https://www.youtube.com/channel/UCpK..._as=subscriber
      Reddit: https://www.reddit.com/user/Forged_Interactive/
  • Popular Now