<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Namespaces on DAD 424 Sound Design for Games</title><link>https://dsu-digital-sound-design.github.io/f-25-dad-424-sound-design-for-games/tags/namespaces/</link><description>Recent content in Namespaces on DAD 424 Sound Design for Games</description><generator>Hugo -- gohugo.io</generator><language>en</language><lastBuildDate>Wed, 12 Jun 2024 00:00:00 +0000</lastBuildDate><atom:link href="https://dsu-digital-sound-design.github.io/f-25-dad-424-sound-design-for-games/tags/namespaces/index.xml" rel="self" type="application/rss+xml"/><item><title>FMOD Namespaces</title><link>https://dsu-digital-sound-design.github.io/f-25-dad-424-sound-design-for-games/lectures/week-11/fmod-namespaces/</link><pubDate>Wed, 12 Jun 2024 00:00:00 +0000</pubDate><guid>https://dsu-digital-sound-design.github.io/f-25-dad-424-sound-design-for-games/lectures/week-11/fmod-namespaces/</guid><description>Key Concepts Namespaces group related types in C# and avoid name collisions. Using directives (using ...;) make types in a namespace available without full qualification. Unity and FMOD both expose APIs through namespaces that you reference from your scripts. Recap: Namespaces &amp;amp; Classes in Unity When you create a new Unity script, you’ll see using directives at the top—these specify namespaces. They let you access types from Unity’s API without fully qualifying them.</description><content>&lt;h2 id="key-concepts">&lt;strong>Key Concepts&lt;/strong>&lt;/h2>
&lt;ul>
&lt;li>Namespaces group related types in C# and avoid name collisions.&lt;/li>
&lt;li>Using directives (&lt;code>using ...;&lt;/code>) make types in a namespace available without full qualification.&lt;/li>
&lt;li>Unity and FMOD both expose APIs through namespaces that you reference from your scripts.&lt;/li>
&lt;/ul>
&lt;p>&lt;strong>Recap: Namespaces &amp;amp; Classes in Unity&lt;/strong>
When you create a new Unity script, you’ll see using directives at the top—these specify namespaces. They let you access types from Unity’s API without fully qualifying them. Below that, each script defines a public class that usually inherits from &lt;code>MonoBehaviour&lt;/code>, which provides lifecycle methods like &lt;code>Start()&lt;/code> and &lt;code>Update()&lt;/code>. &lt;code>MonoBehaviour&lt;/code> lives in the &lt;code>UnityEngine&lt;/code> namespace.&lt;/p>
&lt;p>You can view the definition of classes like &lt;code>MonoBehaviour&lt;/code> in Visual Studio using “Go To Definition,” which helps to reveal their structure and included namespaces.&lt;/p>
&lt;p>Block-scoped and file-scoped namespaces:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-csharp" data-lang="csharp">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// Block-scoped (common in Unity projects)&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">namespace&lt;/span> UnityEngine
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>{
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">class&lt;/span> &lt;span style="color:#a6e22e">MonoBehaviour&lt;/span> : Behaviour
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// class contents&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>File-scoped (C# 10+):&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-csharp" data-lang="csharp">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">namespace&lt;/span> UnityEngine;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">class&lt;/span> &lt;span style="color:#a6e22e">MonoBehaviour&lt;/span> : Behaviour
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>{
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// class contents&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="working-with-namespaces">&lt;strong>Working with Namespaces&lt;/strong>&lt;/h2>
&lt;p>Create a new script in Unity called &lt;code>MyScript.cs&lt;/code> to explore namespaces.&lt;/p>
&lt;p>If you remove a using directive (for example, &lt;code>using UnityEngine;&lt;/code>), references like &lt;code>MonoBehaviour&lt;/code> will no longer resolve and you’ll get compiler errors. You can also declare your own namespaces to organize custom scripts.&lt;/p>
&lt;p>For example, using a traditional namespace declaration:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-csharp" data-lang="csharp">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">namespace&lt;/span> MyNamespace {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">class&lt;/span> &lt;span style="color:#a6e22e">MyClass&lt;/span> { }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>To use &lt;code>MyClass&lt;/code>, either add &lt;code>using MyNamespace;&lt;/code> to the top of another script or fully qualify it as &lt;code>MyNamespace.MyClass&lt;/code>.&lt;/p>
&lt;p>&lt;code>MonoBehaviour&lt;/code> derives from the &lt;code>Behaviour&lt;/code> class (also in &lt;code>UnityEngine&lt;/code>). Use “Go To Definition” in your IDE to trace the inheritance chain and see which namespaces types come from.&lt;/p>
&lt;h2 id="fmod-namespaces-in-unity">&lt;strong>FMOD Namespaces in Unity&lt;/strong>&lt;/h2>
&lt;p>FMOD commands for game audio, like &lt;code>PlayOneShot&lt;/code>, rely on namespaces just like Unity does. Example component:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-csharp" data-lang="csharp">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">using&lt;/span> FMODUnity;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">using&lt;/span> UnityEngine;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">class&lt;/span> &lt;span style="color:#a6e22e">PlayEvent&lt;/span> : MonoBehaviour
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>{
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e"> [SerializeField]&lt;/span> &lt;span style="color:#66d9ef">private&lt;/span> &lt;span style="color:#66d9ef">string&lt;/span> eventPath; &lt;span style="color:#75715e">// or use EventReference&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">void&lt;/span> Start()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// Positional one-shot&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> RuntimeManager.PlayOneShot(eventPath, transform.position);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// Or, to attach to a GameObject:&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// RuntimeManager.PlayOneShotAttached(eventPath, gameObject);&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Here:&lt;/p>
&lt;ul>
&lt;li>&lt;code>FMODUnity&lt;/code> is the FMOD Unity integration namespace.&lt;/li>
&lt;li>&lt;code>RuntimeManager&lt;/code> is a class within &lt;code>FMODUnity&lt;/code>.&lt;/li>
&lt;li>&lt;code>PlayOneShot&lt;/code>/&lt;code>PlayOneShotAttached&lt;/code> are methods on &lt;code>RuntimeManager&lt;/code>.&lt;/li>
&lt;/ul>
&lt;p>To see how these work, open the FMOD integration scripts in your project (for example, &lt;code>RuntimeManager.cs&lt;/code>) and inspect how namespaces and classes are organized. Declaring &lt;code>using FMODUnity;&lt;/code> lets you call &lt;code>RuntimeManager.PlayOneShot(...)&lt;/code> without repeatedly writing the full namespace.&lt;/p>
&lt;p>Go and find your Unity project’s &lt;code>FMODUnity&lt;/code> folder (usually under &lt;code>Assets/Plugins/FMOD/src&lt;/code>) to explore the FMOD classes and their namespaces.&lt;/p>
&lt;h3 id="understanding-playoneshot">Understanding PlayOneShot&lt;/h3>
&lt;p>Common overloads you’ll use in Unity:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-csharp" data-lang="csharp">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// Positional one‑shot at a world position&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>RuntimeManager.PlayOneShot(&lt;span style="color:#66d9ef">string&lt;/span> eventPath, Vector3 position);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>RuntimeManager.PlayOneShot(EventReference eventRef, Vector3 position);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// One‑shot attached to and following a GameObject&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>RuntimeManager.PlayOneShotAttached(&lt;span style="color:#66d9ef">string&lt;/span> eventPath, GameObject go);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>RuntimeManager.PlayOneShotAttached(EventReference eventRef, GameObject go);
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Notes:&lt;/p>
&lt;ul>
&lt;li>Fire‑and‑forget: these methods return &lt;code>void&lt;/code>. You can’t stop or change parameters afterwards. For control, use &lt;code>FMOD.Studio.EventInstance&lt;/code> or a &lt;code>StudioEventEmitter&lt;/code>.&lt;/li>
&lt;li>Position vs attached: &lt;code>PlayOneShot&lt;/code> uses a fixed &lt;code>Vector3&lt;/code> at spawn time; &lt;code>PlayOneShotAttached&lt;/code> follows the GameObject’s transform.&lt;/li>
&lt;li>EventReference vs string: &lt;code>EventReference&lt;/code> is type‑safe and assignable in the Inspector; a string path is quick but error‑prone.&lt;/li>
&lt;/ul>
&lt;p>Example using an &lt;code>EventReference&lt;/code> field:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-csharp" data-lang="csharp">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">using&lt;/span> FMODUnity;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">using&lt;/span> UnityEngine;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">class&lt;/span> &lt;span style="color:#a6e22e">PlayEventRef&lt;/span> : MonoBehaviour
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>{
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e"> [SerializeField]&lt;/span> &lt;span style="color:#66d9ef">private&lt;/span> EventReference eventRef;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">void&lt;/span> Start()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> RuntimeManager.PlayOneShot(eventRef, transform.position);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// Or follow this GameObject:&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// RuntimeManager.PlayOneShotAttached(eventRef, gameObject);&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="example-when-you-need-control">Example: When You Need Control&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-csharp" data-lang="csharp">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// For parameter control, don&amp;#39;t use PlayOneShot:&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">var&lt;/span> instance = RuntimeManager.CreateInstance(&lt;span style="color:#e6db74">&amp;#34;event:/Music/Stinger&amp;#34;&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>instance.setParameterByName(&lt;span style="color:#e6db74">&amp;#34;Intensity&amp;#34;&lt;/span>, &lt;span style="color:#ae81ff">0.8f&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>instance.start();
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// ... later, you can modify or stop it ...&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>instance.release();
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="key-takeaway">Key Takeaway&lt;/h2>
&lt;p>&lt;strong>The GUID overload is the &amp;ldquo;real&amp;rdquo; function&lt;/strong> - the EventReference and string versions are just convenient wrappers that convert their input to GUID and pass it along. This is a common design pattern called the &lt;strong>Telescoping Pattern&lt;/strong> or &lt;strong>Overload Chaining&lt;/strong>.&lt;/p>
&lt;p>&lt;strong>Bottom line:&lt;/strong> PlayOneShot functions are perfect for simple one-off sounds that don&amp;rsquo;t need runtime control.&lt;/p>
&lt;h3 id="event-instances-and-nested-namespaces">&lt;strong>Event Instances and Nested Namespaces&lt;/strong>&lt;/h3>
&lt;p>FMOD exposes nested namespaces, such as &lt;code>FMOD.Studio.EventInstance&lt;/code>:&lt;/p>
&lt;ul>
&lt;li>&lt;code>FMOD&lt;/code> is a namespace.&lt;/li>
&lt;li>&lt;code>Studio&lt;/code> is a nested namespace inside &lt;code>FMOD&lt;/code>.&lt;/li>
&lt;li>&lt;code>EventInstance&lt;/code> is a struct inside &lt;code>Studio&lt;/code>.&lt;/li>
&lt;/ul>
&lt;p>Open up the definition of &lt;code>EventInstance&lt;/code> in Visual Studio to see its members and methods. &lt;code>EventInstance&lt;/code> is a value type (struct) representing a playable instance of an FMOD event.&lt;/p>
&lt;blockquote>
&lt;p>Struct: A value type that holds data directly, unlike classes which are reference types.&lt;/p>
&lt;/blockquote>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-csharp" data-lang="csharp">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">struct&lt;/span> &lt;span style="color:#a6e22e">EventInstance&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>{
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// Methods and properties for controlling the event instance&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">public&lt;/span> RESULT start();
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">public&lt;/span> RESULT stop(STOP_MODE mode);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">public&lt;/span> RESULT setParameterByName(&lt;span style="color:#66d9ef">string&lt;/span> name, &lt;span style="color:#66d9ef">float&lt;/span> &lt;span style="color:#66d9ef">value&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// ... more members ...&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>See if you can find methods like &lt;code>start()&lt;/code>, &lt;code>stop()&lt;/code>, and &lt;code>setParameterByName()&lt;/code> in the definition.&lt;/p>
&lt;p>If you use types from &lt;code>FMOD.Studio&lt;/code>, add the appropriate using directives:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-csharp" data-lang="csharp">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">using&lt;/span> FMOD;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">using&lt;/span> FMOD.Studio;
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Alternatively, fully qualify names (for example, &lt;code>FMOD.Studio.EventInstance&lt;/code>).&lt;/p>
&lt;p>&lt;strong>Streamlining Your Scripts&lt;/strong>
Unity sometimes adds unused namespaces to your scripts by default. Visual Studio will gray out those not needed; clean them up to keep your code clear.&lt;/p>
&lt;p>&lt;strong>Wrap-Up&lt;/strong>
By understanding and leveraging namespaces you can:&lt;/p>
&lt;ul>
&lt;li>Organize code clearly and avoid name collisions.&lt;/li>
&lt;li>Use shorter, clearer code with &lt;code>using&lt;/code> directives or fully qualified names when appropriate.&lt;/li>
&lt;li>Navigate Unity and FMOD APIs more confidently during audio implementation.&lt;/li>
&lt;/ul></content></item></channel></rss>