Creating Custom Attribute Types

I’m curious if there is a way to create our own custom attribute types to allow us to group together multiple attributes into one single attribute. My goal/hope specifically is to have an array of a custom type that contains a camera entity and scene settings so that I can switch cameras and change the scene lighting depending on the camera that is activated. The best way I’m aware of is having two arrays, one containing the camera entities (for enabling and disabling) and another array for the scene settings and then aligning the index of those to correspond with each other. This works just fine but it would be much cleaner and maintainable if this could be one array containing both the camera entity and settings at the same index. Something along the lines of Views[0].camera Views[0].settings instead of cameras[0] and settings[0]. I am coming from Unity and I know in that, I could either create a serializable type or use a scriptable object to achieve this result. I’m not sure how or if it is possible in PlayCanvas. Any help/insight would be greatly appreciated as this is not the only place where having this functionality available would be the preferred way to go.

Hi @adam5000,

That is a good point. Right now there isn’t any way to do that in the editor, grouping attributes or creating custom ones.

The Playcanvas team has already said that they are actively working in a prefab-like system, so that would be used to group things together. Creating custom/reusable templates in your project.

What I usually do, for the moment, is use “template” (disabled in runtime) entities and settings scripts on them, to define the required attributes. It’s not ideal, since the settings of each entity won’t show up in place, but still it is quite powerful.

1 Like

For this particular scenario, you could create a pc.script that holds the scene settings you need and add it to the camera entity as it looks like you have a 1 to 1 mapping between a camera entity and settings.

2 Likes

I’m currently doing something pretty similar where I’m storing all the “prefab”-like prototype entities in a separate scene which at run time I merge with the game scene on load, index them by their name, and disable the prototype entity. Then I have a global function that I can call to spawn an object based on it’s root name. Although it takes a little more initial overhead than Unity’s prefab system I’ve actually found the flow to be a little easier for me to work with than what Unity has and having a scene where I can view all the “prefabs” side by side is real handy. The only thing that really gets me with this system is that it’s not possible to view anything about run time spawned entities in the inspector which was something I used to use quite a bit for debugging and testing in Unity.

That indeed would be really useful. The launch window has a direct communication link with the editor, that’s how changes are propagated in real time.

I imagine at some point we might be able to have this as an editor feature, have the runtime hierarchy appear in the editor.

1 Like

I think this is probably the way to go and I was just trying to force my idea of how it should function onto PlayCanvas instead of basing the how to do it off of what PlayCanvas can do! That would definitely solve the concern that I have about having to keep two arrays in sync. The project I’m working on has multiple people working remotely on it and I have no doubt that an accidental data modification would occur at some point and all the sudden our cameras and settings would get misaligned with the 2 array, matched by index way. I think your suggestion is probably what I’m going to end up doing.

I suppose the idea of having one camera controller doing the switching is where it gets messy with how PlayCanvas functions and having each camera have a script attached which stores it’s settings, and also be responsible for enabling and disabling itself would be the right way to handle this.
Thank you!

At the moment it’s also not available to make custom types or something like that?

My task is a configuration with nested settings, in general it would suit me if the json attribute supported nesting, but now I do it in such a way that I create an entity and a reference to it in parent json to achieve nesting. Is this best practice now or am I missing something? (Links to assets appear in my settings)