Global Scripts.... how and where?

Discussion in 'Scripting' started by Ramen Sama, Jun 7, 2009.

  1. Ramen Sama

    Ramen Sama

    New Member

    Joined:
    Mar 28, 2009
    Messages:
    561
    I want to have some globally accessed scripts that are not associated with any objects.

    Do i need to create some empty objects to use as a base for these global scripts/variables and attach the scripts to it?
  2. raiden

    raiden

    Member

    Joined:
    Feb 8, 2009
    Messages:
    207
  3. Quietus2

    Quietus2

    New Member

    Joined:
    Mar 28, 2008
    Messages:
    2,061
    What you're looking for is a singleton class. There's numerous ways to implement them and no you don't always need to attach it to a game object. it's an oft discussed topic.

    http://www.unifycommunity.com/wiki/index.php?title=Singleton

    You can start there then search around the forums for other examples, find one most appropriate for your situation.
  4. zigs

    zigs

    New Member

    Joined:
    May 27, 2009
    Messages:
    151
    I'd go for static variables/functions in a seperate class/file :)
  5. Quietus2

    Quietus2

    New Member

    Joined:
    Mar 28, 2008
    Messages:
    2,061
    Doing that is akin to asking someone to walk through a minefield with magnetized iron boots on.
  6. Ramen Sama

    Ramen Sama

    New Member

    Joined:
    Mar 28, 2009
    Messages:
    561


    I created a script and added static function and was able to call it from other scripts. I guess this is basically what i want.

    How is doing it that way a bad idea? That singleton class seems redundant when i can just do it with static variables.

    Care to explain more? I come from using Torque. And with torque you simply called a function or variable by it's name. Basically all functions/variables in scripts were accessible to any other function.
  7. zigs

    zigs

    New Member

    Joined:
    May 27, 2009
    Messages:
    151
    I don't see the big problem. If you want something to be global, why not just make it grobal?
    On the other hand, It's rarely necessary to have such global functions and variables. This is exactly why object orintation is supirior to procedural coding, so don't overdo it.
    Please explain why it's necessary to drag Singletons into it? :)

    - Chris
  8. Thomas Lund

    Thomas Lund

    New Member

    Joined:
    Jan 18, 2008
    Messages:
    465
    I would also like to know whats wrong with a simple static class.

    We use that all over vs. the wiki-singleton pattern, and it has yet to bite us in any way.
  9. Quietus2

    Quietus2

    New Member

    Joined:
    Mar 28, 2008
    Messages:
    2,061
    There's nothing wrong with using a static class if say you're looking for a game/score manager of some type.

    Static variables and functions come with some pitfalls, one just needs to be aware of them. For example, it seems in every one of the thousands of the 'How to access variables in another script' threads, someone will end up suggesting 'just make it static!' That poor user then wonders why his score isn't reseting between scenes, why he now has variables defined two scenes before the script is even loaded, etc.
  10. Thomas Lund

    Thomas Lund

    New Member

    Joined:
    Jan 18, 2008
    Messages:
    465
    Ahhh yes - but that goes for singletons too and other patterns - use when appropriate and not before having read the "manual".

    Just because we are making games doesnt mean that proper computer science knowledge is required :-D

    /Thomas
  11. Ramen Sama

    Ramen Sama

    New Member

    Joined:
    Mar 28, 2009
    Messages:
    561
    One thing i did discover was that you don't have to have a script attached to an object to call functions from that script. For example, you can have a script called TestScript.js in your project folder somewhere.

    In a script attached to an object you can call a function from that script file even though it's not in the scene... using:

    Code (csharp):
    1.  
    2. TestScript.functionname();
    3.  

    this is basically what i was wanting. Glad you don't have to have every script attached to an object to use the scripts. I'm sure nearly everyone knew this, but i'm new :p