Jump to content

[Help] Access function in html file


studdDev
 Share

Recommended Posts

Hello there,

I am relatively new to game development.

I have defined a function ShowAds() within the html index file

    <script type="text/javascript">

        var version = 'dev',
            libs = [
            ];
        var game = new StuddGames.AppBoot();

        function ShowAds()
        {
            console.log("Show Ad");
        }
    </script>

is there a way i can call this function from typescript within the game code?

Thanks for your help

Link to comment
Share on other sites

Yep, it enters the global namespace, which for browsers is 'window'.

i.e.

window.ShowAds()

although, most of the time the `window` is assumed so, any scripts loaded after that one can just do:

ShowAds()

Up to you whether you decide globals are an anti-pattern or not.

Also, I say most of the time because certain patterns and tools can muck with this, TypeScript, for example, will probably moan about calling a global function it knows nothing about it. Usually there are config on tooling (like TypeScript) that tells it to ignore stuff like this (for example, my linting rules will moan, I can either explicitly mention window—which it will treat as an ok global—or tell it that calling ShowAds as a global function is ok).

Link to comment
Share on other sites

@studdDev Are you including your game code after that script? If so, then the browser works by parsing top-to-bottom (mostly) so the inline script will parse and execute first. Oh, I just noticed you’re creating the game instance before declaring the showAds function, so, yeah, won't work as showAds hasn't been declared when you start the AppBoot() stuff and whatever that does (presumably tries to call the function and thus errors).

I'm guessing your workaround works because you end up executing the showAds invocation several times and it fails first time/s around as it hasn't been created yet and the next time (this could simply be on the next tick) it works. It's often worth checking property existence of globals in any case (defensive coding) but if you just move the game code (and its invocation via new StuddGames) after you declare the global then it'll be there.

Also you don't need bracket notation to reference `showAds` and you don't need to call it either, you can just invoke it in the usual way.

Also also, you leak version, libs and game also to global, this might not be an issue but if some of these ads are a little dodgy they may overwrite the global version (and maybe libs, I'd think game is probably safe enough) variable. May not be an issue for you but something to consider.

Link to comment
Share on other sites

@mattstyles thanks for the info, this is our first game in HTML 5 and i am pretty much a noob when it comes to HTML. 

1 hour ago, mattstyles said:

Also also, you leak version, libs and game also to global, this might not be an issue but if some of these ads are a little dodgy they may overwrite the global version (and maybe libs, I'd think game is probably safe enough) variable. May not be an issue for you but something to consider.

Thank you for all your tips, will surely consider them :)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...