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 /
avatar image
0
Question by NikEy · Aug 05, 2013 at 04:14 AM · dynamicparametersdelegatearguments

Dynamic Parameters/Arguments

Hi guys,

In my current project I'd like to pass on parameters in a dynamic fashion. I don't want to use overloads since this makes the code extremely ugly and long for something that should really be very compact. I believe in C# 4.0 one can use the "dynamic" keyword to achieve this, but I am not sure how to make this happen in Unity's C# 3.5.

In my specific case, I'd like to call a sync function which then executes a bunch of RPC calls depending on the dynamic parameter. This parameter could take a variety of types/values, e.g. string, float, Vector2, Vector3, etc.

Example:

 private void Sync(string function, int id, int type, dynamic val) {
     if (val is float) ......
 }

As mentioned, "dynamic" is unfortunately not possible in Unity at this point. What would be another, yet efficient way to achieve this?

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
0
Best Answer

Answer by Adamcbrz · Aug 05, 2013 at 04:33 AM

I am assuming that the script calling the method will now the type. If so something like...

 void Swap<T>(ref T lhs, ref T rhs)
 {
     T temp;
     temp = lhs;
     lhs = rhs;
     rhs = temp;
 }

Maybe that will work for your situation

http://msdn.microsoft.com/en-us/library/twcad0zb.aspx

Comment
Add comment · Show 2 · 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 NikEy · Aug 05, 2013 at 05:18 AM 0
Share

argh, this seems like it could work. Unfortunately my Sync function is using a delegate with yield statement, resulting in this error:

Parameter val' cannot be used inside anonymous method' when using ref' or out' modifier

avatar image NikEy · Aug 05, 2013 at 05:49 AM 0
Share

managed to fix this in the end with lots of convoluted copies and statements like this:

 if (val.GetType() == typeof(Vector3)) {
     Vector3 parameter = (Vector3) (object) val;
 }

thanks

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

15 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

Related Questions

Sequence of methods stored in the list with parameters 1 Answer

Not able To send integer as a parameter on onclick? 1 Answer

Parameters at Startup 1 Answer

Is the documentation for surface shader custom lighting functions wrong? 1 Answer

boolean as function parameter/argument 2 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