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

public void MyFunction (Q how many arguments can i have here?)

Discussion in 'Scripting' started by Genkidevelopment, Feb 14, 2015.

  1. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Ok, so I am finally making some headway with the idea of abstraction and breaking my code down into smaller and smaller functions. Now, on each object I have a separate manager script which calls the action of a function written in a separate script, I am feeding in the argument data relevant to that particular object, and normally this is two, three, maybe four variables...

    My question is...

    How many arguments are you advised to feed in?

    I have the idea of using the same principle above and having one AI manager script function and feeding 'that' the relevant info for each object... This would entail a lot of arguments though, all of my player attributes and more...

    Thanks

    * Sorry - I thought I was in 'scripting'.
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    As many as you need.
     
  3. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Would it be performance costly to have say 100?

    Is there a better way?

    Thanks
     
  4. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    If you think you will have a function that needs 100 arguments, then I think you are doing something wrong. That seems like code smell from a mile away (Although I have never programmed sophisticated AI's before, so I might be wrong). If you want to begin to include abstraction in your code, and overall making it more programmable/readable, I think it is a good idea too read up on how to program S.O.L.I.D. http://www.cmjackson.net/2009/09/04/solid-programming-principles/

    Some nice reads about solid programming:
    S: http://www.objectmentor.com/resources/articles/srp.pdf
    O: http://www.objectmentor.com/resources/articles/ocp.pdf
    L: http://www.objectmentor.com/resources/articles/lsp.pdf
    I: http://www.objectmentor.com/resources/articles/isp.pdf
    D: http://www.objectmentor.com/resources/articles/dip.pdf
     
  5. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,605
    What are you doing that it needs that many? Use variables and multiple smaller functions so they aren't all running all at one time.
     
  6. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    From the bottom up...

    * A Bunch of 'Action' scripts (1 action per script)... each assigned to a separate object - Fixed Update

    *One ActionAIManager Script... in which to call and feed arguments into the action scripts

    *22 PlayerDatabase scripts (1 on each object - as every object is different)... Upon update these database scripts call the function of 'AIManagerScript' which in turns filters out an appropriate action for said objects... and in turn leads to the function of the actual action

    The ActionAIManager also receives input from a separate TacticalManagementScript...

    A good way of doing things?

    Thanks
     
  7. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    would this be where I would add a class as an argument type maybe?

    Thanks

    Peace
     
    Kiwasi likes this.
  8. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
    Yeah, you should definitely brush up again on your Object Oriented Programming theory and practice. I can confidently say you should never need to pass THAT many arguments to a function.
     
    carking1996 likes this.
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Wrap all of the arguments in a class, then pass that class through. This will generally make your programming pipeline faster, and make your code more maintainable.
     
    zombiegorilla likes this.
  10. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
    I agree with pretty much everything you say around here, but this is not one of them.

    If you need to process that much data at a time, it's unlikely the best place to be doing it is outside the objects themselves. Rather, I would have each object's class define the function with necessary executions, and loop through each object to run the code from within.

    The way the OP described it, it sounds like he wants to do this
    Code (CSharp):
    1. void processAllData(object1, object2, object3, object4, ... object100) {
    2.   object1.doSomething();
    3.   object2.doSomethingElse();
    4.   ...
    5.   object100.keepDoingThings();
    6. }
    When he really should be doing
    Code (CSharp):
    1. foreach(object in allMyObjects) {
    2.   object.runClassSpecificCode();
    3. }
    4.  
    5. class coolObject {
    6.   void runClassSpecificCode() {
    7.     // Different stuff for each different class type
    8.   }
    9. }
    Unless I'm just totally misunderstanding what one of you are saying. And I have eaten a lot of ice cream tonight, so that's entirely possible.
     
  11. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I see what you are getting at. This is certainly a legitimate way to do it. It doesn't always make sense to include the method inside the class. But the foreach is certainly a solid way to do the same thing to a bunch of different objects.

    Pause for a minute while I go read the OP, to be honest I made my answer based on the thread title, rather then an indepth look at the OPs needs. Sloppy, I know...

    ... and exactly what type of ice cream are you eating? Up until very recently my country ate more ice cream per capita then any other nation in the world. Its not really known for its mind altering effects...

    .. And back. In essence we are answering two different questions. I'm struggling to figure out which one the OP was actually asking. But anyway, I'll comment on both methods. Your method is valid if the OP is cycling through multiple objects. Note that you get the same effect with both of the following loops of code. Sometimes the method does not belong with the data.

    Code (CSharp):
    1. foreach(object in allMyObjects) {
    2.   object.RunClassSpecificCode();
    3. }
    Code (CSharp):
    1. foreach(object in allMyObjects) {
    2.   RunNonSpecificCode(object);
    3. }
    What I was getting at is a use case more like this.

    Code (CSharp):
    1. public void DoSomething (float floatA, float floatB, int anInt, string someRandomString){
    2.     // Do something worth doing
    3. }
    And replacing it with this, which is neater

    Code (CSharp):
    1. public void DoSomething (ParamaterObject paramaters){
    2.     // Do the exact same thing
    3. }
    4.  
    5. public class ParamaterObject {
    6.     public float floatA;
    7.     public float floatB;
    8.     public int anInt;
    9.     public string someRandomString;
    10. }
    I've had success with this pattern when handing around data containers like messages. It often doesn't make much sense for the message object to hold the methods to act on its data, the same message might get passed to a quest system, the experience system, the AI and the sound system. The message should not contain methods for each of those systems, this is majorly backwards on dependency. But it can contain valid data for all the systems.

    Thoughts?
     
  12. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
    I rescind my statement and I now agree with 100% of your content again. I was oversimplifying the use case, pretty much making the assumption that each object being acted on wouldn't require processing along with the other data as well, which might not be true.

    Although I'm a fan of pretty much any* ice cream you could think of, tonight was a straight up vanilla-fest. Might not have been the ice cream alone, as it was preceded by steak and potatoes as well. So we might be talking about just your average food coma situation here.

    * - Unless you're sneaking in things that don't or shouldn't exist, like broccoli ice cream.
     
  13. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
  14. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
  15. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Thanks for all of the help... yes indeed passing a class seems to be what I am after...

    At the top of the chain I have 22 individual game objects each with their own 'Data Base script' containing all of their relevant attributes... In update this calls the method of 'AI Manager script' and starts the process filtering through and triggering an appropriate action...

    At this point I need to pass a lot of data 22 times, namely everything contained within each 'Data Base Script'... This is where I need to feed the class of 'Data Base Script'? Thus I make the 'AI Manager Script' take the argument of 'Data Base Script'? I think I get it... :D
     
  16. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Haha Off topic mini game idea...

    You have an ice cream and a pristine floor... At ever increasing temperatures (climates, scenes, etc) you have to guide your cartoon player to eat the ice cream without dripping any on the floor... Doing so will result in the loss of a life! The 'floor' police will take you away!

    You can rotate and move your ice cream trying to balance any drips form dripping off, as well as having a 'bite' and 'lick' button to perform such actions and move toward completing your goal... Be careful though... Biting or licking the ice cream too often may result in a 'brain freeze' and the dropping of the entire ice cream... Gauge the 'brain freeze' UI bar for an idea!

    Some ice creams have added difficulty with the need to consider the physics of 'chocolate flakes' and 'waffers'

    :D :D :D

    Or even better, dropping the ice cream causes the scene to destroy itself in a calamitous comical way - banana skin style!
     
    Last edited: Feb 15, 2015