[SOLVED]Problem getting mouse position in a screen

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
RicharDann
Veteran
Posts: 286
Joined: Thu Aug 31, 2017 11:47 am
Contact:

[SOLVED]Problem getting mouse position in a screen

#1 Post by RicharDann »

I'm trying to make a screen where I need to get mouse position when the player clicks the mouse (or touches the screen if on Android), to then show a small click animation on that position. Browsing through the documentation I found renpy.get_mouse_pos() and it seems to be what I need. I made a screen to test and placed a variable to store and use the tuple this function returns, and I've encountered a strange problem while using it.

When I call the screen, the initial mouse position is shown on the variable, but then when I click it doesn't immediately update. I need to double click for some reason for the variable to update on the screen, and even then sometimes it shows an old position of the mouse instead of current.

Here's the code:

Code: Select all

screen mouse_test():
    default mouse_xy = renpy.get_mouse_pos()
    
    text "[mouse_xy[0]], [mouse_xy[1]]"
    
    key "mousedown_1" action SetScreenVariable('mouse_xy', renpy.get_mouse_pos())
Am I perhaps approaching this the wrong way?
Last edited by RicharDann on Mon Oct 09, 2017 11:35 am, edited 1 time in total.
The most important step is always the next one.

User avatar
Divona
Miko-Class Veteran
Posts: 678
Joined: Sun Jun 05, 2016 8:29 pm
Completed: The Falconers: Moonlight
Organization: Bionic Penguin
itch: bionicpenguin
Contact:

Re: Problem getting mouse position in a screen

#2 Post by Divona »

For some reason, renpy.get_mouse_pos() just update rather slowly in screen. Might try to do it outside?

Code: Select all

default mouse_xy = (0, 0)

init python:

    def get_mouse():
        global mouse_xy
        mouse_xy = renpy.get_mouse_pos()

screen mouse_test():

    on "show" action Function(get_mouse)

    text "[mouse_xy[0]], [mouse_xy[1]]"

    key "mousedown_1" action Function(get_mouse)
Completed:
Image

User avatar
RicharDann
Veteran
Posts: 286
Joined: Thu Aug 31, 2017 11:47 am
Contact:

Re: Problem getting mouse position in a screen

#3 Post by RicharDann »

Divona wrote: Mon Oct 09, 2017 10:45 am try to do it outside?
Yes, that works! I had originally planned to do it from outside but I read somewhere in this forum that it was better to do things locally if possible, but it seems better to do it globally for this case.

Thanks a lot, Divona!
The most important step is always the next one.

User avatar
RicharDann
Veteran
Posts: 286
Joined: Thu Aug 31, 2017 11:47 am
Contact:

Re: [SOLVED]Problem getting mouse position in a screen

#4 Post by RicharDann »

UPDATE:

Oddly enough, when I added the show animation code to the key statement, getting the mouse pos started slowing down again unless I double clicked. I started tinkering with it once again and I stumbled upon what seems to be a solution: adding a mouse_up event along with the mouse_down seems to fix the slowdown issue. I haven't encountered any other problems with this so far, only thing left for me to do is to add the anim but that should be easy enough.

Here's how the code ended up looking like:

Code: Select all

init python:

    def get_mouse():
        global mouse_xy
        mouse_xy = renpy.get_mouse_pos()

screen mouse_test():

    on "show" action Function(get_mouse)

    text "[mouse_xy[0]], [mouse_xy[1]]"

    key "mousedown_1" action Function(get_mouse)
    key "mouseup_1" action [Function(get_mouse), Show('spark', x=mouse_xy[0], y=mouse_xy[1])]
The most important step is always the next one.

User avatar
Divona
Miko-Class Veteran
Posts: 678
Joined: Sun Jun 05, 2016 8:29 pm
Completed: The Falconers: Moonlight
Organization: Bionic Penguin
itch: bionicpenguin
Contact:

Re: [SOLVED]Problem getting mouse position in a screen

#5 Post by Divona »

With 'mouseup' method, player can hold left mouse and drag, when releases the spark show up at the wrong place. Here is another option using timer to get mouse position at all time:

Code: Select all

default mouse_xy = (0, 0)

init python:

    def get_mouse():
        global mouse_xy
        mouse_xy = renpy.get_mouse_pos()

screen mouse_test()

    on "show" action Function(get_mouse)

    text "[mouse_xy[0]], [mouse_xy[1]]"

    timer 0.01 repeat True action Function(get_mouse)

    key "mousedown_1" action Show("spark", x=mouse_xy[0], y=mouse_xy[1])
Completed:
Image

User avatar
RicharDann
Veteran
Posts: 286
Joined: Thu Aug 31, 2017 11:47 am
Contact:

Re: [SOLVED]Problem getting mouse position in a screen

#6 Post by RicharDann »

I realized this just yesterday, hadn't come up with a solution yet but timer looks like a fine solution, I didn't know it could be used like that, it works wonderfully.

Only problem left is that the animation doesn't show up above buttons and other clickable areas, but that is to be expected since putting this screen on a higher layer than others would probably make them unable to capture the mouse click. Right?

Once again thanks for your help Divona, I've learned a lot from this and other posts of yours out there, you're amazing! :D
The most important step is always the next one.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], BBN_VN, Bing [Bot], geoWaffle, Google [Bot]