Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3670198
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:20:07+00:00 2026-05-19T02:20:07+00:00

Let me first try to explain what I’m trying to achieve. I’m making a

  • 0

Let me first try to explain what I’m trying to achieve.

I’m making a game where the player can pickup power-ups(which I called, for some reason, “Loot” in my game). So here’s a really basic form of my Loot class:

public class Loot : Sprite
{
    //Properties and fields...

    public Loot(/*parameters...*/)
    {
    }

    public virtual OnPickUp(Player player)
    {
    }
}

Player and Sprite are classes from me. Here’s my problem: my sprite class holds a position(a Vector2, a struct from XNA representing a vector) and from my understanding of how C# works, copying two sprites will copy the reference of eachother, therefor, whenever I change one’s position, it will change the other one’s too.

Whenever a loot should spawn, I have a class holding the possible loots that can spawn which returns a random one and the loot is copied and shown to the screen, but as soon as I get more than one of the same type of loot, it messes up because of the reference copy problem (both will share the same position and only one will be visible).

So then I tried creating a new Loot object whenever I needed to spawn a new loot, so I had to provide a constructor with multiple parameters to easily copy a loot this way:
(Let’s pretend toSpawn can be any type derived from the Loot class, like a HealthPack, or whatever)

Loot spawning = new Loot(toSpawn.Position, toSpawn.Color /*etc...*/);

It looked right until I decided to implement “OnPickUp”. Since I create a new Loot object rather than the proper child class, the functionality of toSpawn‘s OnPickUp function would disappear and picking up a loot would end up doing nothing(it would only call the base class Loot’s empty function).

Obviously, I am doing something wrong here. I don’t have much programming experience, and even less with C#, so I really have no idea of how I can fix this. I tried using an Action< Player> object representing the OnPickUp functionality, it would work but still limit me quite a lot(since I have to pass a static function as an Action< Player>, therefor limiting me to use the information of the player parameter and preventing me from using the sub-class specific information).

So, finally, my question really is: What would be a better design to allow having sub-classes with overloaded functions, but still being able to clone the base class and its properties?

Thank you for your precious time.

If anything is not clear enough(and I know it’s not), then just ask me in the comments and I’ll try to describe my problem in more details.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T02:20:08+00:00Added an answer on May 19, 2026 at 2:20 am

    have your Loot class implement ICloneable

    public abstract class Loot : ICloneable
    {
        public virtual object Clone()
        {
            Type type = this.GetType();
            Loot newLoot = (Loot) Activator.CreateInstance(type);
            //do copying here
            return newLoot;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright let me explain my situation first: I am part of an organization that
First let me say that I really feel directionless on this question. I am
Let me state first: I know that any user that wants to run a
Let's say the first N integers divisible by 3 starting with 9. I'm sure
Let's say I have 3 point clouds: first that has 3 points {x1,y1,z1}, {x2,y2,z2},
First, let's get the security considerations out of the way. I'm using simple authentication
First off, let me start off that I am not a .net developer. The
First of all, let's define a few tables: Users table will store information about
First of all, let me say I am very new to rails, have been
First off, let me preface this question by stating that I'm really a pretty

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.