Other-script referal (Global variables)

In relation to:

  • I am wondering how to use the correct ‘other-script referal’ while ‘var app = this.app;’ in such case (link above) isn’t working anymore.
    This is extremely important for all, as it is becoming very cumbersome to write all code into one script.
    I am for instance seeking to have several buttons on my interace. Button-scripts that should be able to refer to other functionality/scripts.

Which method to use in case my external script is named etec55.js and the variable is called fillTrue:




… and so on

Furthermore I have (at least) two options for instantiating the external fillTrue-variable:

var fillSend = false; // Which is by some in here is called a Global varible in here (as well just as non-recomandable as elsewhere)

Etec55.attributes.add(‘fillSend’, {
type: ‘boolean’,
default: false

There are several ways of doing this:

  • You can have entity reference attributes in scripts so you assign it in the scene editor. From the entity, you can reference all the scripts that it has (similar to GetComponent<>() in Unity). You can see an example here https://playcanvas.com/editor/code/560025?tabs=12879333 where I add an attribute to the script so I can assign the worldEntity and access it in the script. Here’s another example of how a script is referenced in another script https://playcanvas.com/editor/code/438243?tabs=6861120 line 19 where it stores a reference to the OrbitCamera script.

  • You can simply have global variables. The issue would be the order of script execution. Assigning it to the app object would be ‘easiest’ but not great practise.

  • Use events to pass data between entities/scripts/globally.

In your specific problem, what is etec55.js? Is it a external library? Is it a pc.script? Is there any way you can create a simple public project that showcases your issue?

1 Like

Ok - thanks again … you are very helpful. I will first try to go by your bullitpoints
(yes etec55.js is a ‘pc.’-created script that should be visible for my other script -> will try and make public project in case I am still stuck,… for readers otherwise: this combined exchange should be useful)

Are you completely sure that:
this.orbitCamera = this.entity.script.orbitCamera;
still works?

I have tried to use this structure, but without luck (says undefined)

  • so I am wondering if the new IIRC-standard (watch above) prevents its from working?

Yes, it still works. In that case though, both scripts are on the same entity which may not be the case for you.

As mentioned before, without seeing how you are trying to do, it makes it very difficult to help your specific use case. If you can create a simple project that showcases the issue, it will really help.

Wouv … kind of a gamobject-dependent namespace - conclusion: it works like a charm (even with changing vars externally). Now I can throttle ahead with the project.