[SOLVED] A few questions from a beginner

Hi all, I am new to PlayCanvas and javascript. I played a bit arround today but I have a few questions now.
I coudn’t find the answers on the internet, but sorry if they already answered.

  1. Is there a Prefab/Template option available like in Unity?
    Yes, I saw this (http://answers.playcanvas.com/questions/86/is-there-a-prefabs-mecanism) but that is from 2013 and the answer said that PlayCanvas was working on it. So is this option available now?

  2. Is it possible to create a script that runs in the editor?
    So, that I can also make some tools that can be usefull for the game I am building?

  3. Is there a good way to create a Singleton in the project?

  4. I saw something strange happening with the collider. A create a cube and gave it a scale of 8. (cube is 1 meter, so it is 8 by 8 meters). The collider I gave a size of 8 by 8 meters. But the collider was twise as big as the cube. Why is this happening?

Thank you!

  1. Sorry, we don’t at the moment. The closest we have to that feature at the moment is the entity.clone() function. In a script, I would have an entity reference attribute that I would clone from.
  2. Sorry, not at the moment but it is something we are looking into.
  3. You can have global variables or you can scope them to be within objects. E.g
var Sandbox = pc.createScript('sandbox');

// initialize code called once per entity
Sandbox.prototype.initialize = function() {
    // Now Sandbox.singleton is accessible in any code execution pass this point
    Sandbox.singleton = this;
};

Or if you need global functions, I tend to add an ‘extensions.js’ script that is first in the script execution order in settings and looks like this:

(function(){
    pc.util = {};
    
    pc.util.DEBUG = true;
    pc.util.DEFAULT_VEC3_1 = new pc.Vec3(1, 1, 1);
    
    pc.util.setEntityAlpha = function(entity, alpha) {
        if (entity.model) {
            var a = pc.math.clamp(alpha, 0, 1);
            var meshInstances = entity.model.meshInstances;
            for(var i = 0; i < meshInstances.length; ++i) {
                // WARNING: setParameter() is still a beta feature and may change in the future      
                // This is where we set how transparent we want the object to be on a value between 0 and 1
                // 0 = fully transparent
                // 1 = fully opaque

                // Note: The materials on the model MUST have alpha set on opacity -> blend type and be slight less than 1
                meshInstances[i].setParameter("material_opacity", a);
            }  
        }
    };
    
    pc.util.setEntityEmissive = function(entity, color) {
        var meshInstances = entity.model.meshInstances;    
        for(var i = 0; i < meshInstances.length; ++i) {
            // WARNING: setParameter() is still a beta feature and may change in the future
            meshInstances[i].setParameter("material_emissive", color.data3);
        }
    };
    
    pc.util.isMobile = function() {
        return /Android/i.test(navigator.userAgent) ||
            /iPhone|iPad|iPod/i.test(navigator.userAgent);
    };
})();
  1. The collision is done in ‘half extents’ so the values there should be 4, not 8.
1 Like

It is unfortunately that playCanvas doesn’t support Prefabs. I hope this will be available soon. The reason that I wanted to use prefabs is because when I want to chage a entity (like a brick). I need to change all the entities in the scene one by one. (all the bricks)

Annyway, I will try to thing about a solution. Thanks for the reply Steven!

You can easily get around that by using a single “prefab” entity and several cloned/placeholder entities that are positioned, rotated and scaled accordingly inside the scene. Writing a simple script that on runtime clones the “prefab” entity to replace all placeholder entities.

All changes to the prefab entity will be reflected all around the scene. True that it won’t be correct inside the editor (editor scripts would indeed be awesome) but it can work in many cases.

You can even expose several attributes (properties) inside the editor for each entity that overrides the default settings/behaviour of the “prefab” entity.

I like the “prefab” system of Unity but to be honest given the flexibility of PlayCanvas editor/scripting I rarely feel I am missing that concept.

1 Like

On a side note, you can also multi-select entities and modify them all at the same time for most cases such as adding/removing scripts/components, changing attributes on components, adding/removing tags etc.

@Leonidas Yeah that woud work. I will try to make some sort of clone/replace script.Thanks for the tip!

@steven This is also useful to know. Thanks!

I’m also curious about 1 and 2.

Any progress on those?

They don’t have those features still. I’m not sure if they’re implementing those anytime soon.

We’re in the middle of implementing support for templates (AKA prefabs). Good progress made - still lots to do.

We’re in the design stage for Editor plugins.

2 Likes

Awesome! When do you think you guys will finish? Such features could help a lot.

Templates (AKA prefabs) should be done within a couple of months, I reckon. I can’t give an ETA on Editor plugins since that’s super early in development.

Okay. Templates or Prefabs will be useful for the game that I am making.

I did that now with one of my classes but I can’t call MyClass.myFunction() thats defined as

MyClass.prototype.myFunction = function(){
//imagine there is a game logic in here
}

EDIT: Nevermind…i got what you mean…we created a variable during runtime called singleton and whenever we need to call a function we would need to use something like MyClass.singleton.myFunction(). Something tells me I will need to refresh this script reference inside MyClass.singleton on a swap function if i desire to use this singleton reference with hotReload in case I call a method that may trigger an event inside this script, right?

Hot reloading with singletons is a little tricky. Not tried it before but I suspect you would just need to update the singleton global on the swap.

1 Like