Passing parameters to play canvas

Hi,

I’ve just started using PlayCanvas and was wondering if its possible to pass in parameters through GET or POST when loading the game? We are making a game that has lots of user interaction at the front end which we are building in HTML 5/Javascript and want to trigger PlayCanvas from user input. From this, we need to pass across some parameters to initialise the 3D component of our game.

Thanks in advanced,

Alan

1 Like

If you are hosting on playcanvas.com, you can use query parameters in the URL, and they will be available in javascript as usual, e.g. as window.location.search

Thanks for the quick response Dave,

What happens if we are hosting the game somewhere else? We are aiming to host this on our own server with the rest of the application. Does that change where these parameters are available?

Thanks

Alan

If you’re hosting the application yourself, it’s up to you how you integrate it. PlayCanvas games are just a client-side javascript application so you can interface with them however you like.

Hi, here is a good function I found for grabbing url parameters:

function getURLParameter(name) {
  return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [null, ''])[1].replace(/\+/g, '%20')) || null;
}

So you can use:

myvar = getURLParameter('myvar');

2 Likes