Can an abstract class extends from Behavior and be visible in Unity Editor?

Hi :), hope you doing well. I have some classes which share some behaviors but each one has its own specific actions. I was thinking of script a abstract class (that extends Behavior) and the other classes extends this abstrac class. But when I look for those classes i “Add Component” button, neither of them are available. Do you know what could be happening ?
This is my example, first the abstract parent class and then the child class:

export abstract class NetworkingReceiver extends Behaviour{

    @serializable()
    channel !: string;

    @serializable()
    justVR !: boolean; 

    protected connection !: NetworkConnection; 
    protected booleanReceived : boolean = false; 
    
    start(): void {
        this.setBeginListeners();
        this.setStopListeners();
        this.receiverSetUp(); 
    }

    setBeginListeners():void{
        if(this.connection){
            this.connection.beginListen(this.channel, (networking_data)=>{
                this.booleanReceived = networking_data.booleanSent;
                if (this.justVR) {
                    if(this.context.isInVR){
                        this.receiverBehavior();
                    }
                }else{
                    this.receiverBehavior(); 
                }
            });
        }
    }

    setStopListeners():void{
        if(this.connection){
            this.connection.beginListen(this.channel, (networking_data)=>{
                this.booleanReceived = networking_data.booleanSent;
            });
        }
    }
    
    //Specific Behavior for the Receiver 
    abstract receiverBehavior():void; 

    //Specific Initialization for the Receiver 
    abstract receiverSetUp():void; 

}

Child class :

export class ReceiverActivation extends NetworkingReceiver{


    receiverBehavior(): void {
        setActive(this.gameObject,this.booleanReceived);
    }

    receiverSetUp(): void {
        setActive(this.gameObject, this.booleanReceived);
    }
}

Original Post on Discord

by user 632418299711324161

Hello there – we have an experimental AI bot :robot: that might be able to help you with your question. Would you like to try this out?

How it works:
Reply to the messages of the bot that end with a thread_id to interact with it and ask follow-up questions. You can also mention the bot directly in this channel to get help.

Help requested: please wait a moment…

Here is what I found for you :brain: Discord

It sounds like you’ve correctly identified abstract classes and subclassing as a way to create shared behavior in your Needle Engine components. However, abstract classes and the components that extend them are not directly usable in the editor until you create concrete subclasses.

The behavior you’re describing where your classes are not available in the “Add Component” button could be due to a few different reasons:

  1. Abstract Classes: The Needle Engine editor (and Unity’s, by the way) do not expose abstract classes as components to be added because they cannot be instantiated. You need to have concrete (non-abstract) subclasses of Behaviour which do not have any abstract methods.

  2. Export Statements: Make sure to use explicit export statements for your concrete classes, because they need to be properly registered as components that can be added through the editor.

Consider checking that your concrete subclasses (like ReceiverActivation) are properly defined, exported, and are not abstract themselves. Here is an example of a concrete class based on your NetworkingReceiver class:

import { Behaviour, serializable, NetworkConnection, setActive } from "@needle-tools/engine"; // Assuming setActive is a function from Needle Engine

export class ReceiverActivation extends NetworkingReceiver {
    receiverBehavior(): void {
        setActive(this.gameObject, this.booleanReceived);
    }

    receiverSetUp(): void {
        setActive(this.gameObject, this.booleanReceived);
    }
}

thread_GmO6xLIjLOy5y0Jpj6avVJed

Make sure to also check the following:

  • Verify that setActive is a correctly imported function from the engine that you intended to use.
  • Ensure all necessary components and their respective namespaces are properly imported.
  • Check that ReceiverActivation is not abstract and does not have any unimplemented abstract methods from its parent class.
  • Ensure there’s no TypeScript error preventing proper compilation.

If everything looks correct but the issue persists, you might want to consult the Needle Engine documentation or reach out for support directly within your development community, as they might have more context-specific insight.

1 messages left GPT 4
thread_GmO6xLIjLOy5y0Jpj6avVJed
Thanks for your feedback!

I want that, the subclass which extends the abstract class appears in the Unity Editor, the abstract class already extends Behavior from Needle

by user 632418299711324161

Here is what I found for you :brain: Discord

Got it, DiegoEloko. To make sure that your subclass appears in the Unity Editor, you need to ensure that it is properly annotated for serialization and marked as a Needle Engine component. Additionally, it’s necessary to have the export statement for the subclass.

Here’s an example of how you can modify your child class to ensure it appears in the Unity Editor:

import { serializable, Behaviour, NetworkConnection, setActive } from "@needle-tools/engine";

@serializable
export class ReceiverActivation extends NetworkingReceiver {
    @serializable
    channel: string = '';
    
    @serializable
    justVR: boolean = false;

    connection!: NetworkConnection;
    
    booleanReceived: boolean = false;

    start(): void {
        this.setBeginListeners();
        this.setStopListeners();
        this.receiverSetUp();
    }

    setBeginListeners():void {
        if(this.connection) {
            this.connection.beginListen(this.channel, (networking_data)=>{
                this.booleanReceived = networking_data.booleanSent;
                if (this.justVR) {
                    if(this.context.isInVR) {
                        this.receiverBehavior();
                    }
                } else {
                    this.receiverBehavior(); 
                }
            });
        }
    }

    setStopListeners():void {
        if(this.connection) {
            this.connection.beginListen(this.channel, (networking_data)=>{
                this.booleanReceived = networking_data.booleanSent;
            });
        }
    }

    receiverBehavior(): void {
        setActive(this.gameObject, this.booleanReceived);
    }

    receiverSetUp(): void {
        setActive(this.gameObject, this.booleanReceived);
    }
}

thread_GmO6xLIjLOy5y0Jpj6avVJed

In this example, I’ve added @serializable annotations to the properties of ReceiverActivation and removed the abstract declarations. This ensures that the subclass will appear as a component in the Unity Editor.

Remember that you’ll need to make sure that this code is part of a TypeScript file within your Needle Engine project and is compiled properly to be available in the Unity Editor. If you’ve already done these steps and the subclass still doesn’t appear, consulting the Needle Engine documentation or reaching out to the support community for further insights is advisable.

0 messages left GPT 3.5
thread_GmO6xLIjLOy5y0Jpj6avVJed

I want that the abstrac class remains abrstrac but the subclass appears in the editor

by user 632418299711324161

You have reached your limit of 2 messages… please try again in a few hours

Try annotating the class type with //@type Namespace.Typename (directly above the class name)

Okey, let me check, thanks marcel : )

by user 632418299711324161

I wrote this and didn´t work :

@type Namespace.Typename 
export class ReceiverActivation extends NetworkingReceiver{

    //Implementation of abstract method
    receiverBehavior(): void {
        setActive(this.gameObject,this.booleanReceived);
    }
    //Implementation of abstract method
    receiverSetUp(): void {
        setActive(this.gameObject, this.booleanReceived);
    }
}

by user 632418299711324161

Please replace Namespace and Typename with your namespace and typename. Make sure it’s a comment as i said above

Could you tell whats the namespace and the typename ?

by user 632418299711324161

Probably //@type Needle.Typescript.GeneratedComponents.NetworkingReceiver

That works, thank you very much marcel : )

by user 632418299711324161