Search Unity

[RELEASED] Code Control - Easy MVC for Unity

Discussion in 'Assets and Asset Store' started by Jorisvanleeuwen, Feb 22, 2015.

  1. softlion

    softlion

    Joined:
    Sep 6, 2013
    Posts:
    100
    Thank you for clarifying. Loading a single model that has been saved in a group is not supported in Code Control as it potentially has references to the other models. If you want to change something without too much manual labor you would have to load all models that were saved in that group, make the change, and save them back to ModelBlobs.

    You could query the ModelEntry from the ModelBlobs as you are doing in your example, manually go through the XML (using the XmlDocument class), change values of properties and serialize and save it back into the ModelBlob. Note that this has not been tested as Code Control is not designed for this use.

    Thank you again for clarifying, I hope you will get to a solution.
     
  2. cavila

    cavila

    Joined:
    Jun 4, 2013
    Posts:
    67
    Hi Softlion,

    I'm starting to get acquainted with Code Control. The first thing I noticed is the lack of encapsulation of the framework in a namespace. Is this something that's planned for the future?

    thanks.
     
  3. softlion

    softlion

    Joined:
    Sep 6, 2013
    Posts:
    100
    Hi cavila!
    This is planned to be implemented in the next update!
     
    cavila likes this.
  4. vzheng

    vzheng

    Joined:
    Dec 4, 2012
    Posts:
    45

    UnauthorizedAccessException: Access to the path "SomePath" is denied.


    iOS 9.2,Xcode 7.2,
     
  5. softlion

    softlion

    Joined:
    Sep 6, 2013
    Posts:
    100
  6. softlion

    softlion

    Joined:
    Sep 6, 2013
    Posts:
    100
    Update: Code Control patch 1.0.3 was just accepted and is now live on the Asset Store.

    Patch notes:
    - Put all Code Control components in a CodeControl namespace
    - Fixed monitor title warning message in Unity 5.1.2 and up
    - Fixed bug "Element already exists in dictionary" on model register
     
  7. cavila

    cavila

    Joined:
    Jun 4, 2013
    Posts:
    67
    Saw the new namespacing, awesome!
     
  8. gravyleaves

    gravyleaves

    Joined:
    Mar 19, 2014
    Posts:
    28
    Is it possible to have multiple controllers of the same type that use separate models?
    In a local multiplayer game I want each player to have its own controller and model of a basic type. Message flow monitor doesn't seem to allow display copies of one controller.

    Also when I broadcast a global message, only one controller picks it up, not both. How can I get around this?
     
  9. softlion

    softlion

    Joined:
    Sep 6, 2013
    Posts:
    100
    Hi gravyleaves!
    Yes this is possible! Take a look at the example project; the TurretController is being instantiated multiple times, each having its own TurretModel. To implement local multiplayer in your game you could store the (xbox)controller index in the model of your controller.
    Code (CSharp):
    1. class PlayerModel : Model {
    2.     public int PlayerIndex;
    3. }
    4.  
    5. class PlayerController : Controller<PlayerModel> {
    6.     private void Update() {
    7.         // Use model.PlayerIndex to target correct (xbox)controller
    8.     }
    9. }
    10.  
    11. class GameManager : MonoBehaviour {
    12.     private void Awake() {
    13.         for (int i = 0; i < 4; i++){
    14.             PlayerModel model = new PlayerModel();
    15.             model.PlayerIndex = i;
    16.             Controller.Instantiate<PlayerController>("PlayerPrefab", model);
    17.         }
    18.     }
    19. }
    If you start listening to the message in the controller's OnInitialize method, all instances of that controller should be picking up the message. Can you provide some of the code so I can take a look?
     
  10. gravyleaves

    gravyleaves

    Joined:
    Mar 19, 2014
    Posts:
    28
    That was part of it. The other part was one of the controllers intercepting the other's messages. Thanks for the help!
     
  11. softlion

    softlion

    Joined:
    Sep 6, 2013
    Posts:
    100
    I'm not sure if I understand.. can you clarify?
     
  12. gravyleaves

    gravyleaves

    Joined:
    Mar 19, 2014
    Posts:
    28
    Probably not well... My setup was complicated where one controller was intercepting messages after initialization of a separate identical controller before the global initialization message reached the controller which did the intercepting.

    If somehow I could get a reference to controllers when instantiating them I could have an easier work around, but it's fixed now in any case.
     
  13. softlion

    softlion

    Joined:
    Sep 6, 2013
    Posts:
    100
    Yes indeed, the idea is to decouple controllers and communicate through messages. Are your problems now fixed? :D
     
  14. marshmatter

    marshmatter

    Joined:
    Oct 2, 2013
    Posts:
    46
    How is platform support for Code Control?
     
  15. softlion

    softlion

    Joined:
    Sep 6, 2013
    Posts:
    100
    Hi marshmatter!

    PC and Android are tested most. iOS is also tested and performed fine. WebGL is not tested as it was still in beta during Code Control's release. Game consoles are also not tested.

    If it works or does not work for a specific platform, please let us know on this thread! Thank you!
     
  16. Extrakun

    Extrakun

    Joined:
    Apr 2, 2009
    Posts:
    69
    Hi two quick questions:

    1. Can logic to be implemented in the model instead of the controller?

    2. Will references to normal objects (not models) inside a model be serialized properly, and be serialized to a reference?
     
  17. softlion

    softlion

    Joined:
    Sep 6, 2013
    Posts:
    100
    Hi there!

    You could add some methods/properties of simple queries or data manipulation in the models. But all game logic should reside in the controllers.

    Those "normal" objects should have the [serializable] attribute assigned to their class, then they would be serialized properly. Only the Model classes referenced through the ModelRef(s)<T> will be (de)serialized as a separate reference though!
     
  18. Extrakun

    Extrakun

    Joined:
    Apr 2, 2009
    Posts:
    69
    Just curious -- Usually most MVC advocates would advise putting logic into the model as to avoid 'fat' controllers.
     
  19. themarkval

    themarkval

    Joined:
    Feb 5, 2013
    Posts:
    11
  20. LesmoCasanova

    LesmoCasanova

    Joined:
    Mar 22, 2015
    Posts:
    1
  21. softlion

    softlion

    Joined:
    Sep 6, 2013
    Posts:
    100
    Unity Code Control will not support UWP anywhere in near future. Feel free to solve the issues of unsupported platforms in the source code which is included in the Unity Code Control asset package!
     
  22. hkha

    hkha

    Joined:
    Jul 21, 2012
    Posts:
    6
    Can I use ModelBlobs to save binary data like images ?
    and is there a limit ?
     
  23. softlion

    softlion

    Joined:
    Sep 6, 2013
    Posts:
    100
    Model(Blobs) can only store serializable data, that means no images! If you want to store images, you should write that functionality yourself in the associated controllers.
     
  24. GamePowerNetwork

    GamePowerNetwork

    Joined:
    Sep 23, 2012
    Posts:
    257
    Hi @softlion

    First I want to say, thanks so much for this asset. It has really come in handy for my project so far.

    My question is related to reusing models for multiple controllers. I know it is possible.. but is it a bad practice to have a single model used on 2 different controllers?
     
  25. softlion

    softlion

    Joined:
    Sep 6, 2013
    Posts:
    100
    Hi PyroStudios!

    This is up to you, as you need to manage the instantiation and initialization process of the Controllers yourself.

    I hope this answers your question.
     
    Last edited: Aug 4, 2017
  26. MaddoScientisto

    MaddoScientisto

    Joined:
    Jan 30, 2015
    Posts:
    62
    Hey, I've been using this for years because it was so useful but now the website appears to be offline so the tutorials are gone, any plan to bring them back up?
     
  27. softlion

    softlion

    Joined:
    Sep 6, 2013
    Posts:
    100
    Hi MaddoScientisto, cool to hear you've been using Code Control!

    When I check the site it seems to be running fine! Might be that the host took it off for a few hours to perform an update. Let me know if you can reach it now.
     
  28. MaddoScientisto

    MaddoScientisto

    Joined:
    Jan 30, 2015
    Posts:
    62
    Yeah it works now, guess I was just unlucky
     
  29. Spyderworxs

    Spyderworxs

    Joined:
    Aug 21, 2012
    Posts:
    31
    Could you have a look at the product site please? One of the Tutorials: Making a Controller is not working well for me. It tells me

    Error establishing a database connection

    The other two tutorials work well.
     
  30. softlion

    softlion

    Joined:
    Sep 6, 2013
    Posts:
    100
    Hi Spyderworxs! Thanks for the heads up.

    Apparently the web provider has been taken over by another company and something went wrong with the transfer. I've contacted them for support, and am now waiting their reply.

    For the mean time I've uploaded a .txt version of the site. Hope that helps.
    http://unitycodecontrol.com/other/Code Control Website.zip
     
  31. Spyderworxs

    Spyderworxs

    Joined:
    Aug 21, 2012
    Posts:
    31
    Thanks for the text website. That will help, much appreciated. Thanks for the great framework. I have used CodeControl often since you released it and it is great!
     
  32. softlion

    softlion

    Joined:
    Sep 6, 2013
    Posts:
    100
    That's nice to hear!
     
  33. FBones

    FBones

    Joined:
    Aug 28, 2016
    Posts:
    73
    Hi @softlion,

    I have a question regarding the sample game included in CodeControl. I think my question gets at the deeper subject of whether MVC is strictly a design model for the presentation layer of an app or a general architectural approach.

    My specific question is "Where do the Mode classes in the Behaviors folder fit into the MVC pattern?"

    Should I think of these as residing in a separate, business logic layer of the app, or as being part of the MVC paradigm?
     
  34. softlion

    softlion

    Joined:
    Sep 6, 2013
    Posts:
    100
    Hi FBones!

    Oof that has been quite a while writing the example project of Code Control. I've been using a bunch of different MVC based frameworks since then, and I've come to realise there are many interpetations of the architectural approach.

    I agree with you that the Mode classes of the Code Control example should be considered as the logic layer of the app, not MVC, and can thus be implemented in any way the coder sees fit for their game.

    I hope this answers your question!
     
  35. FBones

    FBones

    Joined:
    Aug 28, 2016
    Posts:
    73
    @softlion , Thanks for the note. I have run into problems using the messaging monitor system.

    I have a small project that uses messages, and it works fine when I run it normally. But when I open the Code Control Monitoring window I get a variety of unexpected behavior:

    1. Most immediately, when I run the game, none of my OnPointerEnter() events work if the Code Control Monitoring window is open. This is true even if the Code Monitoring Window is docked into the editor.

    2. If I maximize the game window mid-play, my OnPointerEnter() events do fire and the game works as expected (but of course I cannot see the window at that point).

    3. If (again mid-play) I then un-Maximize the game window, my OnPointerEnter() events continue to work!! ....but none of my messages show up in the Message monitor.

    I'm attaching a screenshot. I have Console readouts showing messages being sent and received, but nothing shows up in the monitor.

    upload_2018-12-26_16-4-27.png
     
  36. FBones

    FBones

    Joined:
    Aug 28, 2016
    Posts:
    73
    @softlion , just a note that I'm no longer getting the behavior mentioned above.

    Also, I need to buy another seat so that a collaborator can work on this project with me. This guidance appears out of date. Any ideas?
     
  37. softlion

    softlion

    Joined:
    Sep 6, 2013
    Posts:
    100
    Hey FBones,

    I'm afraid I don't know the answer to that question. I think its best to contact Unity Support.
     
  38. DavidLe360

    DavidLe360

    Joined:
    Dec 24, 2018
    Posts:
    127
    Hi, i have this error:

    Code (CSharp):
    1. [Error] Instantiating a non-readable 'Logo' texture is not allowed! Please mark the texture readable in the inspector or don't instantiate it.
    2. GameManager.Awake()    Assets/CodeControl/Example/Scripts/GameManager.cs:15
    3. 13:   private void Awake() {
    4. 14:       GameObject hud = GameObject.Instantiate(Resources.Load("GameHUD"), Vector3.zero, Quaternion.identity) as GameObject;
    5. -->15:       logo = GameObject.Instantiate(Resources.Load("Logo"), Vector3.zero, Quaternion.identity) as GameObject;
    6. 17:       MeshButton[] buttons = hud.GetComponentsInChildren<MeshButton>();
    7.  
    Unity 2019.3.12f1

    I suspect there is a kind of conflict with other asset, because when i use CodeControl in a new project, there is no error.

    Btw, 'public static class MathHelper' should have a namespace to avoid conflict.

    Thanks in advance.
     
    Last edited: May 2, 2020
  39. DavidLe360

    DavidLe360

    Joined:
    Dec 24, 2018
    Posts:
    127
  40. RealityDotStop

    RealityDotStop

    Joined:
    Mar 4, 2013
    Posts:
    42
    DavidLe360 likes this.
  41. DavidLe360

    DavidLe360

    Joined:
    Dec 24, 2018
    Posts:
    127
    Wow, an other awesome useful tool! The tutorial was easy to understand, and can't wait to implement this system in my future project.

    The visual graph is very useful for debugging, etc. It would be nice if we have an option, when right click on an actor, to show (ping) the GameObject(s) that using it.

    Hope this asset is not dead. I have tried to find some thing else using MVC with an visual graph (Code Control Monitors), but didn't found any other.

    Anyway, thanks a lots for creating this asset, especially for the Code Control Monitors :)
     
  42. RealityDotStop

    RealityDotStop

    Joined:
    Mar 4, 2013
    Posts:
    42
    If softlion doesn't mind, I have a slightly updated version (Serializes via Json.Net, lets you form ModelBundles, and even inject them internally (or save/restore to disk, of course). It isn't ready yet (namely the integration with the UI remains), that I don't mind maintaining. I like what Code Control does, and have been privately upgrading it. But ultimately, it is based on his code, so it isn't my say.
     
    DavidLe360 likes this.
  43. DavidLe360

    DavidLe360

    Joined:
    Dec 24, 2018
    Posts:
    127
    thx in advanced Hope softlion accept it. :)
     
  44. RealityDotStop

    RealityDotStop

    Joined:
    Mar 4, 2013
    Posts:
    42
    Unfortunately, he hasn't been seen since Feb 2019, so I'm not sure how to proceed. He's made the asset free, but that doesn't necessarily mean he's okay with me releasing modified versions of it. Once I get a bit further and have something to show I'll see if I can hunt him down and get his blessing.
     
  45. RealityDotStop

    RealityDotStop

    Joined:
    Mar 4, 2013
    Posts:
    42
    DavidLe360 likes this.
  46. RealityDotStop

    RealityDotStop

    Joined:
    Mar 4, 2013
    Posts:
    42
    I'm only working on this on the side, so progress is slow, but I figured I'd post an update.

    • I fixed an issue where subscribing to a message during a Message execution caused an exception. Additional Message subscription/releases are now automatically processed once the current Message execution is complete.
    • Added IDisposable Edit() methods, making it easier to work with models without forgetting to issue a NotifyChanges, like the following:
      Code (CSharp):
      1. using (model.Edit())
      2. {
      3.         model.awesomeprop = thething;
      4.        
      5.         if (model.somecondition)
      6.             return;
      7.  
      8.         model.ready = true;      
      9. }
      10.  
      11. //Controllers get updated regardless of exit point!
      12.  
    • Working on support for Unity async support in saving/loading. This is more intuitive than the existing event subscription + callback coroutine system.
    • I'm working on improving support for polymorphic controllers/models. Following a load in the existing system, the user restoring code needs to know 100% of the controllers to be able to successfully restore. This works for small game systems where it is relatively easy to update for even a dozen model types, but becomes unmanageable when the game author needs to go into their level controller and tell it how to handle the thousandth model type.

      To this end, I added an attribute to tag your controller with, indicating what model it takes ownership of, and the Addressable/Resource it should use when loaded. This is currently an opt-in call (Controller.InstantiateAutomatically), and I'm not entirely sold that this is the most elegant way to handle the problem, but I do believe it is a step in the right direction toward allowing Code Control to handle larger game systems.
     
    DavidLe360 likes this.
  47. RealityDotStop

    RealityDotStop

    Joined:
    Mar 4, 2013
    Posts:
    42
    Over the past several months I've made some further modifications, all aimed at making CodeControl more flexible and handle what I was trying to throw at it.

    However, after a long think through about my goals and architecture, I think any further modifications to meet my specific needs would take Code Control further from its strengths and into trying to handle an architecture it was never designed for. MVC inherently tends toward the "God Controller" and restricts a composition style of development, and I have battered at ways around this fundamental obstacle, but any solution I have come up with takes Code Control away from what I consider to be its strength: the simplicity of a 1-1 relationship between a model and its controller and the centralized (and serialized) data model.

    My particular project, which you can think of as simulation builder, consists of hundreds of objects that can be placed in the world, and dynamic reactions to that. I found a way around the need for a controller needing to know the specifics of how to instantiate child controllers, but as I worked I found myself increasingly trying to work compositionally on my GameObjects and Prefabs, and as I added components, I kept having to make weird concessions. Instead of concrete models, I was increasingly having to add entire systems in to handle automated ways of injecting serializable data into the models, and controllers doling out the associated data, and gobs and gobs of boilerplate code. In the end, even the line between controllers and models was getting blurred, because I was trying to avoid God Controllers, but that meant either models handling more than just data, or controller responsibility being shared to other components. In the end, I kept being unsatisfied with the direction I'd have to go to make the framework work, and reconsidering. I just don't think the approach is the right fit for my project.

    My current project just isn't the best fit for Code Control. Is this a fundamental problem with Code Control? No, I took a framework I liked and had used on small projects and tried to shoehorn my new project into it without thinking through the repercussions and whether it truly was the best fit.

    I think with a little work, I can save the good parts of my work and get rid of the bad, if you're still interested. But I'm afraid internally I've already swapped to a framework that works well with compositional construction.



    If you're curious what framework I switched to, I hoped over to a framework written by a acquaintance of mine: (https://github.com/lazlo-bonin/gocs), and am porting what I've learned from CodeControl to handle references, and using the existing Json.Net serialization I got from it.
     
    DavidLe360 likes this.