Hello everyone, I’m trying to create a game using Needles. I’m attempting to make a game in which I need to create something similar to a Scriptable Object. I’ve searched the Needles documentation, but I can’t seem to find the answer. If anyone could help, I would greatly appreciate it. Or if there’s a way to achieve this, I would like to know.
The game is essentially like the mini-games where you receive a recipe and have to cook it. I’ve already managed to make the player move and interact to some extent. The problem arises when I try to create an object. I need the player to be able to create the object, pick it up, place it on a table, and pick it up again. In this case, I believe I need to change the object’s parent, and I think using a Scriptable Object could be a solution.
In the Needles documentation, I’m using the following code to create the object: @serializable(AssetReference)
myScenes?: AssetReference;
async awake() {
if (!this.myScenes) {
return;
}
for (const scene of this.myScenes) {
// check if it is assigned in unity
if(!scene) continue;
// load the scene once
const myScene = await scene.loadAssetAsync();
// add it to the threejs scene
this.gameObject.add(myScene);
// of course you can always just load one at a time
// and remove it from the scene when you want
// myScene.removeFromParent();
// this is the same as scene.asset.removeFromParent()
}
}
onDestroy(): void {
if (!this.myScenes) return;
for (const scene of this.myScenes) {
scene?.unload();
}
}
public loadScene(ingredientsName: string) {
// Check if the list of scenes has been configured
console.log(ingredientsName);
if (this.ingredients === undefined) {
console.error("The list of foods has not been configured.");
return;
} else {
}
// Get the AssetReference for the scene by its name
const scenePrefab = this.getSceneAsset(ingredientsName);
if (scenePrefab) {
// Load the scene using the AssetReference
this.load(scenePrefab);
} else {
console.error(`No scene found with the name: ${ ingredientsName}`);
}
}
private async load(scenePrefab: AssetReference) {
const scene = await scenePrefab.loadAssetAsync();
if (scene) {
if (this.playerTopPoint) {
this.playerTopPoint.add(scene); // Add the loaded scene to the scene root
} else {
this.gameObject.add(scene); // Add the loaded scene to the GameObject itself
}
this.loadedingredients.push(scenePrefab); // Add the loaded scene to the array
} else {
if(!this.hasFood) {
this.loadSceneInTable(this.ingredientsFilter);
console.log("se est ejecutndo esta funcion ")
}
}
}
/**
Gets the AssetReference of a scene by its name.
@param value - The name of the scene.
@returns The AssetReference of the scene or undefined if not found.
*/
private getSceneAsset(value: string): AssetReference | undefined {
// Construct the full URI
const sceneUri = assets/${value}.glb;
// Find the AssetReference of the scene by name
const sceneAssetReference = this.ingredients?.find(prefab => prefab.uri === sceneUri);
// Return the scene asset reference (or undefined if not found)
return sceneAssetReference;
}
This is my code, and actually, the object is being instantiated. What I need is to control or figure out how to change the object’s position. Initially, the player interacts and picks up the item, let’s say a tomato. Then, if they interact again, they should be able to place it on a table, and if they want to interact again, they can pick it up. I mention Scriptable Objects because I see it being done this way in a YouTube video I’m watching. I apologize for the question; I’m really new to this, but I’m eager to learn.
I see, first things first.
You can format your code if you write short snippets
```ts
YOUR_CODE
```
will result in
YOUR_CODE
it will be correctly formatted and highlighted
Alternative way is to drop the whole .ts file here, which works great as well and makes the code readable right here in discord.
public loadScene(ingredientsName: string) {
// Check if the list of scenes has been configured
console.log(ingredientsName);
if (this.ingredients === undefined) {
console.error("The list of foods has not been configured.");
return;
} else {
}
// Get the AssetReference for the scene by its name
const scenePrefab = this.getSceneAsset(ingredientsName);
if (scenePrefab) {
// Load the scene using the AssetReference
this.load(scenePrefab);
} else {
console.error(No scene found with the name: ${ ingredientsName});
}
}
private async load(scenePrefab: AssetReference) {
const scene = await scenePrefab.loadAssetAsync();
if (scene) {
if (this.playerTopPoint) {
this.playerTopPoint.add(scene); // Add the loaded scene to the scene root
} else {
this.gameObject.add(scene); // Add the loaded scene to the GameObject itself
}
this.loadedingredients.push(scenePrefab); // Add the loaded scene to the array
} else {
if(!this.hasFood) {
this.loadSceneInTable(this.ingredientsFilter);
console.log("se est ejecutndo esta funcion ")
}
}
}
/**
Gets the AssetReference of a scene by its name.
@param value - The name of the scene.
@returns The AssetReference of the scene or undefined if not found.
*/
private getSceneAsset(value: string): AssetReference | undefined {
// Construct the full URI
const sceneUri = assets/${value}.glb;
// Find the AssetReference of the scene by name
const sceneAssetReference = this.ingredients?.find(prefab => prefab.uri === sceneUri);
// Return the scene asset reference (or undefined if not found)
return sceneAssetReference;
}
Exactly, currently, the ingredient spawns on the table, and I need to be able to change that spawn to be on the player and be able to put it back on the table. So, I think I’m at the point 2: How to reparent it?
Okay, first of all, I’m really new to this. I’m trying to learn on my own. I know very little about Unity, and I have some knowledge of Three.js and TypeScript. Since there aren’t really any Needles tutorials on YouTube, I’ve had to learn on my own. The input I have is that when the player collides with a counter and presses the E key, the interaction occurs. I think that would be my input.
Those methods are purely visual to make it possible to see when the player makes contact with the counter, allowing differentiation by simply changing the color.
Initially, I would like it to be just one. The issue is that later in the gameplay, the player can slice the tomato, which would be another visual like “slice Tomato,” for example. But what I really want to do now is visually allow the player to pick it up and place it on the counter.