-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Query Strings (URL Parameters) #89
Comments
I'm not sure if expanding the core functionality of any of the love modules is in scope for this project. If you'd like to roll your own extensions, the love.js-api-player lets you interop with the javascript environment. Check out #26. Below is an example of how you could go about getting parameters from your URL into love2d. Once you have the js-api-player setup, add the following to your love2d project. function getParam (param)
local return_value = {}
function getSeedOnDataLoadCallback (data)
-- JS doesn't let us access the return value directly.
-- To work around this we return a table, and then update
-- that table when the return value is ready.
-- Alternatively, you could use a global value.
return_value[1] = data
end
JS.newRequest('getParam(\'' .. param .. '\'), getSeedOnDataLoadCallback)
return return_value
end Add the following to index.html. function getParam (param) {
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const product = urlParams.get(param);
return product;
} To check if data has been returned from the javascript environment use If you want this to run synchronously to avoid headaches make sure you add the following to the top of function love.update (dt)
-- retrieveData will run our onDataCallback when the data
-- is ready.
if(JS.retrieveData(dt)) then
return
end
...
end If you want to get data from the javascript environment during the function love.load ()
local seedTable = getParams('seed') -- seed = {}
while (not(JS.retrieveData(0.001))
do
-- so we don't hog the core
if love.timer then love.timer.sleep(0.001) end
end
-- seed = {'your-param'}
local seed = seedTable[1]
...
end For a more thorough breakdown, and to see how to set up the environment, check out the API-player repo https://github.com/MrcSnm/Love.js-Api-Player |
Would be nice to have the ability to get/set the url parameters.
Example:
mygame.com?seed=12345
love.system.getParam('seed') -> '12345'
love.system.setParam('seed') -> '54321'
https://en.wikipedia.org/wiki/Query_string
https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams
The text was updated successfully, but these errors were encountered: