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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:16:41+00:00 2026-06-12T11:16:41+00:00

Let’s say I have a Player class. It has a Move method that takes

  • 0

Let’s say I have a Player class. It has a Move method that takes in a direction parameter, a ChangePosition method that takes in two ints, a Disappear void method, and others.

I want to allow the user (probably using listbox) to be able to add some of those methods to the Player object’s behavior. For example, they could choose to have it move right and then move down and then disappear. The methods that they choose would then be added to an event, so that when that event occurs, the methods that they chose would execute.

Has anyone done something like this before? What is the best way to do this?
(I’m 13 and I don’t know much of design patterns)

  • 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-06-12T11:16:43+00:00Added an answer on June 12, 2026 at 11:16 am

    First of all, you should put all the game logic into an Engine project, with an Engine class, which is responsible for that alone (handling all the game’s logic and communicating with the UI and Framework [and lower layers, if there are any]).

    Second, you can have an EngineAction class, which has an enum EngineActionType (Move, ChangePos, Disappear, …), and other properties. The general idea is this:

    Have an enum to hold the types:

    public enum EngineActionType
    {
        Move,
        ChangePos,
        Disappear,
        ...
    }
    

    Create an abstract class to group all the engine action classes logically and add them with the common Type property:

    public abstract class EngineAction
    {
        public readonly EngineActionType Type;
    
        protected EngineAction(EngineActionType type)
        {
            this.Type = type;
        }
    }
    

    Create a class for each engine action that holds all needed parameters as properties. It should derive from EngineAction and send the appropriate EngineActionType to the base constructor:

    public class EngineActionMove : EngineAction
    {
        public int XDelta;
        public int YDelta;
    
        public EngineActionMove() : base(EngineActionType.Move)
        {
        }
    }
    
    public class EngineActionChangePos : EngineAction
    {
        ...
    }
    
    ...
    

    After all that, you can put those objects in a list, as the user orders, and the iterate over it, performing them one by one according on their types:

    foreach (EngineAction action in actionsToDo)
    {
        switch (action.Type)
        {
            case EngineActionType.Move:
                EngineActionMove mvAction = (EngineActionMove)action;
    
                // Example:
                player.Position = player.Position.Offset(mvAction.XDelta,
                                                         mvAction.YDelta);
                break;
            case EngineActionType.ChangePos:
                EngineActionChangePos cpAction = (EngineActionChangePos)action;
    
                // Example:
                player.Position = new Point(cpAction.X, cpAction.Y);
                break;
            case EngineActionType.Disappear:
                // Just make the player disappear,
                // because the operation needs no parameters
                player.Disappear();
                break;
            case ...:
            default:
                // maybe throw an error here, to find errors during development
                // it helps you find non-handled action types
        }
    }
    

    Further tip: The least logic you have in the UI, the better. Always.

    Edit: Great idea by Charleh:

    public interface IEngineAction
    {
        void Do(Player target);
    }
    
    public class EngineActionMove : IEngineAction
    {
        public int XDelta;
        public int XDelta;
    
        public void Do(Player target)
        {
            // Example
            target.Position = ...
        }
    }
    
    public class EngineActionDisappear : IEngineAction
    {
        public void Do(Player target)
        {
            // Example
            target.Visible = false;
        }
    }
    
    ...
    

    Adding to list example:

    var actionsToDo = new List<IEngineAction>();
    
    actionsToDo.Add(new EngineActionDisappear());
    actionsToDo.Add(new EngineActionMove { XDelta = 10, YDelta = 20 });
    

    And iteration:

    foreach (IEngineAction action in actionsToDo)
    {
        action.Do(player);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have two objects, Master and Slave . Slave has a method
Let me explain best with an example. Say you have node class that can
Let's say you have a method that expects a numerical value as an argument.
Let's say you have a class, with certain properties, and that you tried your
Let's say I have two assemblies: BusinessLogic and Web. BusinessLogic has an application setting
Let's say we have a Java object that has only primitive fields marked as
Let's say I have a method in java, which looks up a user in
Let's say that I have a SQLite database that I create in a separate
Let's say I have two tables orgs and states orgs is (o_ID, state_abbr) and
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>

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.