I’m trying to understand the limitations with scripting and plugins. Quite a few plugins I use require TextMeshPro, but it looks like it’s not supported. What other types of plugins should I expect not to work?
I’m trying to do something very simple like start a timer on scene load and show it in a text UI obj, but I couldn’t get it to work.
I have the timer script attached to an obj, and a textTimer UI that is attached to the timer script. I think my problem is it needs to be in Typescript
Hello there – we have an experimental AI bot 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.
You are correct that TextMeshPro is not supported in Needle Engine; plugins that rely on this Unity-specific package won’t work. However, Needle Engine does allow for custom components to be written in Typescript, which are then compiled and available for use within the engine (see the documentation).
For simple UI tasks like starting a timer on scene load and displaying it, you can indeed use a combination of custom scripting and Needle Engine’s UI components. If you found a timerUI component within Needle Engine, it should be possible to use it for your purpose, but it’s essential to ensure that the component is appropriately configured to interact with your script and UI objects.
Please share the code of your timer script, and any configuration details for your textTimer UI object for more specific guidance on how to resolve the issue you’re facing with starting and displaying the timer.
Here is a simplified example code of how you could implement a timer using Needle Engine scripting:
import { Behaviour, Text, serializable } from "@needle-tools/engine"; // Make sure to import the `Text` UI component
export class SimpleTimer extends Behaviour {
@serializable() startTime: number = 0;
private elapsed: number = 0;
// Reference to the Text UI component
@serializable(Text)
private timerText: Text;
start() {
this.startTime = this.context.time.realtimeSinceStartup;
}
update() {
// Calculate elapsed time
this.elapsed = this.context.time.realtimeSinceStartup - this.startTime;
// Update the Text UI component
if (this.timerText) {
this.timerText.text = this.elapsed.toFixed(2); // Or format the time as you wish
}
}
}
Remember to attach your Text UI component to the script in the Needle Editor to properly display the timer.
Good luck with your project! If you continue to face challenges, consider reaching out to the Needle Engine community or support channels for more hands-on assistance.
Here’s the revised code in TypeScript for starting the timer:
import { Behaviour, Text, serializable } from '@needle-tools/engine';
export class TimerScript extends Behaviour {
@serializable(Text)
public timerText: Text | null = null;
private startTime: number = 0;
start(): void {
this.startTime = this.context.time.realtimeSinceStartup; // Use context time as start time
}
update(): void {
this.updateTimerDisplay();
}
private updateTimerDisplay(): void {
let timeElapsed: number = this.context.time.realtimeSinceStartup - this.startTime;
let minutes: string = Math.floor(timeElapsed / 60).toString().padStart(2, '0');
let seconds: string = (timeElapsed % 60).toFixed(2).padStart(5, '0');
if (this.timerText) {
this.timerText.text = minutes + ':' + seconds;
}
}
}
In this version of the script, we’ve used the realtimeSinceStartup property of the context.time to start the timer and update the timer display. Additionally, we made sure to check the existence of the timerText before updating it to avoid potential errors.
After I save my script, I’m expecting to see GeneratorRunning in my console log so the script is available in the Unity Editor, but I don’t. Maybe git is not setup correctly.
Yes, thanks, I ended up restarting my editor and got that squared away, I think now I just need the correct code. Trying the script your bot gave me along with a few others but not having much luck
I can now see the timer running in the console but am getting timerText not initialized in the console. The text UI is still not updating. I am using the multiplayer template if that makes a difference.
I looked through the templates to see if a timer was included in any of them but did not see it. Thanks.
@Marcel_Wiessler1 Finally got it, thanks! If I wanted to pay someone to help speed up the learning curve for this engine, should I post in the job-board here?
In case anyone needs a timer script, this is working for me:
import { Behaviour, Text, serializable } from “@needle-tools/engine”;
export class SimpleTimer extends Behaviour {
@serializable() startTime: number = 0;
private elapsed: number = 0;
// Expose a public field for the Text UI object
@serializable(Text)
public timerText: Text;
start() {
this.startTime = this.context.time.realtimeSinceStartup;
// Optional: Check if the Text UI object has been assigned
if (this.timerText == null) {
console.error("SimpleTimer: No Text UI object assigned.");
return;
}
}
update() {
// Calculate elapsed time
this.elapsed = this.context.time.realtimeSinceStartup - this.startTime;
// Update the Text UI component if it's not null
if (this.timerText != null) {
this.timerText.text = this.elapsed.toFixed(2); // Or format the time as you wish
}
}
Glad you got it to work! If there’s specific topics and questions you’d like to hear about / learn about it’s also worth posting right here, we might be able to make a video about it or at least improve the docs.