Unsure of how to convert c# public class to javascript
I am working on a tutorial series and recoding everything in Javascript from C#. I am only doing this as the tutorial is perfect for me but not in Javascript. I know enough C# to read some of the lines of code, but not enough to write it from scratch. This particular part makes objects "Living Entities" and allows them to take damage if hit by a projectile.
I have a script called IDamageable.js and so far it only contains the following:
function TakeHit(damage:float,hit:RaycastHit){
}
In C# a script called LivingEntity.cs uses the following lines to somehow connect the IDamageable part:
public class LivingEntity : MonoBehaviour, IDamageable {
}
I am unsure of what the comma does. Like the IDamageable part in that line. So I am unable to ask the question directly of how to implement an [Extend? Value? Whatever?] in Javascript. So my question is, what is the IDamageable parameter called.. and how do I do the same thing in Javascript. My apologies in advance, I think my question could be a lot more straight forward if I knew the terminology of the position IDamageable occupies in the C# code.
The the IDamageable part an interface? It looks similar to other interfaces when I kept searching around. If someone can identify this I am sure I could find the resources I need to progress already on the internet.
It means it is inheriting from the IDamageable class. Variables and functions in that class are also accessible to LivingEntity.
This should show you the equivalent code in JS.
Answer by taxvi · Mar 28, 2016 at 07:55 AM
It's common practice in coding to name interfaces ISomething
so most probably IDamageable
is an interface (look up the difference between class and interface if you don't know already). Any class can have only 1 parent class (`MonoBehavioiur` in your case) but it can also implement infinite number of interfaces.
Interface is a collection of method headers without body. When implementing an interface in a class you have to write body for all the methods (or it will not compile). Using interface is good for casting different objects in same method, or iterating through them.
For example, in your case you may have an explosion in your scene so you'd write something like:
foreach(IDamagable d in ExplosionZone){
d.TakeHit(10f);
}
So in this way you can take any class implementing IDamageable
like Tiger
and DeathStar
and cast them in this loop and they will both take hit even though they don't have a common parent class.
So, for now, in your LivingEntity
class you have to implement all the methods that are declared in IDamageable
to let your stuff compile.
Thank you. This is enough for me to find out how to achieve this in JavaScript.
Your answer
Follow this Question
Related Questions
Reference Vector 3 variable to float variable. 2 Answers
Heightmap From Texture - Script Converter 2 Answers
Copy and Pasted code for Trajectory and got one parsing error... 1 Answer
Can't instantiate new class C# 0 Answers
List of Class With GameObject 0 Answers