The Photon Forum
is Closed Permanently.

After many dedicated years of service, we have made the decision to retire our Forum and switch to read-only: we´ve saved the best to last! Your search result can be found below. Plus, we offer support via these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

How to fix "Photon is not defined" in TS

Nevey
2023-02-08 20:14:07

Hello!

I'm just warming up to TypeScript so there's a good chance I'm simply missing something here.

I've added the Photon Javascript SDK (including the d.ts file) to my Cocos Creator project:

Next, I'm only trying to start the LoadBalancingClient:

VSCode recognizes Photon because it's declared as a module in "Photon-Javascript_SDK.d.ts, but when running it, it's not defined. Most likely because I'm not importing it anywhere? I've no idea what the best approach is here.

If someone could give me a hand I'd be most grateful!😊

Cheers!

Comments

vadim
2023-02-09 09:18:57

Hi,

To have Photon in the global namespace, you need to run Photon-Javascript_SDK.js.

We already answered how to do it: https://forum.photonengine.com/search?query=cocos

I hope this haven't changed in Cocos since then.

Nevey
2023-02-09 09:41:39

Hi Vadim,

Thanks for the clarity and quick answer. However, when I follow these instructions and import the SDK as a plugin it doesn't change anything, Photon is still not defined in global namespace.

SDK plugin settings:

I've tried "Enclosed" (which will cause the import to fail, makes sense) instead of "Global". Also tried enabling "Load in Native" and play around with other loads enabled/disabled.

I'm obviously missing something here.

Cheers!

vadim
2023-02-10 11:40:22

I installed Creator 3.7.0, created a new project, added Photon-Javascript_SDK.js, chose Global and checked all boxes in its properties, saved the properties (pressed the button with green checkmark) and run the project. Then I opened browser console and typed 'Photon' and pressed Enter. Photon global object properties were printed in console.

Nevey
2023-02-10 11:47:10

I did exactly the same. Maybe I'll have to play with my tsconfig a bit? I got this exact same approach working for i.e. the PlayFab sdk.

Thanks for confirming this is the way.

Cheers!

Back to top