Is there any proper way of writing typescript as OOP like in C#

I have worked only as Unity Developer so I am little confused about the concept of typescript.

What I am trying to do is to define a custom class and make a container of classes that I can access all of the classes that was built.

so in C# it should be look like

public List BoardAreaList= new List();

but seems like in the converting phase (typescript β†’ C#) ,

It is commented out and I have no idea what is wrong, maybe it is not able to do such things or is it a grammer problem?
image.png

Original Post on Discord

by user 151660356224286721

Sorry - I can’t help with image.png
Only text files are supported

You can ping the bot with your c# class and get help for translating :slightly_smiling_face: highly recommend you read our scripting docs tho. Its almost the same as in unity and C#

What you try definitely works (is possible). Cant write much code right now but this should hopefully help you.

@Needle Engine Assistant
Can you make an example of a c# list field in a needle engine typescript component. Also generate the BoardArea type with a string field

import { Behaviour, serializable } from '@needle-tools/engine';
import { Object3D } from 'three';

// Define the BoardArea type in TypeScript with a string field
export class BoardArea {
    @serializable() // No type specifier needed for primitive types
    public name: string;

    constructor(name: string) {
        this.name = name;
    }
}

// Define a component with a list of BoardArea instances
export class MyComponent extends Behaviour {
    @serializable(BoardArea) // Specify the type for deserialization
    public boardAreas: BoardArea[] = [];

    // Lifecycle methods and other component logic would follow
}

This code defines a TypeScript BoardArea class with a name field and a MyComponent component with a list (Array) of BoardArea instances. The @serializable decorator is used to ensure the BoardArea instances in the list can be properly serialized and deserialized by Needle Engine.

4 messages left GPT 4
thread_aGeH0ZvIqFqNzLwXnoSoO4lM
Thanks for your feedback!

^ the above is almost correct except you can omit the constructor in BoardArea if it is exported from Unity

thanks for the reply marcel i’ll try that out !

by user 151660356224286721

@Needle Engine Assistant
import { Behaviour, serializable } from β€œ@needle-tools/engine”;
import { Object3D } from β€œthree”;

export class BoardArea
{
@serializable(Object3D)
public Areatransform!: Object3D;
@serializable()
public AreaName!: string;

constructor(transform: Object3D, name: string)
{
this.Areatransform = transform;
this.AreaName = name;
}

}

export class MovementManager extends Behaviour
{
public boardTransforms!: Object3D;

@serializable(BoardArea)
public BoardAreaList: BoardArea=;

//List of classes where AreaTransform and AreaName is stored
// public BoardAreaList!: BoardArea;

columnAlphabet: string=[β€œA”,β€œB”,β€œC”,β€œD”,β€œE”,β€œF”,β€œG”,β€œH”];

awake()
{
console.log(β€œk11”);
this.GenerateArea;
console.log(this.BoardAreaList.length);
}

GenerateArea()
{
for(let i=0; i<8; i++)
{
for(let j=0; j<8; j++)
{
var index = i*8+j;
var transform = this.boardTransforms[index];
var name = this.columnAlphabet[i];
var number = j+1;
name = name.concat(number.toString());
var newArea = new BoardArea(transform,name);
this.BoardAreaList.push(newArea);
}
}
}

}
why is this commented out in converted c# script like this below.

namespace Needle.Typescript.GeneratedComponents
{
public partial class MovementManager : UnityEngine.MonoBehaviour
{
public UnityEngine.GameObject @boardTransforms;
// public @BoardAreaList = new { };
public string @columnAlphabet = new string{ β€œA”, β€œB”, β€œC”, β€œD”, β€œE”, β€œF”, β€œG”, β€œH” };
public void awake(){}
public void GenerateArea(){}
}
}

by user 151660356224286721

Here is what I found for you :brain: Discord

The provided TypeScript code defines a MovementManager component class with a nested BoardArea class in the context of the Needle Engine, which relies on TypeScript decorators such as @serializable for serialization of component properties. The Needle Engine code is designed to be used in a JavaScript/TypeScript environment. When the code is converted to C# for use in Unity, certain features may not directly translate between the two languages.

In the TypeScript code, BoardArea is defined with serializable properties, and instances of BoardArea are stored in an array BoardAreaList within MovementManager. However, in the generated C# Unity code, the equivalent list BoardAreaList is commented out.

The likely reason for the commented-out BoardAreaList in the generated C# code could be that the Needle Engine’s TypeScript to C# conversion tool might not directly support custom classes with serialization decorators like it does for built-in types. Unity’s serialization system has different requirements and limitations compared to the serialization used in Needle Engine for the web.

To resolve the issue, you would typically need to adapt the C# code manually to ensure it correctly represents the data structures and logic intended in the original TypeScript code. You might define a [Serializable] attribute for a class equivalent to BoardArea in C# and make sure that it can be serialized within a MonoBehaviour. However, this code requires careful manual translation, and I can help guide you through that process or clarify any specific technical questions you have regarding Needle Engine or Unity serialization.

5 messages left GPT 4
thread_VeqNc2yyqa9grxFWEaiLWXXi

@Needle Engine Assistant so you mean that I can write Backend structure in c#?

by user 151660356224286721

500 The server had an error while processing your request. Sorry about that!

4 messages left GPT 4
151660356224286721@1179139880685682808-2

@Needle Engine Assistant
I am not sure what you mean by that.

show me an example doing this paragrpaphs specifically in steps.

To resolve the issue, you would typically need to adapt the C# code manually to ensure it correctly represents the data structures and logic intended in the original TypeScript code. You might define a [Serializable] attribute for a class equivalent to BoardArea in C# and make sure that it can be serialized within a MonoBehaviour. However, this code requires careful manual translation, and I can help guide you through that process or clarify any specific technical questions you have regarding Needle Engine or Unity serialization.

by user 151660356224286721

500 The server had an error while processing your request. Sorry about that!

3 messages left GPT 3.5
151660356224286721@1179139880685682808-2

@Needle Engine Assistant
The provided TypeScript code defines a MovementManager component class with a nested BoardArea class in the context of the Needle Engine, which relies on TypeScript decorators such as @serializable for serialization of component properties. The Needle Engine code is designed to be used in a JavaScript/TypeScript environment. When the code is converted to C# for use in Unity, certain features may not directly translate between the two languages.

In the TypeScript code, BoardArea is defined with serializable properties, and instances of BoardArea are stored in an array BoardAreaList within MovementManager. However, in the generated C# Unity code, the equivalent list BoardAreaList is commented out.

The likely reason for the commented-out BoardAreaList in the generated C# code could be that the Needle Engine’s TypeScript to C# conversion tool might not directly support custom classes with serialization decorators like it does for built-in types. Unity’s serialization system has different requirements and limitations compared to the serialization used in Needle Engine for the web.

To resolve the issue, you would typically need to adapt the C# code manually to ensure it correctly represents the data structures and logic intended in the original TypeScript code. You might define a [Serializable] attribute for a class equivalent to BoardArea in C# and make sure that it can be serialized within a MonoBehaviour. However, this code requires careful manual translation, and I can help guide you through that process or clarify any specific technical questions you have regarding Needle Engine or Unity serialization.

I am not sure what you mean by that.

show me an example doing last paragraph specifically in steps.

by user 151660356224286721

500 The server had an error while processing your request. Sorry about that!

2 messages left GPT 3.5
151660356224286721@1179139880685682808-2

Can you make an example of the last paragraph?

500 The server had an error while processing your request. Sorry about that!

4 messages left GPT 4
272528485732450305@1179139880685682808-2