A few clarifications on C# patterns found in the document: LEVEL UP YOUR CODE WITH GAME PROGRAMMING PATTERNS

I’m reading and rereading the document LEVEL UP YOUR CODE WITH GAME PROGRAMMING PATTERNS

It’s fantastic. But I have two C# and one design related question:

  1. C# properties. All of the examples look something like this:
private int currentHealth;
public int CurrentHealth { get => currentHealth; set => currentHealth = value; }

To be clear, doing the above is completely pointless, correct? I understand that properties can allow you to protect the underlying private data and control/validate how it’s set/get. But when all you do is store the value or spit it out unchanged, it’s not helpful, correct? I guess I could see it allowing you more freedom in the future to manipulate the data - is that why it’s done?

  1. I’m starting to recognize the power of a null conditional operator, but I’m wondering about this block:
private void OnDestroy()
{
        if (health != null)
        {
            health.HealthChanged -= OnHealthChanged;
        } 
}

Instead of the above null check, could I simply do:
health?.HealthChanged -= OnHealthChanged;

  1. I’m planning to try out the MVP model for segregating health data and presentation as described in the document, however I’m currently depleting health smoothly in the Update function based on the value of a stored float. For example if an enemy does 2f damage to the player, the value 2f will be used in the Update function to smoothly deplete health. I assuming that using an event based system is a bad idea when a health bar is effectively changed once per frame? I can think of a few ways to improve this, but has anyone come up with a good method to both smoothly deplete health and use MVP that they’d be willing to share?

Thanks for any tips.

Exactly, if for example you have a person with an Age property and an age field and in the future instead of reading the age from somewhere, you read the date of birth, you can use the Age property getter to calculate the age and no changes will be needed to the rest of you codebase.

This won’t work for Monobehaviours, because they have overloaded the equality operator to not check for null, but to check if the C++ side object has been destroyed. For normal C# classes is the same, for Monobehaviours you should always use the == and != operators.

See: https://unity.com/blog/engine-platform/custom-operator-should-we-keep-it

2 Likes

So, in response to 2) “About htis block” the block of code if I read it correctly Is removing an event of which is called OnHealthChange, meaning it disables and stops it. The code you wrote underneath the health?.HealthChanged -= etc. is a pointer operation asking “Does health variable exist, if so continue”, pointers were introduced in C#11 and above (before that they were…difficult) So its in some regards a coding principle that exists but has not been fully operational.

You can fully override everu part of the Unity engine given the knowhow, and in that I credit Unity for them being able to (or perhaps not knowing) that you can override.

The concepts are on principle functional, it is a matter of preference. You can “Unsubscribe” from an event or you could just simply do it in method implementation

1 Like

This has nothing to do with pointers, this is how you unsubscribe from events that have been in C# since forever. The Null-conditional operator means “if the operand before is not null, not if the variable exists”

1 Like

Okay, assuming I am incorrect, how does the code know exactly which operation to look to? this is the sole mention. Does it not point to?

It is a pointer in that it as you 100% correctly states “if this variable(X) is not true doe this” on a surface it is not visually a pointer, but it is one all the same.

Events are not a part of C#, as the concept is unique, you could argue that string variableName = “Three” is an event. The question is more of, how does Unity deal with this information. Unity is coded in C++ not C# we (as users) code in C# but unity is C++

C# doesn’t have pointers, it only has delegates that are objects that contain pointers to methods among other things. Pointers in C# only exist in unsafe context.

Please check your facts, events: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/events/ They are a very basic part of C#.

No, no one could, events are strictly defined in C#, I provided the link above.

Again no, Unity has a part of code in C++, but most of its code nowadays is C#, here’s the code in the github repository that is available for anyone to check: https://github.com/Unity-Technologies/UnityCsReference

1 Like

I agree with your “Unsafe” aspect, but the issue is define unsafe, the definition in this instance would be closer to unknown, or to put it in laymans terms “Does it exist”

Microsoft

It is an old and sadly, not always helpful manner of coding, I can guarantee you, regardless of if it is C# or C++ or heck, even FORTRAN, if you know how, you can make it work in any other language you desire.

Unity at its base is C++, C# is based on C++ I can code something in C++ and make Unity not just know it, but also say hey this overrides me, ill do that instead.

Events, when I say are not a part of C# is more a wording, I can create an entire C# application without subscribing to an “Event”, its a term that is optional

You have no idea what you are talkin about, please go do some basic C# tutorials, because I can’t be having a conversation explaining basic stuff:

I don’t have to define “unsafe”, I’m not using it like we use it in a conversation “not safe”, unsafe like events is very strictly defined and it’s a C# keyword: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/unsafe

You cannot use pointers in C# outside of unsafe context, you cannot use pointer operations outside of unsafe context.

The rest of your post doesn’t even make sense, you are confusing the language that was used to make C#, with what is based upon. No C#, is an OOP language that is based on Java. C++ is not even a managed language, you can create an entire C# application without subscribing to an “Event” but no one would even hire a coder like that, that’s horrible programming.

Please go do some basic C# tutorials.

1 Like
  1. I did not, and have not berated your remarks, do not do the same
  2. Yes C# is based on C++, as is many others, it is an OOP as is C++
  3. I could code you something in any language that outputs a whole different language, I wrote FORTRAN in Java because why not
  4. unsafe is a check variable
  5. Everything I can do in C# I can 100% do In C++, I know because I have done it between Unity and Unreal Engine.
  6. As i said, in C# pointers are quite new, before C#11 they did not exist, but have existed in other languages. Microsoft are at the moment questioning the use of the language itself and adapting it to be more .net (perhaps just a name change rather than anything else) hence Binary serialization is no longer supported y microsoft.

IFIP - recommend that you store most data offline, regardless of language, and minimalise anything that promotes online access. IFIP openly support languages like C++ for its functionality

all in all, we disagree, and that in of itself, is great, we both learn and you seem like a genuine person, given chance our discussions would be good. But we have avoided the topic originally posted