How to fix "Photon is not defined" in TS

Options

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!

Best Answer

  • vadim
    vadim mod
    edited February 2023 Answer ✓
    Options

    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.

Answers

  • vadim
    vadim mod
    edited February 2023
    Options

    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
    Options

    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
    vadim mod
    edited February 2023 Answer ✓
    Options

    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
    Options

    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!