Static URL - Possible?

For developers using the Construct 2 Javascript SDK

Post » Thu Dec 01, 2016 12:58 pm

Hi Guys,

As I understand it at the moment c2runtime.js takes the current url to link to the resources, images/media/logo.png and so on, which is great for about 99.6% of use cases :D . However, not so good for websites using templeting systems or CMS's.

I am trying to upload a game to a WordPress site (a lot more painful than it should be), if I load all the exported files in the WP root directory and use the code from the game index.html file <body> in the home page, I can get the game to work perfectly. But lets be honest, most of the time that's not where we want the game. Because the url is relevant to the files location http://www.domain.com. If I the create a page in WP called Games the url becomes http://www.domain.com/games/, so c2runtime.js now trying to find a folder in the root directory called games, which obviously doesn't exist. Creating a folder called games and trying to trick it doesn't work either.

I know I can embed the games using iframes and other methods but that doesn't fully work for what I'm trying to do.

Is there a way, in c2runtime.js, to declare a static url to the resource folder, ignoring the what the browser says? or to register/enqueue relevant files in the WP functions file?

Thanks in advance
B
4
S
1
Posts: 15
Reputation: 580

Post » Thu Dec 01, 2016 1:13 pm

As far as I know and have experienced, doesn't matter where you placed your resources, if the link point to the index and you haven't moved any other file inside the game structure, it should work fine as intended.

For example, in the past I used to share my games using dropbox and never had problem with linking it. These days I'm sharing them by some websites, where I created a subfolder and placed the game there, using a shortcut (foldername.sitename.com) to link them, like your link structure, but I can link directly with no problem (sitename.com/foldername), because the system will always ask for the files using the same address of the index.html

Hope it helped.
Image
B
108
S
24
G
18
Posts: 1,373
Reputation: 22,824

Post » Thu Dec 01, 2016 3:26 pm

Hi TELLES0808,

Thanks for replying. In a normal website you would be right, a typical structure may be:

Root Folder ¬
js folder
css folder
img folder
game folder
index.html
game.html

and so on, and as long as you point the src in any page to the game folder everything will work fine.

However, WP doesn't work that way, in fact it doesn't even create pages as such, just meta data in the database used to build a page. Although a url may say www.domain.com/page/ the page doesn't physically exist in the directory.

A usual practice when including scripts in WP is to use a function to register/enqueue the paths to the files so that WP knows where to look, unfortunately I'm not having much luck with it, that's why I was wondering if there was a way to bypass the way c2runtime.js grabs the url and replace it with a static one.

Regards
B
4
S
1
Posts: 15
Reputation: 580

Post » Fri Dec 02, 2016 12:15 pm

Can't you just upload it to a subfolder somewhere and then just point an iframe at it?
Scirra Founder
B
400
S
236
G
89
Posts: 24,546
Reputation: 195,471

Post » Fri Dec 02, 2016 2:56 pm

Hi Ashley,

Thanks for the reply. Yes, I can manage that without much of a problem, however, my use case requires that a user plays a game, on the the game over page there is a button to link to a 'my score' page and isn't allowed to go back to the game. This is handled with php $SESSION with a token. Unfortunately, the iframe doesn't end the session, so users can just hit the browser back button to go back to the game, even though I'm using sandbox="allow-same-origin allow-top-navigation" in my iframe. Basically, the ifram isn't letting go.

I suppose I could add some javascript to my 'my scores' page to check where the request originated and end the session that way. I just thought it would be a more elegant solution to include the game scripts within the WP environment.

Thanks
B
4
S
1
Posts: 15
Reputation: 580


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 1 guest