[Solved]Issue with "renpy.focus_coordinates()"

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.
Post Reply
Message
Author
User avatar
Dark79
Regular
Posts: 63
Joined: Sun Apr 18, 2021 2:21 pm
Contact:

[Solved]Issue with "renpy.focus_coordinates()"

#1 Post by Dark79 »

Hi everyone,

I am trying to use renpy.focus_coordinates() function in screens language but it doesn't work the way as I assumed it would.

It says-
This attempts to find the coordinates of the currently-focused displayable. If it can, it will return them as a (x, y, w, h) tuple. If not, it will return a (None, None, None, None) tuple..

Here is the code:

screen TestScreen():

default button_xpos = 0

textbutton "Get Xpos" xalign 0.5 yalign 0.5:
action SetScreenVariable("button_xpos", renpy.focus_coordinates()[0]) #On click it should set the xpos of the focused button

text "Coordinates - [button_xpos]" xalign 0.5 yalign 0.3 #Display the button's coordinates

textbutton "Return" xalign 0.5 yalign 0.8:
action Return()

label start:

call screen TestScreen
$ renpy.pause(hard=True)
return


Problem is that the button is focused when I hover with mouse or keyboard and once it is clicked, it displays the result as "None" but when I click again, then on the second time it will show correct xpos of the button. Why can't I get the right xpos result on the first time when clicking?
Last edited by Dark79 on Wed Dec 15, 2021 3:09 pm, edited 1 time in total.

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Issue with "renpy.focus_coordinates()"

#2 Post by Remix »

The value of a screen action is evaluated when the screen is shown (not when the click occurs)

You could maybe do it through a Function call...

Code: Select all

init python:

    def set_button_xpos():

            cs = renpy.current_screen()

            if cs is None:
                return

            cs.scope["button_xpos"] = renpy.focus_coordinates()[0]

            renpy.restart_interaction()

...

    action Function(set_button_xpos)
Frameworks & Scriptlets:

User avatar
Dark79
Regular
Posts: 63
Joined: Sun Apr 18, 2021 2:21 pm
Contact:

Re: Issue with "renpy.focus_coordinates()"

#3 Post by Dark79 »

Remix wrote: Wed Dec 15, 2021 2:21 pm The value of a screen action is evaluated when the screen is shown (not when the click occurs)

You could maybe do it through a Function call...

Code: Select all

init python:

    def set_button_xpos():

            cs = renpy.current_screen()

            if cs is None:
                return

            cs.scope["button_xpos"] = renpy.focus_coordinates()[0]

            renpy.restart_interaction()

...

    action Function(set_button_xpos)
Thank you so much for the fast reply and solution, this actually works. Not exactly sure what is going on here ( never checked renpy.current_screen() & renpy.restart_interaction() ) but it works. :D

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: [Solved]Issue with "renpy.focus_coordinates()"

#4 Post by Remix »

Because you are using a screen variable (rather than global) we need to target the screen itself... so

renpy.current_screen().scope["screen variable name"]

is what we use there.

We use renpy.restart_interaction() at the end to effectively just redraw the screen, so the text "Coordinates - [button_xpos]" bit updates
Frameworks & Scriptlets:

User avatar
Dark79
Regular
Posts: 63
Joined: Sun Apr 18, 2021 2:21 pm
Contact:

Re: [Solved]Issue with "renpy.focus_coordinates()"

#5 Post by Dark79 »

Remix wrote: Wed Dec 15, 2021 3:15 pm Because you are using a screen variable (rather than global) we need to target the screen itself... so

renpy.current_screen().scope["screen variable name"]

is what we use there.

We use renpy.restart_interaction() at the end to effectively just redraw the screen, so the text "Coordinates - [button_xpos]" bit updates
Thanks for explaining more clearly. I have tried to place variable "button_xpos" outside of the screen and use SetVariable() instead of SetScreenVariable(), but I got the same result. So far I used global variables while placing them inside the def functions.

But as much I understand I don't need to use renpy.current_screen() to access those variables that are outside from screen.

I have tried to find more info about renpy.current_screen() but it seems to be nowhere in the documentations.

Post Reply

Who is online

Users browsing this forum: Majestic-12 [Bot], NoFanru