Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Optimization on script!!! Help!

Discussion in 'Scripting' started by d2, Aug 1, 2014.

  1. d2

    d2

    Joined:
    Apr 23, 2014
    Posts:
    8
    in my game the player can pick up diferent kind of items, i have somethig like this:

    void OnTriggerEnter(Collider other){
    switch(other.tag){
    case ("Bullet"):
    //Something
    break;
    case("Red"):
    //Something
    break;
    case("SpeedUp"):
    //Something
    break;
    case("SpeedDown"):
    //something
    break;
    case("Valid"):
    //something
    break;
    .
    .
    etc................
    }
    }
    }

    this is in my player script, is better this way or write a scrip for each item that send a message to the player when the item is picked up????
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    I would make a base class called ItemPickup. Then you can extend that class so that each object does something different when it is picked up.
    Code (csharp):
    1. public abstract class ItemPickup : MonoBehaviour
    2. {
    3.   public abstract void DoPickup();
    4. }
    Then your player can change that switch statement to this.
    Code (csharp):
    1. void OnTriggerEnter(Collider c) {
    2.   ItemPickup itemCheck = c.GetComponent<ItemPickup>();
    3.   if (itemCheck != null) itemCheck.DoPickup();
    4. }
    Alternatively, you can have ItemPickup check for the player then run DoPickup() itself if it detects it collided with the player.
     
    d2 likes this.
  3. hammil

    hammil

    Joined:
    Jun 5, 2013
    Posts:
    56
    Since you're asking if there's a better way, I hope you don't mind if I go on a small tangent

    Do you know about polymorphism in OO design? What I'd suggest you do is make a base Item class, like this:

    Code (CSharp):
    1. public class Item : MonoBehaviour
    2. {
    3.     public virtual void PickUp(Player player)
    4.     {
    5.         Debug.Log("Picked up " + name);
    6.     }
    7. }
    Then, you can make your specific item scripts derive from Item, and then override the PickUp method to have other effects. As an example:

    Code (CSharp):
    1. public class SpeedUp : Item
    2. {
    3.     public override void PickUp(Player player)
    4.     {
    5.         base.PickUp(player);
    6.         player.speed += 1f;
    7.     }
    8. }
    EDIT: Looks like I was beaten to it!
     
    d2 and GarthSmith like this.
  4. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    lol at least we can be reasonably sure this is a good way to handle this case!
     
    hammil likes this.
  5. d2

    d2

    Joined:
    Apr 23, 2014
    Posts:
    8
    thank very much you both!!, i'll will the use the inheritance and the override, sometimes when im making my game i forget about all the OO stuff....
     
  6. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Good choice. There's nothing inherently wrong with what you were doing... and using a switch statement is far better than multiple if/then statements because the CLR builds a lookup table for switches internally that makes them fast. However, going the OO route will result in much cleaner code.
     
  7. d2

    d2

    Joined:
    Apr 23, 2014
    Posts:
    8
    mmmm, but in performance what will be better? stay with my switch code or go for the OO design??
    the game is for mobile..
     
  8. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    6 of one half dozen of the other I think. I would go the cleaner OO route.