- Home /
Can you have an abstract property/variable in a class?
So basically what I want is to know the best way or easy way to ideally get the compiler to throw an error if a variable I assign to an abstract class goes unassigned in value.
For example
if I have an abstract weapon class, pretty much all weapons will have a rate of fire and a Fire function.
Now i can declare a Fire function and if i don't do something with that function in a class that inherits I get tossed an error. If I have the variable RateOfFire though and never assign it no error is thrown.
This becomes a problem because like if i have a projectile base class and it has a damage value. I want to set a damage value for each of the "bullet" types in my game. However if i forget it could take quit a while to figure out why i'm getting a damage of 0 or 1 or whatever the default is or even the last weapons damage or whatever.
Basically even if perhaps i have a stun weapon that does zero damage i'd like to specify it does zero damage because i'd rather it toss an error and type damage = 0; than end up forgetting to make my rockets do damage and go WTF why isn't anything dying or its sometimes dying after I shoot and maybe sometimes it 1 hit kills cause it has the damage of my nuke weapon i just used or something.
Should i use a if statement or something or what is there a way to force a class variable to be specifically assigned a variable by an inheriting function?
any help? does the question make sense?
I just can't imagine no one has ever wanted to force a variable to be abstract before
Answer by whydoidoit · Apr 27, 2013 at 02:26 AM
You could make it an abstract property but that won't help that much as it won't show up in the Unity inspector.
It sounds to me like you should define a base class abstract method called verify object that calls a method that will be overridden in each subclass and whose job is to verify the settings that you have made. I'd just make that a series of Debug.LogError if in the derived class something hasn't been set.
And actually an abstract property would just make you define how it worked, not set a value.
Thanks! I'll probably just go with the property. I don't $$anonymous$$d it not showing up in the inspector. I don't know i just never change values there, I don't know why but i don't care to use it for that.
I use the editor values mostly just to debug quickly by pause check and to manually move objects to a position (placing a camera for example at why to my eye appears to be the correct place for a 3rd person camera to sit at)
I don't really use it ever to adjust variables on the fly.
perhaps because I use C# so unless i declare it public it isn't viewable by default anyways.
I was considering the property I was just wary (i'm still a new programmer and still unsure of good coding practices, I have read about about don't have empty classes really or empty functions and stuff and I was worried that having a property which is basically a function nearly, just for the sake of making a variable abstract was bad coding practice)
well i was basically looking at this
http://www.codinghorror.com/blog/2006/08/properties-vs-public-variables.html
the thing is the code at the top is what I was going to do and it basically says that I shoudlnt do that :P
Well it says you shouldn't then says you should :D
If you're program$$anonymous$$g .NET in Visual Studio for any modern framework then pretty much everything should be a property (they even have a short syntax for it now), however as Unity doesn't support properties in the inspector (hopefully one day it will grow up and do that!) it's not much point using them unless you need it at runtime.
They also would only force you to define the handling of the property, not the value.
This is a classic example of where unit tests would be useful. Perhaps you should consider that? Having a test scene that instantiates objects and tests that they are right - then you could have an editor script that opened that scene and logged it.
Basically every time you write an object you write a test class for it that is only compiled if UNITY_EDITOR is defined.
I will look up unit tests and how they are done.
I'm self taught which isn't a big deal expect that it means I can end up program$$anonymous$$g for a year and if i've never run into a problem and been told oh there is this thing called unit tests or something. Basically until I run into problems where the solution is "this" I don't learn it.
I mean I read a program$$anonymous$$g book to start but yea. I didn't know what abstract and virtual classes were until I was like well i know I want to use inheritance (which my book covered and i knew about from reading on classes) and got to reading about it more and was like well I want every weapon to have a fire function and i want inherited functions to be FORCED to define it. How can i do that?
So off to read about unit tests!
Thanks as always for your help
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Problem with 3D platform tutorial 2 Answers
Can be mesh asset saved with an editable parameter? 0 Answers
Best Language for Unity 0 Answers
Problem with getting a value from a enum 2 Answers