Search Unity

Classes? Custom functions?

Discussion in 'Scripting' started by Thordis, Mar 3, 2006.

  1. Thordis

    Thordis

    Joined:
    Mar 1, 2006
    Posts:
    18
    I'm new to Unity, but am very impressed with the art pipeline. I was able to create a little game in only two days, surprising myself. Coming from a background in C++ though I'm having some confusion with the Unity system of scripting. I'm wondering how to define a class or struct (or equivalent) with custom functions and data objects etc.?

    Thank you very much,
    Will
     
  2. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Welcome!

    In C# (which you will probably be more comfortable with coming from C++) you get an example of a class when you create a new C# script file. One thing to remeber about unity scripting, if your class uses any unity classes (I think this applies to all) your class must inherit from MonoBehaviour:
    Code (csharp):
    1.  
    2. public class MyClass : MonoBehaviour
    3. {
    4.  
    5. }
    6.  
    If your class doesn't use any unity-specific functions though, you can inherit from whatever you want, or nothing at all. You can also create a base script that inherits from monobehavior and any subclass of that new script will also have access to monobehaviour. I used this with around 5 seperate classes to create a state machine and steering behaviour AI. Your classes can also be seperate script files, and unity will automaticcaly use them if you reference it (call a function in it or subclass, etc) so you don't need to have every script on a gameobject. That is really cool. I will throw some simple code here for you in C# to look at, basic stuff but it may help.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class MyBaseClass : MonoBehaviour
    6. {
    7.  
    8.  public void CheckWorld ()
    9.  {
    10.   //blah blah....blah
    11.  }
    12. }
    13.  
    14. public class MyNewClass : MyBaseClass
    15. {
    16.  /*you can now access the CheckWorld() function in the other class and any Unity function in this class.  Note this class could also be in another file than the top one. */
    17. }
    18.  
    I hope that helps, also check out the script wiki:
    http://unify.bluegillweb.com/scriptwiki/index.php/Main_Page to see some examples.

    Good luck, and feel free to correct me guys if I missed something as I'm no C# expert yet. ;)

    Oh, BTW, AFAIK structs have the same format in C# as in C++. You will want to check to be sure though.

    -Jeremy
     
  3. Thordis

    Thordis

    Joined:
    Mar 1, 2006
    Posts:
    18
    Ahh, great!

    Thank you!
     
  4. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Glad it helped. One thing I forgot to mention which may be a common "gotcha" if you are used to OOP in C++ is that in C# there is no multiple class inheritance. A class can only inherit from one class. There are other ways of getting access to multiple classes though, you just can't sublass more than one. There are Interfaces in C# which are what they sound like, they are to set up your class Interfaces (saves your butt, if you forget to implement a required method, it will tell you), and AFAIK you can inherit from as many Interfaces as you like.

    A good tutorial on C# is here: http://www.softsteel.co.uk/tutorials/cSharp/cIndex.html

    Good luck,
    -Jeremy
     
  5. MikHaven_legacy

    MikHaven_legacy

    Joined:
    Jun 4, 2011
    Posts:
    1
    Wanted to clarify this works for files, which is what I needed. *Changing the base code to my real world Math Helper Clamper. Basically takes a value, the min, and the max, and locks it into those values. NICE for Health going from 0 - 100% in this example.

    Code (csharp):
    1.  
    2. [U]'Clamp.cs'[/U]
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class Clamp:  MonoBehaviour
    7. {      
    8.     public static int Clamp(int value, int min, int max)
    9.     {
    10.           return (value < min) ? min : (value > max) ? max : value;
    11.     }
    12. }
    13.  
    Code (csharp):
    1.  
    2. [U]'PlayerHealth.cs'[/U]
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class PlayerHealth :  Clamp
    7. {
    8.     public int MaxHealth = 100;
    9.     public int CurHealth = 100;
    10.     public float HealthBarLength;
    11.     public float HealthBarMaxLength;
    12.    
    13.     public void AdjustCurrentHealth(int Adj)
    14.     {
    15.         CurHealth += Adj;
    16.                     CurHealth = Clamp(CurHealth, 0, MaxHealth );
    17.     }
    18. }
    I gotta reply this is exactly what I was looking for. Its brillant. I was trying to come up with some base functions for my scripts. This made it a quick and dirty way to basically put together a 'DLL type' of object. That I can call from different scripts, all wrapped up in a base class that I have all the 'functions' stored in one place.

    I guess the basic functionality is there. Begs the question. Would you eventually want to go into a DLL for these types of base functions you need for your scripts. Helper functions if you will?
     
    Last edited: Jun 4, 2011
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Mathf.Clamp already exists; no need to reinvent it.

    --Eric