- Home /
How should I give gameobjects health?
I've been watching allot of videos teaching me about inheritance, interfaces, polymorphism etc, and I'm thinking about starting on a new project using what I've learned to make a bigger game than I've ever made before, but I don't know what tool I should use to do what because there are endless ways to do basically anything, and I want to go the most practical route.
I thought about implementing inheritance by creating a humanoid class as the base class, containing all of the basics like pickUpItem() and takeDamage().
However, what if I want more classes, other than just humanoids, to be able to take damage? Should I make one component that deals with health and spread it on everything I want to be able to take damage? or should i make an interface like IDamagable that other classes that I want to be able take damage implement? or should I go a bit extreme and make every single class that should take damage inherit from a class that contains a function that deals damage?
If I decide on using either a component or an interface should I remove the takeDamage() function from the humanoid class and make them use a component or an interface like all the other classes/objects do?
Answer by Laiken · Dec 18, 2016 at 06:36 AM
I normally decide how I write my code based on this:
1) what will make it harder for bugs to appear
2) what will make the code more easy to understand in case I need to go back to it to change later
3) what I am more confortable with
In your case I would probably make a script that contains health and other data that the objects that have health also have and name it UnitStats or UnitInfo. Just because it matches the 3 conditions better than other alternatives (I look at the script name and I already know exactly what it is about and it will be easy to understand and make changes). But you might be more confortable with another approach
But seriously. I think it's one of those things that you learn better when you do it the wrong way. It makes you give more value and feel better when you know exactly what you are avoiding when you are writing your code the "correct" way
You're right. It feels allot better when you understand why you're writing the code a certain way. However, I don't feel comfortable going into a project, knowing that I will be wasting allot of time doing it incorrectly and in the end having to redo it all over again, that's why I hesitated to start right away.
One question tho: Isn't it allot more costly having to run a getComponent every time to check if an object has a health component ins$$anonymous$$d of an inheritance or an interface where I $$anonymous$$NOW that it can take damage? Is there perhaps a better way of checking if it can take damage without running getComponent?
I'd say you should just run GetComponent everytime until you find another way of shortening the code process. also if you do ever get stuck on a code just send a ask away on the Unity Answers forums, people are bound to help :) (Just make sure to google your situation first otherwise people might tell you it's a duplicate question :p ) Also I highly suggest you add a comment ( "//message" ) under every code you do describing what it does so you can come back later to it and know what it's supposed to do, it also help people trying to fix your code cause it tells them what you're trying to accomplish.
I know exactly what you mean! I lost a lot of time before because of stuff like that and those 3 guidelines I follow are what I concluded based on my previous mistakes! But I think that, unless you have a lot of experience with program$$anonymous$$g, that kind of time losing will be unavoidable, but you will learn from it.
About the performance thing. You will probably not get performance hits if you are not using lots of GetComponents every frame. I heard some times that you spend more time worrying about optimization before the game is ready than if you fix the parts of your code that are giving performance hits with the profiler after the game is ready.
But if you want to see what is better, you can do this:
public int loopAmount = 1000;
void Update()
{
for (int i = 0; i < loopAmount; i++)
{
Health health = GetComponent<Health>();
}
Debug.Log(Time.unscaledDeltaTime);
}
it will use GetComponent 1000 times per frame (or whatever amount of times you decide) and see the time it takes for that process to happen. After that do the same using the other method you want to compare and see if there's noticeable difference. But again, if you are using it only when you need to get the health from someone, it will matter little if you use GetComponent or not.
Your answer
Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
Accessing interface in a derived class through the parent class 0 Answers
How to change variables from another script? 1 Answer
Instantiating an object from its interface 3 Answers
How to set an object reference field in the Inspector 0 Answers