Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Limnage · Dec 02, 2020 at 02:46 PM · oop

Mapping input objects to effect objects in a typesafe way

The problem I'm trying to solve is conceptually very simple.

I'm trying to make an ability consist of a list of input objects and a list of effect objects. The input objects define types of user interactions (like selecting a ground tile or selecting a unit), and each input object will get a certain type of target from the user (e.g. tile or unit). The effect objects define effects on a specific type of target (for example moving to a ground tile or damaging a unit).


Using an ability in code would consist of:

  • Prompting the user input objects sequentially (waiting for the user to finish the input for each one before moving on to the next)

  • Executing the effects sequentially (each effect will take get its data from the input object it is mapped to)

So an example of a defining an ability could be:

 Ability: {
    Inputs: {
        Select Tile,
        Select Unit
    }
 
    Effects: {
        Move to (input 1),
        Deal 10 damage (input 2)
    }
 }

Ideally I want to make the mapping from input to effect typesafe, since each type of effect has a type of target data that it's expecting (e.g. tile or unit). Here's an example of code I wrote to represent the action, input, and effect objects in code:

         public class AbilityData
         {
             List<InputData<Target>> inputs;
     
             List<AbilityEffect> effects;
     
             public void exampleCreate()
             {
                 BaseInputData<Tile> input = new TileSelectData();
                 inputs.Add(input);
     
                 effect = new List<AbilityEffect>();
                 BaseAbilityEffect<Tile> effect = new BaseAbilityEffect<Tile>(new TileEffect(), input);
                 effects.Add(effect);
             }
             public void exampleRun()
             {
                 foreach (InputData<AbilityInput> input in inputs)
                 {
                     input.promptInput();
                 }
     
                 foreach (AbilityEffect effect in effects)
                 {
                     effect.execute();
                 }
             }
         }
        public interface AbilityEffect
         {
             void execute();
         }
     
         public class BaseAbilityEffect<T> : AbilityEffect where T : Target
         {
             InputData<T> input;
     
             TargetEffect<T> effect;
     
             public BaseAbilityEffect(TargetEffect<T> tEffect, InputData<T> input) {
                 this.input = input;
                 this.effect = tEffect;
             }
     
             public void execute()
             {
                 effect.execute(input.getInput());
             }
         }
     
         public class TargetEffect<T> where T : Target
         {
             public virtual void execute(T input) { }
         }
     
         public class UnitEffect : TargetEffect<Unit>
         {
             public override void execute(Unit unit)
             {
                 // Do something on the unit
             }
         }
     
         public class TileEffect : TargetEffect<Tile>
         {
             public override void execute(Tile tile)
             {
                 // Do something on the tile
             }
         }
     
         public interface InputData<out T>
         {
             void promptInput();
     
             T getInput();
         }
     
         public abstract class BaseInputData<T> : InputData<T> where T : Target
         {
             public abstract T getInput();
     
             public abstract void promptInput();
         }
     
         public class TileSelectData : BaseInputData<Tile>
         {
             public override Tile getInput()
             {
                 // Return the stored input
             }
     
             public override void promptInput()
             {
                 // prompt for the input and store it.
             }
         }
     
         public class UnitSelectData : BaseInputData<Unit>
         {
             public override Unit getInput()
             {
                 // Return the stored input
             }
     
             public override void promptInput()
             {
                 // prompt for the input and store it.
             }
         }


This seems like it will work fine in the normal local case, but the problem arises when you need to provide an override for the input. For example, in a networked game, the clients will trigger the input and then send the targets to the master server. The master server would then need to override the input objects to have the targets it received, and then call the effects.

So I want to add something like

 void overrideInput(T);

to the InputData interface, but because it uses covariance, I can't use the generic type parameter as a parameter to any of the interface function.

Is there a way around this limitation? The concept seems very simple: ensure that the effect objects are only matched with input objects of the same target type. Of course I could accomplish this in brute force with a bunch of unsafe casts, but it seems like there should be a better way.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

209 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Building Scripts on each other 1 Answer

Player's God animator vs separate animators? 1 Answer

Basic C# Syntax & Knowing What Is Needed 2 Answers

[SOLVED] What happens when I call a virtual void that is not overwritten? 2 Answers

,Why do I get the same value from object property? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges