- Home /
Is there some way to make a property return a static value if the object is null/does not exist?
Hello,
can I make a property of an object return a static value if the object itself is null? I'm trying to use a component system for some of my game objects and I figured it'd be convenient if the property just returned the value 0 if the object didn't exist (if my spaceship didn't have a booster, I don't want to check for missing boosters all the time) It'd be far more convenient if booster power requests just returned 0. My current attempt at coding this looked like this, but it returns null reference errors if I actually try to access the properties.
private bool initialised = false;
[SerializeField, Tooltip("maximum boosting Time for BoosterDrive ScriptableObject"), Range(0f,1000f)] private float maxBoost = 100f;
public float MaxBoost
{
get
{
if(this == null)
{
return 0f;
}
else return maxBoost;
}
}
Honestly no clue if this is even possible, but it'd be very neat if it was.
Answer by unity_ek98vnTRplGj8Q · Dec 07, 2020 at 07:17 PM
I believe you can use Null Conditional Operators and Null coalesciong operators to accomplish this. Something like
public static float GetMaxBoost(BoosterDrive boost)
{
return boost?.maxDrive ?? 0f;
}
Which basically checks if boost in null before it tries to access its property, and if it is null then it returns 0 instead. I haven't really used these myself and I haven't tested this code so I'm not sure if it works.
Of course this does not have to be a static method, you could use it from the calling function like this
Boost boost = null;
float maxBoost = boost?.maxBoost ?? 0f;
I think you are missing the point. Go read his original post :)
That isn't really what he asked though. Yes null coalescing and propagation does what they say they do. But trying to access instance members from a static method is not going to work unless you are using a singleton pattern (or any similar) which literally keeps a static reference to an instance of the whole object.
Not using a singleton pattern.
I was just trying to look for a way to optimise my code, but it seems my big brain idea just doesn't work.
K.I.S.S ... keep it simple ... stupid ;). The biggest brain move is being able to make something work in the most simple way. Don't try to complicate it its probably a sign that you are doing something wrong.
If I'm understanding correctly, he wants to be able to say
float maxBoost = boost.maxBoost;
And have it equal 0 if boost is null. You correctly answered that no, this is not possible as maxBoost is a property of an instance of boost. However, it seems like he wants his code to be as close to this as possible, which is why I replied with the answer that I did.
float maxBoost = boost?.maxBoost ?? 0f;
If he does not want to use my answer then that's fine, but I don't think I've given an incorrect answer.
Similarly trying to check if this is null in a instance properties getter is not going to give you much in the way of joy either. Read up on null references/ pointers and you will see why.
Answer by sacredgeometry · Dec 07, 2020 at 06:45 PM
If this is null then you can't access properties on an object because its a null pointer i.e. it doesn't have a type of your object so it doesnt have the properties of your object either.
i.e. you cant use instance members if you dont have an instance to reference.
Thank you. Do you know if there's some way to make a static function return something non-static?
Or do I just have to deal with it and do something like this?
public static float Get$$anonymous$$axBoost(BoosterDrive boost)
{
if(boost == null) return 0f;
else return boost.$$anonymous$$axBoost;
}
using this as a property:
private float $$anonymous$$axBoost { get {return maxBoost;}}
Just put that to private since I wouldn't want anyone accessing it from the outside. (Is this possible anyways?)
You can make a static method return what ever you want you just need a reference to the object if it doesnt have one.
Static just "means" class (at least you can think of it like that) in this context ... i.e. it's a class method. So loosely speaking there is no concept of this
because there is no instance for this
to apply to. When you make a static member it belongs to the class e.g.
public class $$anonymous$$yClass
{
public static int $$anonymous$$yInt = 120;
}
There is only one $$anonymous$$yInt ... it is not on the instance its on the class. So if you did
var a = new $$anonymous$$yClass();
a.$$anonymous$$yInt = 23;
You would get an error because its static ... i.e. $$anonymous$$yClass.$$anonymous$$yInt. Similarly if you instantiated a bunch of $$anonymous$$yClass objects and changed $$anonymous$$yInt it would "change for all of them" because it doesnt live on the instance :D.
I think you are getting tied up in a knot. What are you actually trying to achieve because there is probably a much simpler way to do it.
I think unity_ek...'s idea is what I basically want.
The idea was that basically when I as a player try to use boost it's just using the property of the instance of the scriptable object. If I haven't actually put a booster object on my spaceship it should just add zero boost.
I just figured this might be a way to keep the lenght of the class that actually uses the boost shorter.
void AddBoost(BoosterDrive boost)
{
accelerationVector = accelerationVector + accelerationVector.normalized * boost?.maxBoost ?? 0f;
}
Your answer
Follow this Question
Related Questions
pre generated static Object in C# 1 Answer
How to list all the properties and methods from the class? 1 Answer
Using static properties that return _instance.variable... 0 Answers
Water4 with different properties across different scenes 0 Answers
how to make easily accessible classes. the type like Unity has in JS for example. 1 Answer