Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 /
avatar image
0
Question by Hsmblada · Aug 11, 2015 at 12:02 PM · c#listdelegatequeueturn

Queueing methods and calling them at specific times

So I'm trying to build a turn based battle system in which a player can take a specific number of actions per turn. Additionally a player can manipulate the times when these actions will be taken and at the end the actions will play out at the player-determined times.

I'm kind of at loss at how to approach this and would appreciate a hint at how to accomplish this. Right now I've tried queueing my actions / functions by storing them in a list, I've tried using delegates and queues but a problem I encounter is the use of different parameters for my functions, i.e. a Move function for a unit will take a Vector3 targetPosition but a SwapWeapon function for a unit will take a Int index parameter. Additionally right now I'm implementing my action functions by using Coroutines, so I can call them at various times and overlap each other but since these aren't working the way I want yet this is up for change as well. I imagine I could implement the timer by letting the player choose the starting times of his actions, then when it's time to execute just starting a timer and calling the function in my queue at the respective time but I haven't tried implementing this bit yet.

Thanks for any answer I might get.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by YoungDeveloper · Aug 11, 2015 at 12:04 PM

Check out c# delegates, you can add - register and remove - unregister any methods of same param type. When you call the event all registered methods will be called, you can even loop and do picky stuff. As param will be any possible type really, create a container which will hold a dictionary that way you will be able to pass any data you want without creating override type of events.

Comment
Add comment · Show 5 · Share
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
avatar image Hsmblada · Aug 11, 2015 at 01:37 PM 0
Share

Thank you for your answer. Could you please give an example of how to use any parameter by using dictionaries? I don't quite understand how I'd go about doing that.

avatar image YoungDeveloper · Aug 11, 2015 at 02:48 PM 1
Share
 //pseudo
 public class DataObject{
     private Dictionary<string, System.Object> _data = new ...
 
     public void add(string key, System.Object value){
         //add ..
     }
 
     public System.Object get(string key){
         //get ..
     }
 }
 
 //to add
 int a = 5;
 string b = "something";
 
 dataObject.add("myNumber", a);
 dataObject.add("myString", b);
 
 //to get
 int num = (int)dataObject.get("myNumber");
 string text = (string)dataObject.get("myString");

of course this will work with any data types or objects

avatar image Hsmblada · Aug 11, 2015 at 03:56 PM 0
Share

Thanks, I tried to implement your suggestion but encountered a few problems. $$anonymous$$y code looks something like this:

 public class DataObject {
         // like in your example above
     }
     
     ...
     
     public delegate void Action(DataObject param);
     private Action actionQueue;
     
     ...
     
     // here I want to call a move function of an object of custom type Unit which normally takes 
      // Vector3 as a parameter (I use DataObject.get in Unit.RotateAnd$$anonymous$$ove to get the Vector3)
     DataObject param = new DataObject();
     param.add("targetPosition", mousePosition);
     actionQueue += selectedUnit.RotateAnd$$anonymous$$ove(param);
     
     // then if I want to invoke all stored method calls I do something like this
     if(actionQueue != null) {
        actionQueue();
     }

With this code I get the following error message: "Operator +=' cannot be applied to operands of type TurnController.Action' and `void'"

avatar image YoungDeveloper · Aug 11, 2015 at 04:57 PM 1
Share

It doesnt work like that, you register only the methods and then call the Action() passing the value.

 public delegate void Action(DataObject param);
 private Action actionQueue;
 
 private void Test(DataObject data){
     //unpack you data here
 }
 
 actionQueue += Test;
 
 
 
 DataObject param = new DataObject();
 param.add("targetPosition", mousePosition);
 
 actionQueue(param);
avatar image Hsmblada · Aug 11, 2015 at 10:52 PM 0
Share

Oh I see, that makes sense. But then this isn't the kind of flexibility I had hoped to achieve, is there no way to store the parameters of a function call WITH the function call? Because I wanted to collect all the calls including their parameters so I could later on easily rearrange and call them in the order I want with a simple loop. This way this seems really difficult; maybe I could store the DataObjects in a separate list but then it would be hard to associate the parameters with their respective function calls after rearranging the order of the calls.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Frame Drop on a function dealing with an updated List type (Only once after new element Added) 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# - Is it possible to sync a Queue to a List, or is there a better option? 1 Answer

A node in a childnode? 1 Answer


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