Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 js__ · Mar 21, 2016 at 11:32 AM · c#networking

Networking Command class argument

I'm trying to implement some kind of Flux architecture in Unity. Now I want to use it in multiplayer game. The idea is to send action to the server and then the server will distribute it to the clients.

I wanted to use Command in order to send the Action to the server:

 [Command]
 public void CmdDispatch(Action action) {
   // handle action
 }

However when I run this code I got some kind of strange error.

InvalidProgramException: Invalid IL code in Unity.GeneratedNetworkCode:_ReadAction_None (UnityEngine.Networking.NetworkReader): IL_0000: newobj 0x06000001

After some investigation, I find out that the problem is the Action argument. The Action class looks like this

 public class Action
 {
     private string type;
     private Dictionary<string, object> data;
 
     // constructor and some methods
 }

I think I need to serialize the Action object somehow. So the question is how to do that and if Command is a good approach for this or I should rather use something else.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by js__ · Mar 22, 2016 at 07:06 PM

I managed to resolve this problem, so I think it is a good idea to put here solution in case somebody with similar problem found this.

First of all I serialized the Action object itself to byte array. I found this answer on Stack Overflow useful.

However, serialized Action object was too large due to the Dictionary. So I created structure where I converted Dictionary to 2 arrays.

 [Serializable]
 public struct ActionStruct
 {
     public string type;
     public string[] keys;
     public object[] values;
 }

This was about 10 times smaller than the original action which was fine to send as an Command argument.

Comment
Add comment · 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
1

Answer by softrare · Mar 21, 2016 at 12:00 PM

A command cannot be executed with any arbitrary object as parameter.

Read here: http://docs.unity3d.com/Manual/UNetActions.html

"Arguments to Remote Actions

The arguments passed to commands and ClientRpc calls are serialized and sent over the network. These arguments can be:

 basic types (byte, int, float, string, UInt64, etc)
 arrays of basic types
 structs containing allowable types
 built-in unity math types (Vector3, Quaternion, etc)
 NetworkIdentity
 NetworkInstanceId
 NetworkHash128
 GameObject with a NetworkIdentity component attached"

EDIT: Oh didn't read the end of your question. Ok, to do what you want you will have to send data over to the server in one of the above datatypes and create the Action object on the "other side" (on the server).

Comment
Add comment · 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

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

141 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

Related Questions

How do you correctly remove a player that has disconnected from a game when using "OnServerDisconnect" ? 0 Answers

MultipartFormDataSection not working - Error: 403 0 Answers

ClientRpc not functioning 0 Answers

Unity Networking 0 Answers

Check what group id you have after being instantiated. (PUN) 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