Continuously poll for mouse position in background and also have player interaction?

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
newbiemate
Regular
Posts: 85
Joined: Tue Dec 19, 2017 1:36 pm
Contact:

Continuously poll for mouse position in background and also have player interaction?

#1 Post by newbiemate »

I am trying to set up a really simple mini-game that requires constant polling of the player's mouse position. Depending on where the mouse position is, a few images will show up. At the same time, I also want the player to interact with some of the displayables.

Here is the code:

Code: Select all

screen render_secrets():
    $ x, y = renpy.get_mouse_pos()
    $ print "%s, %s" % (x, y)
    
    if ( mouse pos is some condition):
        # show something at x+10, y+20
    elif (mouse position is some other condition):
        # show something at x+100, y-40
             
    # etc...

screen render_buttons():
    imagebutton:
        idle "images/card_idle.png"
        hover "images/card_hover.png"
        action Return("card")
        xpos 100
        ypos 100
        
    imagebutton:
        idle "images/ball_idle.png"
        hover "images/ball_hover.png"
        action Return("ball")
        xpos 100
        ypos 500

label doit():
    scene bg game
    show screen render_buttons()

    label render_loop:
    
        # continuously poll for mouse coordinates, and display secrets
        show screen render_secrets()
        
        # Allow the player to interact with some displayables
        clicked = ui.interact()
        $ print clicked
        
    jump render_loop
However, this is not working. There are two issues:

- Mouse coordinates are not continuously polled. The console does not output a stream of coordinates. It only outputs once, then stops.

- I think ui.interact() is blocking my loop, because I don't see the results of clicked until I click on a displayable that returns an action (the imagebuttons).

Can someone help me with this issue? I essentially want a way to continuously poll the mouse coordinates in the background and then show images based on where the mouse is, while allowing the player to directly interact with my other displayables too.

The secrets are displayed dynamically, and is not always in a static location, which is why I need to constantly get the mouse position.

User avatar
xavimat
Eileen-Class Veteran
Posts: 1461
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Discord: xavimat
Contact:

Re: Continuously poll for mouse position in background and also have player interaction?

#2 Post by xavimat »

I'd suggest to change completely your approach and use the "hovered" action.
Instead of getting the mouse position and calculating if it's inside an area that triggers something, let renpy do all this work.
Buttons (and imagebuttons, textbuttons, hotspots) have a "hovered" action that is triggered when the mouse enters their area. (the area can be irregular with focus_mask). That action could be to show another parts of the screen, hidden before, that the player can interact with.

Forget agout "ui." functions:
The implementation of Ren'Py has changed, and UI functions that create displayables can now be far slower than their screen language equivalents.
https://renpy.org/doc/html/screen_pytho ... -functions
And don't put labels inside labels, renpy does not complain but it's not necessary.
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

newbiemate
Regular
Posts: 85
Joined: Tue Dec 19, 2017 1:36 pm
Contact:

Re: Continuously poll for mouse position in background and also have player interaction?

#3 Post by newbiemate »

If ui.interact() is no longer going to be supported, can you help give an example of what the equivalent code would be for processing an action?

ie, I have this screen:

Code: Select all

    imagebutton:
        idle "images/card_idle.png"
        hover "images/card_hover.png"
        action Return("card")
        xpos 100
        ypos 100
How do I wait for the user's input in my loop, and then process the Return("card")?

User avatar
xavimat
Eileen-Class Veteran
Posts: 1461
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Discord: xavimat
Contact:

Re: Continuously poll for mouse position in background and also have player interaction?

#4 Post by xavimat »

screens can be used in two ways:
show screen > to show some info to the player, but the game can continue without interacting with the screen. (That screen may [or may not] have also buttons to interact, and it should be hidden explicitly)
call screen > the game is frozen until the interaction on the screen Returns() or does something to continue the game.

Here you need call screen

About the hovered part, please, explain a little bit more what do you want to achieve. There are examples of "point and click" minigames in the cookbook if that's what you want.
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

newbiemate
Regular
Posts: 85
Joined: Tue Dec 19, 2017 1:36 pm
Contact:

Re: Continuously poll for mouse position in background and also have player interaction?

#5 Post by newbiemate »

I guess forget the hovered part... I'll try rewriting the game logic so that it doesn't depend on continuous mouse coordinates like you suggested.

I'm now trying to figure out how to do user interaction with buttons in renpy, without the use of "ui". I was reading this forum and a lot of threads was using ui.interact() to wait for the player's action, which is why I was using it.

If I now have this simple renpy program:

Code: Select all

screen render_buttons():
    imagebutton:
        idle "images/card_idle.png"
        action Return("card")
        xpos 100
        ypos 100
        
    imagebutton:
        idle "images/ball_idle.png"
        action Return("ball")
        xpos 100
        ypos 500

label doit():
    # Allow the player to interact with some displayables
    $ clicked = ui.interact()
    $ print clicked      
    
    # Now do something with clicked
    if clicked == 'ball':
        call ball_label()
        show screen show_more_balls
        
     if clicked == 'card':
         call card_label
         show screen show_more_cards
         
    jump doit

label start():
    scene bg game
    show screen render_buttons()
    call doit()
    
    return
If I press the card imagebutton, it will return a string from the action ("card") and I can then process what should happen after ui.interact(). Similarly, if I click on the ball imagebutton, it will return a string from the action ("ball") and I can do stuff based on this... etc.

Since you are right about not using the ui module anymore (stated in the docs), how should I update the code above? Particularly, I'm not sure how to do this part without the ui.interact():
call screen > the game is frozen until the interaction on the screen Returns()

User avatar
xavimat
Eileen-Class Veteran
Posts: 1461
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Discord: xavimat
Contact:

Re: Continuously poll for mouse position in background and also have player interaction?

#6 Post by xavimat »

Easy.
You'll use the _return variable, as stated here: https://renpy.org/doc/html/screen_actions.html#Return

In this example, the name of the variable "clicked" is not a renpy name, you can use any variable-name you wish:

Code: Select all

screen render_buttons():
    imagebutton:
        idle "images/card_idle.png"
        action Return("card")
        xpos 100
        ypos 100

    imagebutton:
        idle "images/ball_idle.png"
        action Return("ball")
        xpos 100
        ypos 500

label doit:
    # Allow the player to interact with some displayables
    call screen render_buttons
    $ clicked = _return

    # Now do something with clicked
    if clicked == 'ball':
        call ball_label
        # ...

    elif clicked == 'card':
        call card_label
        # ...

    jump doit

label start:
    scene bg game
    call doit

    return
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

Post Reply

Who is online

Users browsing this forum: Google [Bot]