[Solved] Activate button on press (not release)

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
goldo
Regular
Posts: 127
Joined: Mon Jan 23, 2017 8:23 am
Contact:

[Solved] Activate button on press (not release)

#1 Post by goldo »

Hi everyone,

I am making a minigame where the player must click on a moving button.

However, the clicking only 'happens' upon releasing the mouse button, not pressing it down, which leads to player frustration as they have to release it really quick or nothing happens. It also leads to a strange situation where it works better to always hold the mouse button down then release it on target, instead of just clicking.

So, long story short, is there any way to create a button that activates on pressing down the left mouse button?
Last edited by goldo on Sun Jul 21, 2019 12:58 pm, edited 1 time in total.

User avatar
Per K Grok
Miko-Class Veteran
Posts: 882
Joined: Fri May 18, 2018 1:02 am
Completed: the Ghost Pilot, Sea of Lost Ships, Bubbles and the Pterodactyls, Defenders of Adacan Part 1-3, the Phantom Flyer
itch: per-k-grok
Location: Sverige
Contact:

Re: Activate button on press (not release)

#2 Post by Per K Grok »

goldo wrote: Sun Jul 07, 2019 9:09 am Hi everyone,

I am making a minigame where the player must click on a moving button.

However, the clicking only 'happens' upon releasing the mouse button, not pressing it down, which leads to player frustration as they have to release it really quick or nothing happens. It also leads to a strange situation where it works better to always hold the mouse button down then release it on target, instead of just clicking.

So, long story short, is there any way to create a button that activates on pressing down the left mouse button?

key mousedown acts on press, so you can use than to build your own 'button' that reacts to on press.

You could do it with the code below.

bX,bY is the position of the button
bW,bH is width and height.

Code: Select all


screen checkMouse():
   key "mousedown_1" action Jump("checkClick")

---------

label checkClick:
   python:
       mX=renpy.get_mouse_pos()[0]
       mY=renpy.get_mouse_pos()[1]

    if bX<mX<bX+bW and bY<mY<bY+bH:
        --- god things happen ---
   else:
        --- bad things happen ---

    jump gamelabel
One thing you have to watch out for with this time of solution is 'side rollback'. You need to have that turned off or make certain the 'button' never is in the side rollback area.

If by moving you mean jumping from point to point in an instance you will always know bX and bY, so that will not be a problem.

If you mean moving like in a Duck Hunt type game that can be more problematic dependent on how the movement is coded. But that can be handled to.

User avatar
Andredron
Miko-Class Veteran
Posts: 718
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

Re: Activate button on press (not release)

#3 Post by Andredron »

Per K Grok wrote: Sun Jul 07, 2019 11:49 am
goldo wrote: Sun Jul 07, 2019 9:09 am Hi everyone,

I am making a minigame where the player must click on a moving button.

However, the clicking only 'happens' upon releasing the mouse button, not pressing it down, which leads to player frustration as they have to release it really quick or nothing happens. It also leads to a strange situation where it works better to always hold the mouse button down then release it on target, instead of just clicking.

So, long story short, is there any way to create a button that activates on pressing down the left mouse button?

key mousedown acts on press, so you can use than to build your own 'button' that reacts to on press.

You could do it with the code below.

bX,bY is the position of the button
bW,bH is width and height.

Code: Select all


screen checkMouse():
   key "mousedown_1" action Jump("checkClick")

---------

label checkClick:
   python:
       mX=renpy.get_mouse_pos()[0]
       mY=renpy.get_mouse_pos()[1]

    if bX<mX<bX+bW and bY<mY<bY+bH:
        --- god things happen ---
   else:
        --- bad things happen ---

    jump gamelabel
One thing you have to watch out for with this time of solution is 'side rollback'. You need to have that turned off or make certain the 'button' never is in the side rollback area.

If by moving you mean jumping from point to point in an instance you will always know bX and bY, so that will not be a problem.

If you mean moving like in a Duck Hunt type game that can be more problematic dependent on how the movement is coded. But that can be handled to.
Else:

viewtopic.php?f=51&t=51263

goldo
Regular
Posts: 127
Joined: Mon Jan 23, 2017 8:23 am
Contact:

Re: Activate button on press (not release)

#4 Post by goldo »

Thanks! It is a kind of duck hunt type game.

Branching to a jump will not be practical as I will have several moving buttons that must keep running at once, but I guess a Function would work just as well.

The only problem is: how can I get the button's coordinates at the time of clicking to make sure the cursor is over it? (the button is being moved around with ease transforms)

User avatar
Per K Grok
Miko-Class Veteran
Posts: 882
Joined: Fri May 18, 2018 1:02 am
Completed: the Ghost Pilot, Sea of Lost Ships, Bubbles and the Pterodactyls, Defenders of Adacan Part 1-3, the Phantom Flyer
itch: per-k-grok
Location: Sverige
Contact:

Re: Activate button on press (not release)

#5 Post by Per K Grok »

goldo wrote: Sun Jul 07, 2019 1:21 pm Thanks! It is a kind of duck hunt type game.

Branching to a jump will not be practical as I will have several moving buttons that must keep running at once, but I guess a Function would work just as well.

The only problem is: how can I get the button's coordinates at the time of clicking to make sure the cursor is over it? (the button is being moved around with ease transforms)
I did a duck hunt type game a while back in ren'py (with a bit of extra pygame)

viewtopic.php?f=11&t=51931&p=496272&hil ... ok#p496272

What I did was that I created a small loop in the script in which I updated the position of the ducks (or pterodactyls in my case). I also checked for hits in that loop (using pygame's keydown). So I always was updated on the position of each hitbox.

Code: Select all


label timerlab:
    $renpy.pause(delay=0.02, hard=True)


label runner:
    python:
        mx, my = pygame.mouse.get_pos()
        
    --- check hit ---
        
    --- update position of 'ducks' ---
        
   --- update and show 'hunter' (direction the weapon is pointed)---
   
   jump timerlab     

I rendered the 'ducks' in a screen. If I remember correctly because they where animated images and the loop interfered with the animation.

An admittedly hacky solution. But it worked for me.

Thanks for mentioning functions. That suddenly made me realize that a function could be the solution to a completely different problem I have struggled to find a good solution to. :)

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3792
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Activate button on press (not release)

#6 Post by Imperf3kt »

You could always use imagebuttons, you can move those and they can be clicked on even while moving with no need to track position.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

goldo
Regular
Posts: 127
Joined: Mon Jan 23, 2017 8:23 am
Contact:

Re: Activate button on press (not release)

#7 Post by goldo »

Imperf3kt wrote: Sun Jul 07, 2019 5:24 pm You could always use imagebuttons, you can move those and they can be clicked on even while moving with no need to track position.
Yeah but so do normal buttons, I am back to my original problem which is how to click them when the button is pressed, not released. I am thinking I might look in a different direction if I can't get it to work. I'll keep you updated on the issue.

goldo
Regular
Posts: 127
Joined: Mon Jan 23, 2017 8:23 am
Contact:

Re: Activate button on press (not release)

#8 Post by goldo »

Hi guys, still trying to work on a solution.

Do you know if it's possible to create a renpy button as an object and store it as a variable? Then, could I ask that object for its coordinates at a point in time?

The logic would be like:

Code: Select all


buttons = [button1, button2, ...]

while running:
    --- make buttons move ---
    --- checks for mouse button down ---
    
    if mouse button down:
        --- ask for cursor coordinates ---
        for button in buttons:
            --- if cursor coordinates == button coordinates ---
            --- CLICK HAPPENS ---
            break
    

goldo
Regular
Posts: 127
Joined: Mon Jan 23, 2017 8:23 am
Contact:

Re: Activate button on press (not release)

#9 Post by goldo »

Hi guys,

I'm happy to report I found a way, it's actually surprisingly easy.

Before running the mini-game, I use this code to change button behavior:

Code: Select all

    $ config.keymap['button_ignore'] = []
    $ config.keymap['button_select'] = ['mousedown_1', 'K_RETURN', 'K_KP_ENTER', 'K_SELECT']
Then, after the mini-game is over, I use this to go back to regular button behavior:

Code: Select all

    $ config.keymap['button_ignore'] = ['mousedown_1']
    $ config.keymap['button_select'] = ['mouseup_1', 'K_RETURN', 'K_KP_ENTER', 'K_SELECT']
While changing config values mid-game is not necessarily a good idea, this doesn't seem to cause any negative side effects, so it works for my purposes.

Post Reply

Who is online

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