2
\$\begingroup\$

I have used Unity 3D for quite some time, and a lot of my game programming experience originates from using UnityEngine.

Writing in XNA, I am setting up a nested list of objects. Each object will be an instance of a class, GameObject. At the point of creation, I will have to perform some administration on each element, to sort through them and allow correct operation.

My understanding is that if I have the administration function inside the class, each instance will contain the instructions to perform the task, effectively having N times the same instructions. I have seen some functions instead use the static identifier, in UnityEngine. My understanding is that each instance will instead reference the same copy of the instructions, tidying everything up a bit better.

From what research I have done, all I can find supports that creating static functions inside instantiated instances of a class is impossible. However, I have functions inside UnityEngine that are static, and linked to instantiated copy's of a single class.

My question is: How do I contain a static function inside my class, to perform routine administration function? I am wanting it to be able to crunch numbers derived from the instance calling it, in order to set up other fields within the instance. Would I have to send a ref type of that field, to ensure I am editing the exact instance of it, or do I have a keyword that automatically points to the instance calling the function?

\$\endgroup\$
  • 2
    \$\begingroup\$ Structure your code based on correct semantics, not potential optimizations - especially if you don't have thorough knowledge of the internals of the platform you're programming (and thus optimizing) for. C# doesn't need to duplicate the code of instance methods (and neither does any other OOP language that I know of, as it would be terribly inefficient and would exclude the language from many potential devices and operating systems). \$\endgroup\$ – T. C. Aug 26 '15 at 17:56
  • \$\begingroup\$ @TheodorosChatzigiannakis, my confusion comes from UnityEngine specifically handling these functions as statics. It made sense, logically, that if they didn't need to set it up that way, they would not have. As Pat has pointed out, C# does not need to duplicate, so as it is my optimization is running as intended :) \$\endgroup\$ – Gnemlock Aug 26 '15 at 18:00
10
\$\begingroup\$

In C#, having an instance (non-static) method in a class does not copy the instructions for each object created from that class. Static or not, the data that represents the instructions only exist in one place.

The member variables, or state, of each of the instantiated objects do get their own space in memory. When you write a non-static method on a class, the compiler adds a first parameter to that method which is the location of memory where the specific instance's state is stored.

In general, but no where near always: If you need a method on a class to access the state of an object, make it a non-static method. If you need a method that does something without needing state, make it static.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Solves the problem for me. Still unsure why Unity handles these functions as statics. \$\endgroup\$ – Gnemlock Aug 26 '15 at 18:01
  • \$\begingroup\$ @Timelord64 Unity is a strange beast. It's good for fast development, but I wouldn't necessarily say it's the best example of good game architecture or programming. Such things are of course subjective. \$\endgroup\$ – Pharap Aug 27 '15 at 0:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.