Global game logic placement?

Hi everyone!

I’m currently investigating the viability of porting a project to Unity, and have been poking around in the trial since lunch. :slight_smile:

I have a question or two after reading the documentation and browsing through the tutorials. I apologize in advance for any ignorance on my part.

  1. How/where would you place handlers for “global” game logic? I notice that most or all scripts are bound to objects (at least in the tutorials). What if you want a global script to keep tabs on events and create new events or new objects if needed? Where would you place it and when/how it is called? Would it too be called with update and fixedupdate?

  2. How does Unity handle globals? Does Unity follow the various language conventions? (IIRC, for example in javascript, variables declared outside of functions as vars are global) If not, how is a global variable declared and accessed?

Many thanks in advance!
Dan

Hi Dan and welcome to Unity.

  1. Usually I just make an empty game object and attach the script to it.

Usually this script is also in the first scene - which also might show a splash screen - and you keep it alive using DontDestroyOnLoad
http://unity3d.com/Documentation/ScriptReference/Object.DontDestroyOnLoad.html

  1. variables outside of functions are not global variables in Unity’s JavaScript.
    You can use the static keyword to make a global variable:
static var someGlobal = 5;

(In a script ScriptName.js)

You can access it from another script using:

ScriptName.someGlobal = 6;

Here’s a handy script at Unify’s wiki. There’s a bunch of great stuff there.
http://unify.bluegillweb.com/scriptwiki/index.php/AManagerClass

It basically makes a script you attach to any object easily accessible. (It makes the class a singleton.)

Say you made a script(class) called GameLogic

Just include the code (with appropriate alterations) from that link and your class will be easily accessible from elsewhere via:

**GameLogic.instance.**whateverMemberVariableOrFunctionYouWantGoesHere()

It’s exactly what you want.

In my games I make an empty game object called SingletonsGO (GO is for Game Object) and I add all the singleton class scripts I want to it.

It works great.

Handlers? Are you coming from Director by any chance? I just started a Director project over from scratch with Unity, and it was VERY worth the effort.

Figuring out how scripts talk to each other and how globals work, compared to Director, was a mental block for me, but you quickly appreciate how those things work in Unity.

Could someone point me to some samples or explain how globals work in Director. Just want to see from where people are coming from.

Many thanks for the replies! The described approach will no doubt work well for me. I assume the “singleton” scripts attached to empty objects do get called each frame as well (useful for global counters and similar)?

As for Director, it’s a little bit more straight forward.
Basically, scripts are divided into separate types (movie scripts, and parent scripts for example). Movie scripts are accessible throughout the application, while parent scripts are really classes of sorts (used to create instances, called children in Director). Any variable defined using the “global” keyword becomes global - as long as it is outside of a function.

So, to have a script run at startup, you simply create a movie script and define a handler/function within it that listens for a director key event (startmovie etc.). Within that same script, you could define globals that are immediately available to all other scripts. Did I manage to explain that clearly enough? :slight_smile:

While we’re on the topic of syntax, what about constants? Is there a specific way to define them, or do you simply use static variables for those as well?

TIA!
Dan

In Director, a global is accessible by ANY script.

You identify it as global at the top (or at least, before it’s used) of every script that uses it (NOT just when it’s first created):

global playerhealth

You can identify multiple globals in one line, too:

global playerhealth, omeletesremaining, antidotediscovered

That’s about it. If identified as global, any script can change those variables and any other script will have access to the current values.

The one quirk is that you can use the SAME names as non-global just by not identifying them. So you could have a script that used playerhealth as global, but not omeletesremaining. In which case, you could use the name omeletesremaining for some unrelated local purpose–but it would be a bad habit.

(That’s all with conventional Lingo syntax. Lingo does have JavaScript syntax too now.)