How to name a screen by the content of a variable? [SOLVED]

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Message
Author
User avatar
pyPat
Regular
Posts: 30
Joined: Wed Sep 11, 2019 5:34 pm
Github: patlol
Location: France
Contact:

How to name a screen by the content of a variable? [SOLVED]

#1 Post by pyPat »

Hello
i want name a screen by the content of a variable, i.e.

Code: Select all

label play_screen(nameScreen, nameImage, x, y):
    $ nameImageVar = nameImage
    $ xVar = x
    $ yVar = y
    screen nameScreen:
        add nameImageVar xalign xVar yalign yVar
        modal False
    show screen nameScreen with Dissolve(1.0)
    return
    
label start:
    $ theNameScreen = "aname"
    call play_screen(theNameScreen, "judith parle.png", 0.99, -0.1)
    "Show screen"
    hide screen theNameScreen
    "nothing anymore?"
    hide screen nameScreen
    "and now?"
after the first hide

Code: Select all

 hide screen theNameScreen
the image is still visible, but the second hide

Code: Select all

hide screen nameScreen
deletes it, which means that the screen does not have the value of the variable as its name but its name: nameScreen and not aname

How to name a screen by the content of a variable, to manipulate it with this identity?
$ var = "nameofscreen"
...
hide screen var
Last edited by pyPat on Thu Oct 17, 2019 11:37 am, edited 1 time in total.
English is not my native language; please excuse typing errors.

User avatar
Kia
Eileen-Class Veteran
Posts: 1040
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

Re: How to name a screen by the content of a variable?

#2 Post by Kia »

the screen you are showing the image inside is named: nameScreen
when you hide the screen "aname" using `hide screen theNameScreen` it wouldn't have any effect on the "nameScreen" screen.
I can only assume that the screen name can't be a variable while creating a new screen since it doesn't accept a string as a name like: screen "aname":
I can think of few cases that creating screens on the fly would be useful but for now you're stuck with pre-defined screens that's content can be changed instead.

User avatar
Alex
Lemma-Class Veteran
Posts: 3093
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: How to name a screen by the content of a variable?

#3 Post by Alex »

pyPat wrote: Fri Oct 11, 2019 10:05 am Hello
i want name a screen by the content of a variable, i.e. ...
Why do you want to rename screens in midgame in the first place?
You've already made a screen with dynamic (changable) content in it. You can show this one screen with different content or even change content while screen is shown, and easily hide it using its name.
What do you try to achieve?

User avatar
Kia
Eileen-Class Veteran
Posts: 1040
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

Re: How to name a screen by the content of a variable?

#4 Post by Kia »

Alex wrote: Fri Oct 11, 2019 11:54 am Why do you want to rename screens in midgame in the first place?
his use case might not be that hard to achieve with few screens but I can think of one case, imagine you want to show a small text that fades out every time the player clicks something, naturally you don't know how many screens you would need and defining a new screen with a new name for each would prevent over writing another screen that isn't faded out yet.

User avatar
pyPat
Regular
Posts: 30
Joined: Wed Sep 11, 2019 5:34 pm
Github: patlol
Location: France
Contact:

Re: How to name a screen by the content of a variable?

#5 Post by pyPat »

yes this would be necessary to create a large number of screens on the fly with a call and put the generic code in a block. For me, that was the goal.

Yes when we do not know the number of screens that will have to be called in a loop or under a condition and be able to manage them individually.
And certainly many other uses....

It's a shame that we can't name a screen by a variable. That would be an improvement to consider!
English is not my native language; please excuse typing errors.

User avatar
pyPat
Regular
Posts: 30
Joined: Wed Sep 11, 2019 5:34 pm
Github: patlol
Location: France
Contact:

Re: How to name a screen by the content of a variable?

#6 Post by pyPat »

Maybe in python with something like:

Code: Select all

dicScreen = dict();
var = 'screen'+i;
dicScreen[var] = 'nameScreen';
if there are functions or methods equivalent to screen name, show screen name and hide screen name in python. To be explored

Code: Select all

 renpy.define_screen(name, function, modal="False", zorder="0", tag=None, variant=None)
 renpy.hide_screen(tag, layer=None)
 renpy.show_screen(_screen_name, *_args, **kwargs)
This should work, right? I've never tried python for ren'py features before

But I don't understand what function is in this case https://www.renpy.org/doc/html/screen_p ... ine_screen
renpy.define_screen(name, function, modal="False", zorder="0", tag=None, variant=None)
Defines a screen with name, which should be a string.

function
The function that is called to display the screen. The function is called with the screen scope as keyword arguments. It should ignore additional keyword arguments.
The function should call the ui functions to add things to the screen.
English is not my native language; please excuse typing errors.

User avatar
Kia
Eileen-Class Veteran
Posts: 1040
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

Re: How to name a screen by the content of a variable?

#7 Post by Kia »

pyPat wrote: Fri Oct 11, 2019 2:46 pm This should work, right? I've never tried python for ren'py features before
In theory yes, you can use `renpy.define_screen` to create a screen on fly, but then you need to work with ui elements and that's a can of worm I opened once and not touching again, they're finicky, slow and as far as I know, there's no documentation on them. I started thinking, this is exactly what I need and days later found myself digging through renpy files to find out what arguments a timer takes.

User avatar
pyPat
Regular
Posts: 30
Joined: Wed Sep 11, 2019 5:34 pm
Github: patlol
Location: France
Contact:

Re: How to name a screen by the content of a variable?

#8 Post by pyPat »

Kia wrote: Sat Oct 12, 2019 2:56 am they're finicky, slow and as far as I know, there's no documentation on them.
OK, ok :cry:
I see, everything I love... Especially the lack of doc, it's the most horrible thing I know.
I'm not opening this Pandora's box!
Thank you for the feedback!

By the way, how do you like the ren'py doc? Personally, I find that there are not enough concrete examples, and for a non-English speaker the sentences are not always clear. I am aware that the document produced already represents a lot of work...
On Discord once I dared to talk vaguely about it, I was insulted. Maybe I'm the one who's slow...
English is not my native language; please excuse typing errors.

User avatar
Kia
Eileen-Class Veteran
Posts: 1040
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

Re: How to name a screen by the content of a variable?

#9 Post by Kia »

pyPat wrote: Sat Oct 12, 2019 5:48 am By the way, how do you like the ren'py doc?
Well, I've learned everything I know from the documentation, since it's maintained by the community it does lack a fair bit when it comes to example codes and explanation, specially with older parts like ui, but till now asking in the forums covered most of my problems.
I know I'm partially to blame for not participating in the maintenance but I have my excuses:
  • I barely find the time to work enough to catch up with my bills.
  • There never been a push for it.
  • I don't even know where to start and how to participate.
  • And I'm lazy.

User avatar
pyPat
Regular
Posts: 30
Joined: Wed Sep 11, 2019 5:34 pm
Github: patlol
Location: France
Contact:

Re: How to name a screen by the content of a variable?

#10 Post by pyPat »

Lazy, not really, I often see you answer on the forum where you solve a lot of problems.

ren'py it's a real tour de force (challenge) to have written this with python 2.x and pygame, it's quite fluid.
For the documentation I think my nullity in English and the poverty of google trad counts a lot!

Thank you anyway and good luck with the bills :)
English is not my native language; please excuse typing errors.

strayerror
Regular
Posts: 159
Joined: Fri Jan 04, 2019 3:44 pm
Contact:

Re: How to name a screen by the content of a variable?

#11 Post by strayerror »

Based on the outline in your original post, you can accomplish what you wanted, however I'd just caution that it's a little beyond what's required in most situations, so it's possible a more suitable solution may exist for your situation. None-the-less, here's one technical solution to the problem posed:

Code: Select all

# Example set up code - DO NOT USE
image jparle = Solid('f77c', xysize=(320, 240))


# Example
screen play_screen(nameImageVar, xVar, yVar):
    add nameImageVar xalign xVar yalign yVar

label start:
    $ theNameScreen = "aname"
    show screen play_screen('jparle', 0.99, -0.1, _tag=theNameScreen) with Dissolve(1.0)
    "Show screen"
    menu:
        "hide using a {b}literal{/b} name":
            hide screen aname
        "hide using a {b}dynamic{/b} name":
            $ renpy.hide_screen(theNameScreen)
    "nothing anymore!"
    "fin."
    return

User avatar
pyPat
Regular
Posts: 30
Joined: Wed Sep 11, 2019 5:34 pm
Github: patlol
Location: France
Contact:

Re: How to name a screen by the content of a variable?

#12 Post by pyPat »

YES!
Great, it works perfectly.
The use of _tag is the key.

I saw the use of tag in the doc on:
renpy.hide_screen(tag, layer=None)
and
renpy.define_screen(name, function, modal="False", zorder="0", tag=None, variant=None)

Actually he talks about _tag, usable with the solution proposed by Kia, but not in show statement
I didn't make the connection....

I ask myself a question: how did you find this kind of thing? insider? magic? IQ around 180? Incantations of the god Python ? reading the source files of ren'py???
Or simply full knowledge and understanding of the doc?
I want to know! :)

Anyway, thank you!
English is not my native language; please excuse typing errors.

strayerror
Regular
Posts: 159
Joined: Fri Jan 04, 2019 3:44 pm
Contact:

Re: How to name a screen by the content of a variable?

#13 Post by strayerror »

For the most part it's a pretty decent mix of docs, the source files and reading bits of code that others have shared on the forums to learn how people do things (and trying to recognise when there may be better ways to do the same). All that combined with visiting the questions area here fairly frequently and trying to solve some of the more interesting problems people come up with, both as a way to help out, and also to learn what's possible, even if I don't need it at that moment - as was the case here! Glad it does what you need, good luck with your project!

User avatar
Alex
Lemma-Class Veteran
Posts: 3093
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: How to name a screen by the content of a variable?

#14 Post by Alex »

Kia wrote: Fri Oct 11, 2019 12:07 pm ...I can think of one case, imagine you want to show a small text that fades out every time the player clicks something ...
Kind of sample to play with :roll:

Code: Select all

init python:
    def my_func():
        for i in range(100):
            tag = "scr_{}".format(i)
            if not renpy.get_screen(tag):
                renpy.show_screen("my_text_scr", txt=tag, tag=tag, _tag=tag)
                break

transform my_move_text_transform:
    linear 1.5 yoffset -100 alpha 0.1

screen my_text_scr(txt, tag):
    default txt_pos = None
    
    if not txt_pos:
        $ txt_pos = renpy.get_mouse_pos()
        
    text txt pos txt_pos at  my_move_text_transform
    timer 2.0 action Hide(tag) repeat False
    
screen test_scr():

    key "mousedown_1" action Function(my_func)
    
    textbutton "Done" action Return() align (0.95, 0.05)

label start:
    "..."
    call screen test_scr
    "?!"

User avatar
pyPat
Regular
Posts: 30
Joined: Wed Sep 11, 2019 5:34 pm
Github: patlol
Location: France
Contact:

Re: How to name a screen by the content of a variable?

#15 Post by pyPat »

Thank you Alex, great example!

It took me a little while to understand all the code but it's okay.
Only one question: why repeat False
in

Code: Select all

timer 2.0 action Hide(tag) repeat False
This code has no reason to repeat even without repeat False ???

I love this kind of improbable code, just for the beauty of it.
strayerror wrote: Mon Oct 14, 2019 4:45 pm both as a way to help out, and also to learn what's possible
:D thank you
English is not my native language; please excuse typing errors.

Post Reply

Who is online

Users browsing this forum: Google [Bot]