0

Basically I am using Unity and it used c#. I want to program procedurally. Can I mimick this with static namespaces and static classes, so that I can use the static namespaces as "header files", so I dont have to instantiate a bunch of singletons. How is this performance wise?

For example:

namespace Balls 
{
    public static Ball [] balls;

    public static class Balls
    {
        public static void UpdateBalls (){}
    }  
}

Another file:

using static Balls;

namespace Player 
{
    public static class Player 
    {
        public static bool PlayerAlive;
        public static Vector3 position;

        public static void UpdatePlayer () 
        {
            for (int i=0; i<balls.Length; i++)
            {
                if (position == balls[i].position)
                {
                    PlayerAlive = false;
                }
            }
        }
    }
}

Another file:

using static Balls;
using static Player;

namespace Main 
{
    public static class Main 
    {
        public static void MainUpdate ()
        {
            if (PlayerAlive) 
            {
                UpdateBalls ();
                UpdatePlayer ();
            } 
        }
    }
}
Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
Tree3708
  • 33
  • 4
  • 2
    Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the [How to Ask](https://stackoverflow.com/help/asking) page for help clarifying this question. On another note, using static classes the way you are as namespaces, defies the purpose of having namespaces anyways; there could be naming conflicts etc. – Trevor Nov 25 '19 at 12:44
  • Of course you can do that. But what does this have to do with **object-orientation**? The point here is that you have objects of concrete classes, nt just static toolboxes. – MakePeaceGreatAgain Nov 25 '19 at 12:49
  • The thing is I just dont like how c# forces everything to be an instantiated class. I have lots of data which only needs to be represented once, so I dont need a class for it. I dont want to spawn one instance for a class. Also, I want to practice procedural programming but I have to use c# for now. I am wondering how this work performance-wise? Do namespaces add alot of overhead in c#? For example I would make "Ball" a class, but general info on the state of all balls does not need a class. – Tree3708 Nov 25 '19 at 12:55
  • The state of all balls could easily be handled by retaining the state of each ball within that ball instance. Then you simply need to access those states with a statemanager. – gmiley Nov 25 '19 at 13:05
  • Yes. But in game design it is not performant for the CPU to loop over many classes at the same time, every frame. So, I am storing the info of the balls as separate arrays which dont need to be in a class. That way when I need to change a value the whole data of the class doesnt need to load, I can just access a simple array. Anyway, this was just a very simple example. What I am asking is if using static namespaces like this is bad for performance. Is it the same as using a header file in say c++? I have made one game using OOP, this is my second. I find this static method very liberating. – Tree3708 Nov 25 '19 at 13:15

0 Answers0