Search Unity

software design best practices

Discussion in 'Scripting' started by verbatimline, Apr 7, 2009.

  1. verbatimline

    verbatimline

    Joined:
    Mar 24, 2009
    Posts:
    88
    HI guys,
    So i coded the interface for a simple test I am working on. It is solely based on OnMouse and update functions. The question is regarding all the variables needed for my script. I have 24 of them and it seems excessive. What I fear is that it may be the wrong way to go about it by slowing things down.

    My question is: Would it be wise and optimized to break this script down into functions that take arguments and return results or keep it the way I have it which is mainly comprised of functions that do stuff to global variables which are then sampled when needed?

    Is there a way to time a cycle. The Maya api has a class that does specifically that.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Simply having variables doesn't slow anything down. Calling functions does, though...the more functions you call, the more overhead you have. Granted, in most cases this overhead isn't going to amount to much, and having local variables can be faster. Best thing is to make the best design you can think of, and worry about making the game faster later, if necessary.

    --Eric
     
  3. verbatimline

    verbatimline

    Joined:
    Mar 24, 2009
    Posts:
    88
    I'm not a javascript guy but a variable outside the update or OnMOuse functions is automatically a global variable, correct? or do you have to use the Global keyword in order for the var to global?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yep.

    --Eric
     
  5. verbatimline

    verbatimline

    Joined:
    Mar 24, 2009
    Posts:
    88
    cool thanks
     
  6. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    I think more correctly these are called member variables, since these variables are members of the objects that you instantiate from the class. This means that if you have an enemy class with an ammo variable and create two enemy objects from it, they will each have their own ammo variable, so the two enemies can have different amounts of ammo.

    Global variables on the other hand are normally meant to be variables that only exist once in the game rather than once per object.

    Rune
     
  7. verbatimline

    verbatimline

    Joined:
    Mar 24, 2009
    Posts:
    88
    that is exactly how it works in MEL scripting.