A Little Help With Namespace

Hi this is more of a c# question, less Unity, but hopefully someone can help me.

I am learning namespaces. I have one class STORE_MNG, namespace thisname.storeMng.

I have a btn class BTN, namespace thisname.storeMng.btn

I am trying to have the btn access the STORE_MNG via a function, btnPressed.

void btnPressed(){
storeMng.callStaticFunctionX();
}

I am told that callStaticFunctionX() does not exist in the storeMng namespace.

I have tried different pointers, accessors to the STORE_MNG, thisname.storeMng, STORE_MNG…, Nothing is allowing btn to access the static function in STORE_MNG.

What am I doing wrong??!

Firstly, your capitalisation is a bit off, in C# it’s common to use PascalCase for namespaces, classes and methods.

So, storeMng is your namespace, you can’t call a function in a namespace, you need to call a function in the class. So in your example, it would be thisname.storeMng.STORE_MNG.callStaticFunctionX()

If you put a “using thisname.storeMng” at the beginning of your file, you can use just STORE_MNG.callStaticFunctionX()

So the formula is thename.theClass?

Yes, if you want to use the fully qualified namespace name, but it is more common to add a “using” statement.

Take a look at these links for more information:

https://msdn.microsoft.com/en-us/library/dfb3cx8s(v=vs.71).aspx

Great,
Thank you.

Hi,
So I am having some difficulty again in regards to namespace. I believe I got it working last time as the script I was calling from, is somewhat lower on the totem pole than what I am trying to call from now, which is at the top.

I have example.store as the main name space, class STORE

I then have example.store.mng as the next, class STORE_MNG

I have placed a call in STORE, example.store to STORE_MNG, example.store.mng via many variations of…

//in STORE, example.store
example.store.mng.dothis();

///also have tried....
example.store.mg.STORE_MNG.dothis();

///and this.....
STORE_MNG.dothis(); ///with do this as a static function

all inform me that the name “mng” does not exist in example.store.

What do I do?

so example.store and example.store.mng are both namespaces? With the classes example.store.STORE and example.store.mng.STORE_MNG ? And you want to call the static function dothis on the STORE_MNG class?

If so, then you would do example.store.mng.STORE_MNG.dothis(); or you would use using example.store.mng; and then STORE_MNG.dothis();

Also, I would strongly suggest you adopt the standard capitalisation of namespaces and classes in C# – for example Example.Store.Store and Example.Store.Mng.StoreMng

1 Like

Ok great.
Hopefully I will never need to ask about this again.
Many thanks!!!

Edit. So would you build a project around namespaces? Example, player manager, enemy manager, score manager, using namespaces? Potentially, I could see each one having a namespace. For instance…

Root

Root.Player

Root.EnemyManager

Root.Score

And so on. In my mind, Root, would subscribe to all major functions in each manager. So, player dead, game start, pause… And so on.

Would is be a fisable model?

Namespaces are just a tool to keep your code organised, so feel free to create them how you want to. Once you’ve used them for a while, you’ll quickly see for yourself how it’s logical to group your code.

That being said, don’t create too many of them. If you only have 1 or maybe 2-3 classes in a namespace, it probably doesn’t need its own namespace. For a small game for example, it’s probably enough to have just one namespace for all your code. If you start with one, and it becomes unmanageable, you can look at the classes you have and see which naturally fits in a group, and create a separate namespace for these.

If it helps, you can think of namespaces like you think of folders in a regular file system. If a few scripts are related in a meaningful way, put them in a single namespace … just like you would put a few related documents together in a folder.

In fact, a lot of the big IDE’s out there (think: Eclipse, Visual Studio, etc…) use this convention implicitly. If I create a folder in my project called Eisenpony.SpellingGame.AI and use the IDE to create a new class inside of that folder, Visual Studio will automatically put the class into the Eisenpony.SpellingGame.AI namespace for me.

I think this might help you get a better feeling for how to use namespacing. No need for a new namespace for each class.

If that doesn’t help, well… then just carry on with how you are doing things; experience is the best way to understand.

1 Like

So, for my games, usually I have a RootMng at the head whom each the PlayerMng, EnemyMng, and ScoreMng each report to the RootMng on a major event. Something like when the player dies, passes a level, bring in new enemy… etc.

So, would a single namespace handle this? If I have a namespace “Root”, I would certainly place that in the RootMng script, now, for each sub manager, how would the naming process occur? Would each also be under namespace “Root”, or would each have its own, example, “Root.Player”, “Root.Enemy”… and so on.

We have a single namespace encompassing all of our management-type code. It’s called FracturedState.Game.Management

Honestly - it sounds like you’re really over-thinking this…

Does List have it’s own namespace called System.Collections.Generic.Lists? No. It’s a generic collection so it goes in System.Collections.Generic.

I think you’re misunderstanding namespaces and that’s where the confusion comes from. Namespaces have nothing to do with events and no effect on what a class does. You can create 1 namespace or 10000 namespaces it doesn’t matter. They’re really just folders to organize your classes. In my game, I only have about 3-4 namespaces I think.

No, you don’t put namespaces in scripts, it is the other way around: scripts get placed in namespaces.

Also here’s something that may help you.
Let’ say you have this line:

System.Collections.Generic.List myList;
System.Collections.Generic is the name of the namespace. List is the name of the class. The namespace + the name of the class is called the fully qualified name.

We create namespaces by major features.

For instance, have a “global” namespace which will encompass any general logic, i.e. namespace GameName

Then for different features,

namespace GameName.Tutorial
namespace GameName.SaveSystem
namespace GameName.SocialIntegration.Facebook
etc.

Ok, I was thinking namespaces would allow me to reference other scripts via code much more easily.

So, example…
Root
Root.PlayerMng
Root.EnemyMng

All fall under the Root namespace. As such I could call any member under this namespace, from any other member under the namespace simply by doing this for example…

///in player mng at player death
Root.PlayerDied();

then in the Root Mng, I could do this…

Root.EnemyMng.PlayerDied();

I was thinking it was an easier way to access ones component.

You can’t call methods on namespaces

Honestly, until you start running into issues with your script names becoming too cluttered and similar… just don’t use them at all. They serve very little practical purpose, at least on the scale of a Unity game. They mostly serve to help you understand the organization of your code better, and they’re clearly not doing that for you. Just don’t use them at all.

1 Like

Right, but will the namespace allow access to the class? which if true proves my interpretation.

So, …
from the PlayerMng class wanting to access the RootMng script.

//in player mng
RootNameSpace.RootMngClass.doThis();

Let me give you a practical example of one of the very few times that you’ll actually find namespaces serving a real purpose. There’s a Unity plugin called Vuforia (has to do with AR), and within Vuforia, there was a class called Image. For a long time, this was fine. But then, Unity 4.6 came along and introduced the new UI, including, among other things, its own class called Image. Now, anytime I typed ‘Image’ into a script, the compiler didn’t really know whether I meant Vuforia’s Image class, or Unity’s Image class. Unity’s class was within UnityEngine.UI, and so could always be accessed as UnityEngine.UI.Image, but typing just Image would make the compiler think that I wanted the namespace-less Vuforia Image class. Naturally, this caused problems, but mostly annoyances - I had to use the full namespace “path” to reference Unity’s Image class, every time.

In a more recent version of the plugin, Vuforia stared putting its classes under its own ‘Vuforia’ namespace. And the world was happy again; I’d add ‘using Vuforia’ to the top of my scripts that used that, and ‘using UnityEngine.UI’ to the top of my scripts that used that, and all was well.

It’s important to note one thing: nothing that Vuforia did with namespaces is necessary. They could have renamed the Image class to VuforiaImage, and achieved exactly the same effect, with the only exception being that typing in the class names would have been more annoying. And that is the entire use case of namespaces - to prevent collisions of names by organizating classes into sensible clusters.

2 Likes

If doThis is static then that will work but this has absolutely nothing to do with namespaces. Namespaces aren’t access gates. You could just as easily not namespace RootMngClass (which is a terrible name by the way) and just type RootMngClass.doThis()