Game Object Constructor

Hello folks!

After a long break, I am returning to learning Babylon. I have a crazy idea for an audio game. I’m thinking of something for the kids. To show them that exercising imagination and calming down can be very appealing.
This project is something like guided meditation, but it is the player who decides which direction to go. It is not a simple recording of music and speech. And it’s more like entertainment. Such a wolf in sheep’s clothing :wink:

I don’t know about kids in other countries, but in Poland, remote teaching is a great failure. I have two teenagers and I can also see how much electronics involve them. It is a type of drug. Therefore, I thought that I would develop my programming skills and prepare something to test.

And now the specifics, I will not bore you with my plans anymore :slight_smile:

I prepared the scene and player controls but got stuck making the game locations.
This is my Room constructor. It will have even more parameters.

class Room {
constructor(name, width, height, depth, posX, posZ, material, descNear, descFar, bgMusic, bgMusTransition, footSteps) {
this.name = name;
this.width = width;
this.height = height;
this.depth = depth;
this.posX = posX;
this.posZ = posZ;
this.material = material;
this.descNear = descNear;
this.descFar = descFar;
this.bgMusic = bgMusic;
this.bgMusTransition = bgMusTransition;
this.footSteps = footSteps;

return BABYLON.MeshBuilder.CreateBox(name, {width: width, height: height, depth: depth});
}

}

I’m creating an instance:
let room1 = new Room(“The first place”, 2, 2, 7, 134, 236, colorGreen);

and then I have to assign color and positions separately:
room1.material = colorGreen;
room1.position.x += 134;
room1.position.z += 236;

I do not know how to write it in the constructor so the new object immediately contains the right material and the X and Z position.
So that there would be no need to write this additional code:
room1.material = colorGreen;
e.t.c.

Can someone guide me how to do it?
Thanks for any hint!

Hi @Bociakrodyl
like this? :slight_smile:

class Room {
  constructor(name, width, height, depth, posX, posZ, material, descNear, descFar, bgMusic, bgMusTransition, footSteps) {
	this.name = name;
	this.width = width;
	this.height = height;
	this.depth = depth;
	this.posX = posX;
	this.posZ = posZ;
	this.material = material;
	this.descNear = descNear;
	this.descFar = descFar;
	this.bgMusic = bgMusic;
	this.bgMusTransition = bgMusTransition;
	this.footSteps = footSteps;
	
	this.mesh = BABYLON.MeshBuilder.CreateBox(name, {width: width, height: height, depth: depth});
	this.mesh.material = material;
	//this.mesh.position.x..
	//this.mesh.position.z..
	return this.mesh;
  }
}

and if you don’t want a reference in “this.prop”

class Room {
  constructor(name, width, height, depth, posX, posZ, material, descNear, descFar, bgMusic, bgMusTransition, footSteps) {
	this.name = name;
	this.width = width;
	this.height = height;
	this.depth = depth;
	this.posX = posX;
	this.posZ = posZ;
	this.material = material;
	this.descNear = descNear;
	this.descFar = descFar;
	this.bgMusic = bgMusic;
	this.bgMusTransition = bgMusTransition;
	this.footSteps = footSteps;
	
	let mesh = BABYLON.MeshBuilder.CreateBox(name, {width: width, height: height, depth: depth});
	mesh.material = material;
	mesh.position.x..
	mesh.position.z..
	return mesh;
  }
}
1 Like

Works beautifully!
Thank you for explaining with an example,
and for the cosmic speed of response! :smiley:
Now I’m going to stick next pieces of my audio engine.

I will continue in this thread because I have a puzzle about reaching for an object property.

I created the object based on the class from the first post in this thread:
let room1 = new Room(“The first place”, 4, 1, 2, 0, 0, colorBlue, “Description 1”, “Description 2”, “firstSong”, 2, “pcv”);

I found two methods of reaching for the mesh:
scene.getMeshByName(“meshName”)
scene.getNodeByName(“meshName”)

But all I can get out of it is the mesh name:
let myObj = scene.getNodeByName(“The first place”);
console.log(myObj.name);

Unfortunately, I can’t get the other attributes of the objects that this mesh belongs to. So it doesn’t work:
myObj.descNear

Is there any method to reach for the object to which the mesh belongs?

I suppose that in my dreams I was thinking sharply about this problem :wink:
Because this is the basis for further work, i.e. extracting various properties of the selected location in the game.

And I had the idea of using Map().

let objectsList = new Map(); // holds every game locations

When I create an object, I also add it to the map:
let room1 = new Room(“room1”, 4, 1, 2, 0, 0, colorBlue, “Description 1”, “Description 2”, “firstSong”, 2, “pcv”);
objectsList.set(“room1”, room1);

When I click, I get a mesh name:
let objName = bjsevt.source.name;

So I can pull the object from the map in that way:
let myObj = objectsList.get(bjsevt.source.name);

Now I have its property!
console.log(myObj.descNear);

Jupi!
:slight_smile: