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

Noob question about methods

Discussion in 'Scripting' started by Thomas12, Mar 30, 2015.

  1. Thomas12

    Thomas12

    Joined:
    Dec 24, 2014
    Posts:
    36
    Hello everyone,

    I now get that if I create a file such as test.cs and the file contains the simple code

    public class test {
    // some stuff
    }

    I will be able to do

    instance_of_test = new test();

    from any other script in Unity. Very handy. But what about methods I want to be accessible from everywhere as well?

    Say I have two methods

    void GetDoor(Vector3 inputPosition) {
    // some stuff
    }

    void GetDoor2(Vector3 inputPosition) {
    // some stuff
    }

    and I want to be able to call them from everywhere in Unity and store them in two separate files (this is important, I don't want to end up with a very large file containing all of my static methods). What should I do? I can make each of them static in a different public class, but then to call them I have to do

    class1.GetDoor(new Vector3(1,1,1))
    class2.GetDoor2(new Vector3(1,1,1))

    ...it seems overkill, I don't need classes, I just want to be able to call GetDoor and GetDoor2 directly. Any suggestion?
     
  2. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    I don't think you can do that currently (correct me if I'm wrong, someone). I do know that in C# 6.0 I saw that there'll be the ability to add a using directive on static classes, then you'll be able to call the static methods without putting the class name in front.
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    If you make the class partial then you can put the source for that class in multiple files. The better question is why you would want to do that in the first place.
     
    BenZed likes this.
  4. Thomas12

    Thomas12

    Joined:
    Dec 24, 2014
    Posts:
    36
    I just realised that I can use a class as a method thanks to the constructor of the class. All I need is to use the constructor for managing the inputs, to perform on these inputs what I need to perform and to store the results as other variables within the class that I can then access. Maybe this is not very efficient?
     
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Arguments passed to a constructor should be used for the initialization of that object. While a constructor is a type of method (sort of) it's not "using a class as a method".

    What are you trying to do?
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You could use a base class and extend it all over the show.

    But why? At some point you need to stop fighting the way the language is designed.
     
  7. Thomas12

    Thomas12

    Joined:
    Dec 24, 2014
    Posts:
    36
    I just find that my project is better organized if I have one file for each function.

    I just successfully transformed two methods into constructors, it does not seem to lead to any problem so far.
     
  8. ensiferum888

    ensiferum888

    Joined:
    May 11, 2013
    Posts:
    317
    Why would you want one file per function?? I can't even begin to estimate the kind of issues you'll run into down the road with that kind of approach.

    Plus it sort of implies that every single class you'll create will be immutable. Can you explain what kind of gain you're hoping to get by doing that?
     
    Kiwasi likes this.
  9. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Er - yeah - that's a terrible, terrible idea.

    First and foremost - you're now allocating memory to initialize a new object instance every time you want to "run a method". Assuming you're not using it for anything else it then needs to be garbage collected. Build anything of any measurable scale like that and you'll collapse under the weight of your GC.

    Secondly - it would make your calling code incredibly difficult to read.

    Thirdly - that approach just really doesn't make any damn sense :)
     
    Kiwasi likes this.
  10. Thomas12

    Thomas12

    Joined:
    Dec 24, 2014
    Posts:
    36
    All right then, I take note that the best solution here remains to have a static class in which I put all of my (static) methods.

    Doesn't it bother you, when you have, say, 5 functions each with more than 50 lines, that you always have to scroll down to be able to get a look at the function of interest? How do you manage a large number of functions?
     
  11. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    In visual studio you can collapse functions or use regions to manage them. Also, the Single Responsibility Principle states that a class should only have a single responsibility, which reduces the amount of functions it will have in general.
     
  12. evan140

    evan140

    Joined:
    Jul 15, 2014
    Posts:
    72
    I'm not an expert at C# or anything but if it makes sense to have 5 then it just makes sense. I mean 5 isn't really THAT much and we are talking about gaming here. Like this shouldn't raise any alarms and these functions make sense... generally...

    "class Sheep"
    1. Wander()
    2. Eat()
    3. Drink()
    4. Bah()
    5. SummonDemonsForWar()
      (Hey, don't judge my evil demonic war sheep!)
     
  13. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You're worried about a file that has 250 lines in it? I've seen methods longer than that (not that this is a good thing).

    IDEs have a bunch of ways to make that easier for you. One of them is scrollbars :) Cheekiness aside - Visual Studio lets you search for methods and has a dropdown box at the top of every file that lists all the methods in that file. There's also the class view. Not to mention the other proposed solutions of collapsing the methods or putting in regions which are all good too.