Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by aviose · Apr 16, 2012 at 06:41 AM · javascriptinheritanceclassesinherited-membersjavascript class

Using functions of Inherited classes

I ended up temporarily changing the way I had my stuff coded since I'm on a deadline at school, but for future reference, I'm having issues with classes extended from other classes. This is easier to show than it is to explain:

I declare my class and subclass:

 class Base {
   function DoSomething() {
     //does something
   }
 }
 class Sub extends Base {
   function DoSomething(with : parameter) {
     //Does something using parameters
   }
 }
 class Sub2 extends Base {
   function DoSomething(with : parameter) {
     //Does something slightly different with parameters
   }
 }

Later, while trying to use this code, I need to reference either one of these two classes in the same variable (for example, both are CharacterClass, but one is NPC and the other is PC, and one variable is used for both versions of Base). It won't let me use the derived version of DoSomething, and returns that I can't call it with parameters (as none are set in Base's constructor, even though it is in each of its children.

 var subGuy : Sub = new Sub();
 var sub2Guy : Sub2 = new Sub2();
 var myBase : Base;
 if (something) {
   myBase = subGuy;
 } else {
   myBase = sub2Guy;
 }
 myBase.DoSomething(myParam);

The above throws an error, and since the same command is used in both instances, I can't simply use ( myBase as Sub ).DoSomething(myParam); As I never don't intrinsically know which of the two versions I have (and it returns a Base, not a Sub or Sub2 version of the myBase).

Is there any way to access the inheritor's function to perform the task?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image syclamoth · Apr 16, 2012 at 06:59 AM 0
Share

You need to change the base one so that it has the same paramaters. If you were using C# I would be able to help you more, but I'm afraid I'm not too familiar with how UnityScript handles inheritance.

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by zz74b · Apr 16, 2012 at 01:05 PM

As explained by senad, you are not overriding the original class functions.

What I would suggest you do when using inheritance, use the virtual keyword. Then your function will look something like this:

 public virtual void MyFunction(){ ... }

Then in your derived class (in your example, "Sub") you use the override keyword. This will ensure that you really have the correct signature.

i.e.

 public override void MyFunction(){ ... }

If you want to pass different parameters then you lose the static typing of them unfortunately. You could do it like:

 public virtual void MyFunction(params object[] parameters){ .... }

or you could pass an object as the parameter, which is itself a class:

 public virtual void MyFunction(object parameterObject){ ... }

however in this case you'll need to cast the parameterObject to the correct class type in your derived (overriding) class.

The final way might be using reflection, but in all honesty, I would suggest trying to stay within the realms of normal inheritance.

PS: The above is all C# code. You'll have to work out the translation to JS.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aviose · Apr 16, 2012 at 02:31 PM 0
Share

I wouldn't know how to use the virtual function, but thank you for answering. It was hard enough just trying to find information to properly learn class inheritance in JS/Unityscript. $$anonymous$$ost lean towards C# at that point, but I'm not supposed to use C# in this class.

avatar image
1

Answer by senad · Apr 16, 2012 at 09:25 AM

"DoSomething()" and "DoSomething(with : parameter)" are not the same method, as the paramter list is part of the signature. So one can not override the other.

Also "DoSomething(with : parameter)" is not defined in your base class, only the other one.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aviose · Apr 16, 2012 at 02:29 PM 0
Share

I didn't think to set up a second override for the method with parameters in the base. Still new to coding with this many classes, so small intricacies are still a pain in the butt to me. Thanks. I've only been using class functions like this for about a month or so, as only my game design courses even use object oriented program$$anonymous$$g, and our instructor isn't going over custom classes in class this semester.

avatar image
1

Answer by Bunny83 · Apr 16, 2012 at 03:05 PM

The point of inheritance is that you have to use virtual functions so they can be overridden in a sub class. AFAIK it works like this in UnityScript:

 class Base {
     virtual function DoSomething(parameter : int) {
         Debug.Log("Base " + parameter);
     }
 }
 
 class Sub extends Base {
     virtual function DoSomething(parameter : int) {
         Debug.Log("Sub " + (parameter * parameter));
         // to call the overridden function from base:
         super.DoSomething(parameter);
     }
 }

As far as i know there's no "override" keyword in UnityScript. It automatically overrides the function as long as it's virtual.

 var c : Base;
 c = new Sub();
 c.DoSomething(5);

This would print:

 "Sub 25"
 "Base 5"    // because i call the base (super) version

Like others have already mentioned the signature of a function is defined by it's name and it's parameters. Otherwise it's not the same function. If you have multiple functions with the same name but different parameters it's called an overloaded function since it exists multiple times. The compiler picks the correct version according to the parameters you pass in.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aviose · Apr 16, 2012 at 05:21 PM 0
Share

I understood the overloaded function aspect, save that I was still thinking of them as the same function, just with multiple ways to interpret it. Since I didn't think of them as completely separate functions, though, I didn't think about trying to set up a proper parameter version. I also didn't know about the virtual command, and just allowed Unityscript to automatically read both functions. Using this will help me, I'm sure.

Thanks to all 3 of you for answering, and doing so quickly. I'd give you all plusses if I could, but I only started the account last night so I could send an answer to someone else's question I saw while I was looking for the answer to my own question.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Inheriting from classes from other files (Javascript) 1 Answer

How to store a javascript in a variable in the editor then call a function from it? 2 Answers

Defining and inheriting from a Javascript Class 0 Answers

How do I write this C# snipet in java script? 1 Answer

Difficulty instantiating to a custom class 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges