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 938119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:35:24+00:00 2026-05-15T21:35:24+00:00

In my game I have a base class of Loot which has methods universal

  • 0

In my game I have a base class of Loot which has methods universal to anything which can be picked up by the player and stored in his inventory. This would include potions, equipment, ammo, etc. I can equip arrows, but not potions. So Arrow would be a subclass of Ammo, which would ultimately derive from Loot. I can drink a potion but not an Arrow. So Potion would subclass Loot but implement IConsumeable. And so on.

Loot objects have a Quantity property (10 arrows; 2 potions). In my Loot class I have a method called Split which allows the player to take a “stack” of items (like arrows) and split it into two separate stacks. So it decreases the Arrow instance’s Quantity by a certain amount, then returns a new Arrow instance with a Quantity value = to that which was taken from the original instance.

My idea was that I’d write the method in Loot since any Loot can be stacked so long as its int StackLimit property is greater than 1. After decrementing the calling Loot by the quantity specified, I’d return a new object of the same type. The problem is, I don’t know what type of Loot subclass that object will be.

public abstract class Loot
{
    public int Quantity { get; set; }
    public Loot Split(int quantityToTake)
    {
        Loot clone = (Loot)this.MemberwiseClone();
        //RestrictNumberToRange(int min, int max, int value) is a delegate which clamps a value to min,max
        this.Quantity -= Utility.RestrictNumberToRange<int>(1, this._quantity - 1, quantityToTake);
        clone.Quantity = quantityToTake;
        return clone;
    }
}

Is this a really poor way to go about this? I thought about Reflection, but I hear mixed opinions on whether or not to use it in a case like this.

Is there no way to define the method to deal with this.FurthestSubClass?

I know my subclasses may have different constructors, so it’s probably not feasible to try and return ‘new this.FurthestSubclass()’ because I can’t know how to construct it. But I’d like to be able to deal with its Loot methods, so I’m using Loot for the return type.

  • 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-15T21:35:24+00:00Added an answer on May 15, 2026 at 9:35 pm

    I think this is a good case for generics. Try rewriting your split method in Loot like this:

    public TLoot Split<TLoot>(int quantityToTake) where TLoot : Loot
    {
        TLoot clone = (TLoot)this.MemberwiseClone();
        ...
        return clone;
    }
    

    That ought to take care of typing issues.

    EDIT TO ADD: The constructor issue is a bit more interesting, but you may find parameterless constructors useful, in combination with object initalizers. If you modify the constraint as follows:

    public TLoot Split<TLoot>(int quantityToTake) where TLoot : Loot, new(TLoot)
    {
        // stuff
        TLoot newLoot = new TLoot();
        ...
        return newLoot;
    }
    

    The “new(T)” constraint allows you to create new objects based on the generic type.

    FURTHER EDIT: I should give an example of object initializer in this context:

    TLoot newLoot = new TLoot { Quantity = quantityToTake };
    

    This assumes that Loot has a public property called Quantity. Object initializers can set values for any public property that has a public set{};

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this game where balloons are coming from bottom and player has to
I have a base class Vehicle and a derived class Car. package Game {
Imagine I have a game with a base Entity class, with an init method
I have, for my game, a Packet class, which represents network packet and consists
While developing a game I have designed a class that in normal operation has
In my game I created a base class called Entity which I store in
Referencing question #2013421, I have the following RoR models: class Game < ActiveRecord::Base has_many
I have a class structure for a role playing game which looks like this...
I have two models as follows: class Game << ActiveRecord::Base has_many :bells end class
I'm writing an AS3/Flex4 game engine, and I want to have a base class

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.