Search Unity

Programming 101 Question

Discussion in 'Scripting' started by Maker16, Jul 15, 2009.

  1. Maker16

    Maker16

    Joined:
    Mar 4, 2009
    Posts:
    779
    Ok. I'm working on a resource system. Every resource type shares the same variables. They also all have a function to handle consumption of the resource per cycle. Let's call it Consume(). However, the computation for the consumption (the body of Consume()) is different for each type of resource.

    Now, my question is, what's the best approach to coding that? My only thought is to create a resource class with the variables and a virtual Consume() function, and then have each resource type inherit from that class, redefining Consume() for each. That way, I can iterate through an array of all resources and each calls its own Consume() function. Is that the best approach?
     
  2. koko93s

    koko93s

    Joined:
    May 14, 2009
    Posts:
    43
    I'm just a novice myself, so this may not be the best way. Why not use a switch statement inside a single Consume() function that checks the resource type and executes based on that?

    for example:

    Code (csharp):
    1.  
    2.  
    3. // pass whatever you are using to identify resource, string, whatever
    4.  
    5. function Consume(resource : String)
    6. {
    7.  switch (resource)
    8.  {
    9.    case "a":
    10.     //do computation for resource a
    11.     break;
    12.  
    13.    case "b":
    14.     //do computation for resource b
    15.     break;
    16.  
    17.  }
    18.  
    19. }
    20.  
    21.  
    Jason
     
  3. Maker16

    Maker16

    Joined:
    Mar 4, 2009
    Posts:
    779
    Yeah. I hadn't considered that. That would probably save me a lot of trouble.
     
  4. burnumd

    burnumd

    Joined:
    May 27, 2008
    Posts:
    367
    The OP's solution is much more flexible than using a switch, and is a more generally accepted OO solution to the problem. Something like
    Code (csharp):
    1. class Resource {
    2.     virtual function Consume() {
    3.         Debug.Log("Consume1"); 
    4.     }
    5. }
    6.  
    7. class Resource2 extends Resource {
    8.     function Consume() {
    9.         super.Consume();
    10.         Debug.Log("Consume2");
    11.     }
    12. }
    13.  
    14. function Awake () {
    15.         var resource2 : Resource = new Resource2();
    16.     resource2.Consume();
    17. }
    works perfectly well.

    If you're uncomfortable extending classes and using virtual functions (for which there aren't a lot of Unity Javascript / Unityscript-specific tutorials), Unity provides for you the SendMessage() and BroadcastMessage() methods. Using these methods is more expensive than using abstract classes or interfaces (the latter of which is not supported AFAIK in Unity's flavor of Javascript), but gives you even *more* flexibility, as the receiver of said message can ignore the request entirely if you use the SendMessageOptions.DontRequireReceiver parameter in send/broadcast message.
     
  5. Maker16

    Maker16

    Joined:
    Mar 4, 2009
    Posts:
    779
    Why, yes it does. Ok, then. I'll stick with the original design. Just didn't know if it was the best approach.