Page 1 of 1

Pass a parameter to a renpy's Web version game? Get, store, use

Posted: Wed Nov 30, 2022 4:30 am
by renpynoob
Hello!

How to pass a parameter to a renpy web version game, read it and use it inside the game?
Is there a way to read parameters from initializing url?
Let's start the game from url yourserver.com/yourgame/index.html?name1=FirstCharacter&name2=SecondCharacter
I need to: 1. get those name1 and name2 variables from url (how?), 2. store them somehow (??) and 3. use them (how to call them ??) in my game.

If you know some alternatives, instead of getting params from url, please share too.

Re: Pass a parameter to a renpy's Web version game? Get, store, use

Posted: Wed Nov 30, 2022 4:57 pm
by span4ev
If I understand you correctly, you need HTTP requests?
It took me a couple of days recently to get Renpy and the requests library working together, but in the end I gave up. If you can do it, let me know. I didn't have a specific goal in mind, I just wanted to see if I could use Renpy to query a site and get data from it, like weather. I haven't delved into the requests library, but you need something like that:

Code: Select all

init python:

    import requests

    URL = 'https://lemmasoft.renai.us/forums/'
    response = requests.get(URL)
    status = str(response.status_code)

screen test:
    vbox:
        text status # 200. This means that we got an answer and everything is fine
We can also add a few lines and get an HTML page:

Code: Select all

init python:

    import requests

    URL = 'https://lemmasoft.renai.us/forums/'
    response = requests.get(URL)
    text = response.text
    status = str(response.status_code)

    chars = '{}[]'
    result = text.translate(str.maketrans('', '', chars))

screen test:

    vbox:
        text str(result)
The result will be this:
screenshot0005.png
Now the data is in unformatted form, so it would be good to put it in order with other libraries or get specific data via tag, class, ID, get several tags, all of them, or some specific one. But unfortunately Renpy refuses to work with pyowm, bs4, lxml. I have all this installed (CMD --> pip install ...), it all works fine in pure python file, but Renpy gives error "ModuleNotFoundError". Maybe renpy needs a special PATH, I don't know... My PATH is set to installed Python. But maybe renpy doesn't support working with these libraries. I didn't post a question about this topic on the forum because I didn't expect to get an answer, as this topic is specialized and narrowly focused and hardly anyone knows what the issue is. If anyone should ask PyTom, but I was too embarrassed to do so.

If someone asks me "why do you want it, kid?" I have nothing to say... Because it's just curiosity.

In your case you need:

Code: Select all

URL = 'http://yourserver.com/yourgame/index.html'
response = requests.get(URL)
text = response.text

screen test:
	text str(text)
Result:

Code: Select all

<html><head></head><body><!-- vbe --></body></html>
As you can see, the HTML page is empty. To get anything out of it, you can refer to a tag, such as <p> or <a> or by class name (e.g. ".name" or by id = "#name").
The libraries listed above are needed just for these tasks, but they don't work for me...

You can get the data you want through a slice of the result or through a search for occurrences in a string.

Re: Pass a parameter to a renpy's Web version game? Get, store, use

Posted: Thu Dec 01, 2022 5:45 am
by renpynoob
span4ev wrote: Wed Nov 30, 2022 4:57 pm If I understand you correctly, you need HTTP requests?
Thanks for the reply. More likely I need to solve a less difficult task: I need to read parameters from GET request I got.
When someone starts my game using url https://domain.com/game/index.html?param1=var1&param2=var2 I need to get and use those parameters from url.

Example in PHP below:

Code: Select all

if (isset($_GET['param1'])) {	# checking if a parameter used in the initializing url, i.e. in url of the page where we're now
    $name1 = $_GET['param1'];	# reading the parameter and store it in $name1 variable
    echo "Your name:<br>";
    echo $name1;			# printing $name1 variable, i.e. we're using the parameter we got from the url
    }
If someone opens url https://domain.com/game/index.html?param1=MyName page shows:
Your name:
MyName


Unfortunately, I don't know python and don't know if renpy's web games are fully support it.

Re: Pass a parameter to a renpy's Web version game? Get, store, use

Posted: Thu Dec 01, 2022 6:47 am
by drKlauz
This worked locally with RenPy 7.5.2 and Chrome. It is hacky and will need testing with actual game on live server to be sure.

Code: Select all

define game_url=__import__("emscripten").run_script_string("document.URL") if renpy.emscripten else None

Re: Pass a parameter to a renpy's Web version game? Get, store, use

Posted: Thu Dec 01, 2022 11:33 am
by renpynoob
drKlauz wrote: Thu Dec 01, 2022 6:47 am This worked locally with RenPy 7.5.2 and Chrome. It is hacky and will need testing with actual game on live server to be sure.

Code: Select all

define game_url=__import__("emscripten").run_script_string("document.URL") if renpy.emscripten else None
I don't understand what this code is doing and how to use parameters I got (if the code works in reallife) in the code of my game :)

P.s. I will pay if anyone makes a solution for the task I've asked about. Please PM me.

Re: Pass a parameter to a renpy's Web version game? Get, store, use

Posted: Thu Dec 01, 2022 5:51 pm
by drKlauz
renpynoob wrote: Thu Dec 01, 2022 11:33 am
drKlauz wrote: Thu Dec 01, 2022 6:47 am This worked locally with RenPy 7.5.2 and Chrome. It is hacky and will need testing with actual game on live server to be sure.

Code: Select all

define game_url=__import__("emscripten").run_script_string("document.URL") if renpy.emscripten else None
I don't understand what this code is doing and how to use parameters I got (if the code works in reallife) in the code of my game :)

P.s. I will pay if anyone makes a solution for the task I've asked about. Please PM me.
This code set value of "game_url" variable to string in address bar of browser. When tested locally it will be "127.0.0.1:1234/index.html", when deployed it will be "yourdomain.com/game/index.html". If you provide link like "yourdomain.com/game/index.html?param1=123" it will/should still work just fine, but game_url variable will contain "?param1=123" part too.
This will work only for web builds, obviously. Otherwise game_url will be set to None.
Code is trivial:
- check if we running in browser by checking renpy.emscripten
- if yes, import emscripten module used by RenPy to interact with browser
- call run_script_string method of emscripten module, it will execute javascript code and return value as string
- code we execute returns current document URL, which is address of game html file
- set game_url value to retrieved URL

To get this param1 value you need to process game_url content if there is any. Simplest approach would be something like:

Code: Select all

label special_url_check:
  if isinstance(game_url,basestring) and "?param1=" in game_url:
    $special_code=game_url.partition("?param1=")[2]
    ## add check for code here, if code is valid do something, if not do something else
    "Special code is [special_code]. Here is your special content."
  else:
    "No special content for you."
  return
Value of game_url is string, any usual python string manipulations are available.

If things are still unclear, well, dm me specific questions here or on discord.

Re: Pass a parameter to a renpy's Web version game? Get, store, use

Posted: Thu Dec 01, 2022 7:28 pm
by span4ev
renpynoob wrote: Thu Dec 01, 2022 5:45 am Unfortunately, I don't know python and don't know if renpy's web games are fully support it.
It's hard for me to imagine a web build of a game, but it's easy for me to imagine a string that contains characters. As I wrote earlier,
you need slices or search for the occurrence of a substring in a string. So, we have some URL that has parameters in it and we need to extract the parameters from the string

echo - I think that's from PHP. We're not going to need that.

URL = 'https://domain.com/game/index.html ?param1=var1¶m2=var2'
¶ what is that? I think it should be "param2"? I'm not sure what character should separate the parameters, but let it be a comma
So let the string be, for example: https://domain.com/game/index.html ?param1=var1,param2=var2

Code: Select all

URL = 'https://domain.com/game/index.html ?param1=var1,param2=var2'

# Now let's remove all unnecessary stuff through the cut
index = URL.find('param')
result = URL[index:]   # param1=var1,param2=var2

# Now divide the string into a list
result = result.split(',') # ['param1=var1', 'param2=var2'

# if you want to leave only the values after the equal sign:
lst = [] 
for i in range(len(result)):
	index = result[i].find('=')
	var = result[i][index:].replace('=', '')
	lst.append(var)


for i in lst:
	print(i)

# var1
# var2
We get variable values that you can write to the variables you want with your name.
Here you need to understand how many variables will come, count their number. You can access the list "lst"
and get the values by index, for example:

Code: Select all

var_1 = lst[0] # var1
var_2 = lst[1] # var2
var_n = lst[-1] # var_n

Re: Pass a parameter to a renpy's Web version game? Get, store, use

Posted: Fri Dec 02, 2022 12:04 am
by PyTom
You can get the URL as a Python string with:

Code: Select all

init python:
     try:
          import emscripten 
          web_params = emscripten.run_script_string("location.search")
     except ImportError:
          web_params = ""
And then use web_params to pick apart the string. (It'll be something like '?user=foo&password=not%20really', that is, url-quoted.)

Re: Pass a parameter to a renpy's Web version game? Get, store, use

Posted: Sat Dec 03, 2022 2:06 am
by renpynoob
Thank you very much drKlauz, span4ev and PyTom for your help and explanations!
Will try to implement this :)