Referencing GameScript from component scripts

Editor
Feb 14, 2010 at 1:28 PM

as above. im trying to make an object play a sound under a condition in its update loop but i assume its going to be bad practice to reload the SoundBank for each instance of the component, so im just wondering if i can use something like getservice to take the sounds from my GameScript.cs.

Bubzy.

 

Feb 15, 2010 at 1:53 AM
Edited Feb 15, 2010 at 1:53 AM

This is how i do it;

In ScriptManager:
DomainName = "ScriptManager";
 engine.CreateDomain(DomainName);


then fetch it from a componentscript likte this

ScriptManager mScriptManager;
mScriptManager= engine.GetScript<ScriptManager>("ScriptManager");


mScriptManager.Sound.Playsound("scream");


//Torshall

 

 

 

Coordinator
Feb 16, 2010 at 12:02 AM

BlahManager is an anti-pattern. Think twice before going down that road.

Why not just put this 'global'-ish functionality in the game script itself? It's already there and it's going to exist for life of your application.

Editor
Feb 16, 2010 at 7:47 AM
Edited Feb 16, 2010 at 7:48 AM

public static SoundBank soundBank; 

in GameScript.cs

 

OxGame11.GameScript.soundBank.PlayCue("sheep1"); 

in your script files.

this seems to work nicely

 

(tutorials on the way btw, but i lost about 2 hours work on them yesterday so im considering a new method)

bubzy :D

 

Coordinator
Feb 16, 2010 at 8:32 AM

Globals are even worse than blahmanagers :)

Also, you're violating encapsulation by exposing the sound bank publicly. Encapsulate it an expose forwarding methods.

Just do something more like -

Engine.GetScript<GameScript>().PlayCue("sheep1");

Really, this doesn't even go far enough. Your audio system should be a service object. This is preferable -

Engine.AddServices<AudioSystem>(new AudioSystem()); // in engine's service creation hook

Engine.GetService<AudioSystem>().PlayCue("sheep1"); // to use

- or you can just cache the audio system in a private member in the script's ctor.

Just try to avoid anti-patterns and globals :)