Creating instances of object


#1

I’m sorry to keep bothering people with questions. Searching for the information in the documentation is frustrating though and I’m very grateful that people are willing to help me here.

I would like to have my experience randomly spawn several models of ghosts around a play area.

The model is in the hierarchy. I think I instance an object with the Z.Symbol(…) constructor. Anyone done this?

I know I can generate Bezier paths with Z.Bezier. Ideally I would like to have ghost models spawn on a series of preselected paths.

Am I thinking of this the right way?


#2

This is my first experiment, which is meant to spawn a copy of an icon when I point a raycaster at it.

Yeah. Doesn’t work!

var instancenumber = 1;
var instancename: string = “ghost”+instancenumber;

parent.on(“intersectionenter”, (e) => {
// Runs when intersectionenter occurs on the parent node
// The argument e contains useful info about this event:
// https://docs.zap.works/studio/scripting/reference/raycaster/events/intersectionenter/

var instancenew = Z.Symbol(instancename);
instancenew.display();
instancenew.position([0.22,-1,-1.7]);

});


#3

Hi,

Z.Symbol() works with symbols. So, the idea is to create an instance from a symbol (and not from a node already in the hierarchy), then push it to the hierarchy, something like:

var instance = Z.Symbol(ghost_symbol_name);// create an instance from a symbol

then

symbol.nodes.group_parent.push(instance);// add the instance to the hierarchy…

Here, you have a good example from @Mark:

Cheers,


#4

Does what you list as ghost_symbol_name have to be in quotation marks? I have one called 3dshape and it recognizes the three as an actual shape and then says there is no symbol called dshape!

Can you also clarify - the symbol should NOT be in the hierarchy, right?

Can you also clarify, is what you call group_parent the name of the destination node for the new instance? So, root would work? Or the name of the target image?

Thanks so much!


#5

Hi,

1 - Yes the name should be in quotation marks and yes you should avoid starting your symbol’s name with a number. To rename your symbol: duplicate, name the copy and delete the original.

2 - Yes, don’t add the symbol to the hierarchy. The push() function will do the job. :smile:

3 - Yes, group_parent, is just a group to store your instances. Of course, it’s possible to add things directly to the root, but a group helps you keeping your hierarchy neat and tidy. :smile: Big advantage also: you can clear() a group, so you can get rid of your instances when needed…

Cheers,


#6

Thank you so much!