studdDev Posted September 11, 2017 Share Posted September 11, 2017 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 More sharing options...
mattstyles Posted September 11, 2017 Share Posted September 11, 2017 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). studdDev 1 Link to comment Share on other sites More sharing options...
studdDev Posted September 12, 2017 Author Share Posted September 12, 2017 Thanks @mattstyles, but ShowAds(); and window.ShowAds(); is throwing a cannot find property error. but i found a work around. if (window.hasOwnProperty('ShowAds')) { window['ShowAds'].call(); } and seems to be working fine. Thanks again for your help Link to comment Share on other sites More sharing options...
mattstyles Posted September 12, 2017 Share Posted September 12, 2017 @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. studdDev 1 Link to comment Share on other sites More sharing options...
studdDev Posted September 12, 2017 Author Share Posted September 12, 2017 @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 More sharing options...
mattstyles Posted September 12, 2017 Share Posted September 12, 2017 12 hours ago, studdDev said: this is our first game in HTML 5 Make sure you create a thread about it here! During dev maybe, but certainly after you've created a playable version Good luck! Link to comment Share on other sites More sharing options...
Recommended Posts