Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

C# Scripting Tutorials, Scripting without MonoBehaviour -- RPG Tutorial.

Discussion in 'Scripting' started by LPGaming-Company, Jan 24, 2013.

  1. LPGaming-Company

    LPGaming-Company

    Joined:
    Jan 17, 2013
    Posts:
    22
    People are always asking me when I ask for help on something how I am using my scripts without implementing MonoBehaviour, or don't understand why I don't just drag my scripts onto the game-object, that's because I like to use Instancing in my scripts, this is a good practice because if you ever move away from unity and decide to try to make something on your own you wont have a visual drag/drop editor there for you. This is explained further in the first video. This is a series and I will attempt to upload a Video/Day.


    Video #1
    http://www.youtube.com/watch?v=uKMnC33AF9s
    Video #2
    http://www.youtube.com/watch?v=2AgZrQvC3Ik
     
    Last edited: Jan 25, 2013
  2. Durston88

    Durston88

    Joined:
    Mar 15, 2012
    Posts:
    110
    Interesting, but mostly unnecessary if you're sure you're sticking with Unity? Gonna have to watch some more of these in the coming days and see where it goes.
     
  3. DexRobinson

    DexRobinson

    Joined:
    Jul 26, 2011
    Posts:
    594
    Yeah I like to keep all my player variables in a class by itself this way I don't have to use the dreaded DontDestoryOnLoad feature. I normally just make the class static though instead of writing a instance function. Is there anything different a static class vs a non static class with a get instance function? The one thing I do like about mono is being able to adjust numbers on the fly and while the game is playing.
     
  4. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    That's why I often like to use what I call a "pseudo-singleton" - a class that's attached to an GameObject for configuration purposes. For example:

    Code (csharp):
    1. public class PowerupManager : MonoBehavior
    2. {
    3.     // Parameters which can be tweaked in the editor
    4.     public float fireballRadius = 10.0f;
    5.     public float shieldTime = 30.0f;
    6.  
    7.  
    8.     public static PowerupManager Instance { get; protected set; }
    9.  
    10.     void Awake ()
    11.     {
    12.         Instance = this;
    13.     }  
    14.  
    15.     ...
    16. }
    Then you attach this to an object in the level, where you can easily adjust the public parameters. In code, you access it by the Instance property. You can also make the instance private and have the interface run through static functions, of which I have mixed feelings on. On the one hand, it's cleaner in most cases. On the other hand, you can't do things such as Invoke easily.
     
    Last edited: Jan 25, 2013
  5. LPGaming-Company

    LPGaming-Company

    Joined:
    Jan 17, 2013
    Posts:
    22
    I'm honestly not quite sure, I've never used a static class, I'm not sure if this is subject to C# only or if you can do it in java a swell, but throughout my 3 years in AP Comp Sci, I've never heard of it. I'll have to do some re-search and get back to you. Second video coming up today, perhaps a third as well depending on how bored I get.
     
  6. LPGaming-Company

    LPGaming-Company

    Joined:
    Jan 17, 2013
    Posts:
    22
    I'm not sure if I am going to stick with unity, I like it but it is more or less just a program to help me get started I would believe. I enjoy having it all handed to me like this in a way, but at the same time I would much rather have the satisfaction of knowing that I did it alone.
     
  7. Uninity

    Uninity

    Joined:
    Jul 31, 2011
    Posts:
    215
    I see you play League of Legends haha. That's the only game I play these days. What's your summoner level?
     
  8. LPGaming-Company

    LPGaming-Company

    Joined:
    Jan 17, 2013
    Posts:
    22
    Three accounts on 30, one on 22.... watching the euro tourny right now


    -=====================================-

    Second video is up.
     
  9. Uninity

    Uninity

    Joined:
    Jul 31, 2011
    Posts:
    215
    You should of explained what Static meant, because nor do I or other viewers may not know what it means.
     
  10. LPGaming-Company

    LPGaming-Company

    Joined:
    Jan 17, 2013
    Posts:
    22
    Ill add an annotation, sorry about that.
     
  11. DexRobinson

    DexRobinson

    Joined:
    Jul 26, 2011
    Posts:
    594
    @Errorsatz - Yeah I use that as well. I like doing that but if I need to bring certain variables from scene to scene I normally just pack it into a static class so I don't have a have a game object that never gets destroyed and I can debug from anywhere vs having to run through the menus each time.

    As for the meaning of static here is what I found on the msdn,

    Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class. Static class members can be used to separate data and behavior that is independent of any object identity: the data and functions do not change regardless of what happens to the object. Static classes can be used when there is no data or behavior in the class that depends on object identity.


    http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx
     
  12. LPGaming-Company

    LPGaming-Company

    Joined:
    Jan 17, 2013
    Posts:
    22

    Thanks for pulling that, as I didn't really know how to describe it although I did know how it worked.
     
  13. LPGaming-Company

    LPGaming-Company

    Joined:
    Jan 17, 2013
    Posts:
    22
    Third video will consist of playersMana and drawing it to the screen.
     
  14. laakerules

    laakerules

    Joined:
    Aug 29, 2012
    Posts:
    153
    Honestly, ive been working with computers and coding for 6 or so years now, passionatly for the past 4. And unities monobehavior is just a tool used to help the coder in any way possible with THREADS, since unity doesnt allow threads the way i use monobehavior is for the THREAD factor since THREADS are what make a game.

    But good tuts, you should go over abstract classes and interfaces to really get people into the basics of scripting without using monobehavior.
     
  15. LPGaming-Company

    LPGaming-Company

    Joined:
    Jan 17, 2013
    Posts:
    22
    Threading is more or less on the verge of mutliplayer and server communications, not really required in single-player instances.

    I'm going over how to create a simple game with people while not using Monobehaviour, I've only been using C# for around a week and a half now. I'm not claiming to be a guru, although abstract classes are not subjective to C#, going over them would just be extra knowledge. Not needed knowledge for what they are currently doing. I'll be creating video's explaining more in depth about scripting, although I want to make sure I have everything correct before I start informing people.

    Thanks for the reply, and to the other followers, sorry for not uploading a video today. I'll have a few more up tomorrow.. I've been busy.