Creating instances of object

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?

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]);

});

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,

1 Like

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!

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,

2 Likes

Thank you so much!

2 Likes

Just a follow up. This was really useful. Thanks again.

1 Like

Hi, very useful stuff here. Everything works, but… I was trying to do the same but putting the new Z.Symbol() instance in a local var, instead of pushing in an array. But doing so one cannot access the “nodes” property, so I got lost trying to casting to subtypes (I am using a simple button with text symbol and wanted to access and change the text…) etc etc. So, I got back here, I did EXACTLY the same as you by pushing stuff in a local array instead of using a local var, and it works… I can see the “nodes” property.

I am not an experienced TypeScript/JavaScript developer, my main language is C++. What am I missing here?? :slight_smile:

Thanks in advance!
Aaron

Hi Aaron, not sure where your problem is… :thinking:
Just clarifying some points:

  • in the example code above, there is no array. The push() function is the one to add element to the main hierarchy.
  • if you want to make some changes in the symbol’s hierarchy, then your variable should be global.

So, I should say:

  • Declare a global variable
  • Use the variable to instantiate a symbol (your button)
  • push this instance in the main hierarchy
  • use the variable to access the instance hierarchy

Hope it will help :grin:
Jean.