Access double name in hierarchy

I’ve suddenly problems accessing entity with doulbe names in the hirarchy tree.
Got this issue in already tested scripts to :frowning:
root
|->Cam
| |-> Cam

this.app.root.findByName(‘Cam’).findByName(‘Cam’).enabled = true;

got an error ‘cant find property of null’

If I change to:

|->Cam
| |-> Cam2

this.app.root.findByName(‘Cam’).findByName(‘Cam2’).enabled = true;

The script will work again, but I’ve hundreds of lines like this…

Has something changed lately, or do I just imagine things?

Hi @Gurki,

First line with the double names should work correctly, no reason to not work. Can you reproduce this in a sample project and share it to take a look?

It happens if I load a new scene to an existing scene, so not to sure if I can reproduce :frowning: Today I fixed many with ‘find in files’ and ‘replaced all’. Before xmas the code was (mostly) working, today the same code with no changes not :frowning:
Most common is:
root (exsisting Scene)
|-> root (added Scene)
(Has been best working design for multiscene till now)
But the example with the Cam was the first to be noticed by me.

managed to make an similar error in:

https://playcanvas.com/project/678788/overview/herbivore

Script: ‘sceneManger’ at Entity: ‘Scene Manager’
this.app.root.findByName(‘SkyCam’).findByName(‘SkyCam’).enabled = false;

this line will disable the top ‘SkyCam’ and not the ‘SkyCam’ below

Made a very simple demo to show the error:
Test double name in hirarchy tree
-the script in scenemanager init access wrong entity
-script onPress for button works well
but its the same line of code ?

Hi @Gurki,

I think it works as expected? The sceneManager.js script finds and disables the Root/SkyCam/SkyCam entity and disabled it:

What’s the entity you expected to disable? If you want it to target entities inside its local hierarchy then use this.entity.findByName() instead.

1 Like

that was only a sample to show :slight_smile:
It’s more a generel problem. I’ve nearly 500 scripts in my project an more than a thousand line of code with this issue.
In the small example, when testing in chrome browser, the top camera will be disabled
:frowning: If I enable it via editor the cam is on again.
I think the best I can do is rename all entitys with double names to have everything reliable working…
I was just wondering if something with PlayCanvas has changed, becouse behaviour has changed servere

thanx and a happy new year

won’t rework so much code if not necssary :slight_smile:

I don’t think anything around this has changed for years in the engine.

You may want to check if when you call these double names, what is actually in the hierarchy at the time to cross check your logic.

I would also use entity references with script attributes where possible too.

1 Like

I’m not sure what you mean by this?

Edit: Oh, I see what you mean. The top level SkyCam is disabled :thinking:

Yes script attributes are working fine, and Tags altough …
for new scripts I use them but I’ve got so many ‘dead wood?’ (old code) :smiley:

at least I’m happy we could reproduce this, so I’m not totaly mad :slight_smile:

Interesting. It looks like findByName returns itself if the name matches

findByName: function (name) {
			if (this.name === name) return this;
			for (var i = 0; i < this._children.length; i++) {
				var found = this._children[i].findByName(name);
				if (found !== null) return found;
			}
			return null;
		},

This looks intentional and unlikely to be changed.

Assuming that the second name is a direct child underneath the first, you can use:

    this.app.root.findByName('SkyCam').findByPath('SkyCam').enabled = false;

If it’s not a direct child, the workaround is to go through all the children of the first and use findByName on the children.

1 Like

this.app.root.findByName(‘SkyCam’).findByTag(‘SkyCam’)[0].enabled = false;

works too.
I can fix everything, so don’t worry. I’ve posted this issue only becouse I was wondering… :slight_smile:

and maybe the great mystery why I can’t get my plantGrowth script to work is solved too. :smiley:

findByName: function (name) {
for (var i = 0; i < this._children.length; i++) {
var found = this._children[i].findByName(name);
if (found !== null) return found;
}
if (this.name === name) return this;
else return null;
},
?

Yeah, that looks right. I wouldn’t recommend patching the engine’s version but maybe have your own extension or a utility function somewhere.

Ok solved :slight_smile:

if there’s a pitfall be sure I fall for :slight_smile:

thanx and a nice slide into and a good new year 2021