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

Abstract variables ?

Discussion in 'Scripting' started by ThatOneGuy963, Jul 22, 2016.

  1. ThatOneGuy963

    ThatOneGuy963

    Joined:
    Jun 28, 2016
    Posts:
    17
    Now i know this might not be the right place to post this question, because it's more of a C# question than a unity question, but i can't phrase it except in game terms
    So i have an abstract class called Item, question is can i define the Type of a member variable from a child class, kinda like you define abstract methods' functionality or whatever ?
    to be specific i want some items to have either a Sprite icon or a list of Sprites as an icon, like an animation. i'm still pretty much a beginner so sorry if the question is dump or need a little research but as i said i couldn't phrase it in C# terms
    all i can think of is making both Sprite singleSprite and Sprite[] spriteList and checking in the display method if(spriteList.Count == 0)use singleSprite; otherwise use spriteList animation; or making the class generic and have either Sprite or Sprite[]
    both seem dumb and weak and "undynamic" to me, so what could i do ?
    Thanks in advance
     
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    Don't be shy to post ^^ generally speaking, you'll know for sure when something should not be posted in these forums. (basically stuff not related to game development in Unity whatsoever)

    But to address your question, Is there any reason you can't always use an array and just have it be size 1 when you only need one sprite?
     
  3. johnmcmahonart

    johnmcmahonart

    Joined:
    Jul 15, 2016
    Posts:
    19
    I'm kinda new as well so this may not be very helpful but you might want to look at the override keyword. Here is a good link on the differences between interfaces and abstract classes (can't tell from your description which might be better suited for your use case)

    http://www.codeproject.com/Articles/11155/Abstract-Class-versus-Interface
     
  4. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,032
    If there's potential for more than one sprite, you always want just the array. You'll write less code to actually use that too.
     
    Timelog likes this.
  5. ThatOneGuy963

    ThatOneGuy963

    Joined:
    Jun 28, 2016
    Posts:
    17
    Actually the sprite array idea is pretty obvious .. i don't know how could i not think of that, i'm going to use it.
    but just to extend my knowledge a little bit if you guys will...first if it was two different kinds of data types is there a way around it ? like if i for some reason needed either an int or a string is there a way to express that ? And also about interfaces... i never really understood those. when i learned abstract classes it was basically an idea i was looking for ...the first thing i thought of was like how all entities can move and have speed,then all enemies can attack and have damage,than maybe a zombie is an instance of an enemy.but with interfaces i really can't get it.. using abstract classes kinda economizes my code like i can type the movement functions in Entity and inheret it all the way to player and zombie. but what's the point of interfaces if i have to rewrite the function everytime ?is it's idea kinda like the idea of//comments, where i help other developers understand my code ?
    Thanks for your help anyway fellas,really helped !
     
  6. johnmcmahonart

    johnmcmahonart

    Joined:
    Jul 15, 2016
    Posts:
    19
    I use a custom interface in my game for several important things so I'l try and explain how I use it. An interface is just a contract that a class using that interface must implement (you only define the signature of the methods in the interface, you don't care about how the class implements those methods). I have a class in my game called Imap and any class that implements that interface must provide a method to get the data at a given point, the color at a given point and the size of the map. The implementations for data and color for each map are implemented differently because each map does different things. When I need to display those maps for visualization purposes I can pass to my static display class ANY object using the interface Imap, so the display class can display any map that implements Imap, it doesn't care how each map builds its color table as long as it does (my get color method returns a type of Color). The other nice thing about interfaces is a class can implement multiple interfaces, you can only subclass from one abstract class
     
    ThatOneGuy963 likes this.
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,181
    I consider this the best example of an interface in the context of Unity.

    Code (csharp):
    1. public interface IDamageReceiver {
    2.  
    3.     void GetDamaged(int damage, Vector3 damageSource, ...);
    4.  
    5. }
    Then you slap that onto everything in your game that needs to take damage, and all your scripts that needs to do damage to stuff looks for IDamageReceivers.

    It doesn't make much sense to handle being able to take damage through inheritance, because how things that take damage reacts to that is often very different. You also want to avoid inheritance, as you can only inherit from one class, and that'd really restrict how you can build your class hierarchy.

    This means that you can have stuff like:

    Code (csharp):
    1. public class Enemy : MonoBehaviour, IDamageReceiver { ... }
    2. public class Breakable : MonoBehaviour, IDamageReceiver { ... }
    3. public class NPC : MonoBehaviour, IDamageReceiver { ... }
    4. etc...
    And then if you want to make an explosion happen, damaging all stuff around it is easy:

    Code (csharp):
    1. void CreateExplosionAt(Vector3 position, float radius, int damage) {
    2.     Instantiate(ExplosionEffect, position, Quaternion.identity);
    3.  
    4.     var allHit = Physics.OverlapSphere(position, radius);
    5.  
    6.     foreach(Collider c in allHit) {
    7.         var damageReceiver = c.GetComponent<IDamageReceiver>();
    8.         if(damageReceiver != null) {
    9.             damageReceiver.GetDamaged(damage, position);
    10.         }
    11.     }
    12. }
    And now all your enemies and breakables and whatever that needs to take damage gets damaged.
     
    Ironmax and ThatOneGuy963 like this.
  8. ThatOneGuy963

    ThatOneGuy963

    Joined:
    Jun 28, 2016
    Posts:
    17
    This was actually great, kinda wierd that the word "Any" is what cleared it up for me
    But still i have to wonder, i can make a virtual method in an abstract class and change it if i will, is that a thing in interfaces too ? or do i have to repeat the same default code in every class that implements it ?
    Thanks for the feeback !
     
  9. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    Interface is great, also use it to check collision target or any other type of target or interaction

    Yes you need to include (The metod constructor/fields/properties) it in every thing you inherit the interface from. Even if you dont use it.
     
    ThatOneGuy963 likes this.