I’m currently investigating the viability of porting a project to Unity, and have been poking around in the trial since lunch.
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.
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?
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?
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.
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?
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?
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.)