[SOLVED] Cloning objects using same script reference

Hi,
Im trying to make a slot machine game, for that i want to instantiate the reels and the symbols in each reel.
im storing a reel and a symbol objects in the main scene which later I clone, I call those object prefabs.

here is my script which placed on the reel and the symbol prefabs:

var Duplicator = pc.createScript('duplicator');

// initialize code called once per entity
Duplicator.prototype.initialize = function() 
{
    
};

// update code called every frame
Duplicator.prototype.update = function(dt) {
    
};

Duplicator.prototype.createClone = function (index, position, parentEntity)
{
    
    var cloneObject = this.entity.clone();
    parentEntity.addChild(cloneObject);
    cloneObject.setLocalPosition(position.x,position.y,position.z);  
    cloneObject.name = parentEntity.name + "_" + this.entity.name + index.toString();
    cloneObject.script.destroy("duplicator");
    
    if(this.entity.name == "Reel")
        {
            parentEntity.script.reelsManager.RecieveReels(cloneObject);
            cloneObject.script.reelScript.CreateReel();
        }
    if(this.entity.name == "Symbol")
        {
            cloneObject.model.material = cloneObject.model.material.clone();
            cloneObject.setLocalEulerAngles(90,0,0);
            parentEntity.script.reelScript.RecieveSymbols(cloneObject);
        }
};

As you can see I create a clone and send its entity to his parent, then the parent stores all entities in an array like in the code below

var ReelsManager = pc.createScript('reelsManager');


ReelsManager.attributes.add("symbolPrefab",{type: "entity"});
ReelsManager.attributes.add("reelPrefab",{type: "entity"});


ReelsManager.prototype.reelsAmount = 3;
ReelsManager.prototype.reelsSpacing = 0.1;
ReelsManager.prototype.reelsArray = [];


// initialize code called once per entity
ReelsManager.prototype.initialize = function() 
{
    this.app.on("DupeNow",this.CreateMachine,this);
};

// update code called every frame
ReelsManager.prototype.update = function(dt) {
    
};

ReelsManager.prototype.CreateMachine = function()
{
    for(var i = 0; i<this.reelsAmount; i++)
    {
        var symbolScale = this.symbolPrefab.getLocalScale();
        var pos = {x: (this.entity.getPosition().x + i*(symbolScale.x + this.reelsSpacing)),y: 0, z:0};
        this.reelPrefab.script.duplicator.createClone(i,pos,this.entity);
    }
};

ReelsManager.prototype.RecieveReels = function (reelEntity)
{
  this.reelsArray.push(reelEntity);
};

then each entity (reel) uses the same duplicator script to clone another entities which should be children of this entity (reel), each new symbol clone created stored at reelscript (now i have X of them in scene) but all stored in all reelScripts, i mean that each instance of reelscript cointains the same refferences to all symbols in scene.
adding here reelScript

var ReelScript = pc.createScript('reelScript');


ReelScript.attributes.add("symbolPrefab",{type: "entity"});


ReelScript.prototype.symbolsAmount = 3;
ReelScript.prototype.symbolsSpacing = 0.1;
ReelScript.prototype.symbolsArray = [];


// initialize code called once per entity
ReelScript.prototype.initialize = function() 
{
};

// update code called every frame
ReelScript.prototype.update = function(dt) {
    
};

ReelScript.prototype.CreateReel = function()
{
    for(var i = 0; i<this.symbolsAmount; i++)
    {
        var symbolScale = this.symbolPrefab.getLocalScale();
        var pos = {x: 0,y: (this.entity.getPosition().y + -i*(symbolScale.y + this.symbolsSpacing)), z:0};
        this.symbolPrefab.script.duplicator.createClone(i,pos,this.entity);
    }
};

ReelScript.prototype.RecieveSymbols = function(symbolEntity)
{
    this.symbolsArray.push(symbolEntity);
    console.log(this.symbolsArray.length);
};

is there any way to create a unique reelscript which wont share data with all reels???

This part is in global scope so there’s only one instance.

You want to create this array in the scope of the class so each instance has it’s own array. ie

// initialize code called once per entity
ReelScript.prototype.initialize = function() 
{
    this.symbolsArray = [];
};

I also recommend you change the other variables (amount and spacing) as script attributes as you can edge the values in the editor.

1 Like

WOW, Thank You!
Was using unity before, i`m new to all this scopes idea and JS :slight_smile: