imagebuttons with timer element [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
timdonehy200
Newbie
Posts: 20
Joined: Sun Mar 12, 2017 6:15 am
Deviantart: timdonehy200
Contact:

imagebuttons with timer element [SOLVED]

#1 Post by timdonehy200 » Mon Sep 18, 2017 5:55 am

I'm trying to make a scene where the player can "snoop around", BUT with a limited amount of time to do so.
So basically, a scene with imagebuttons of different areas that can be clicked on and it says "nothing interesting here", "nothing interesting there" until the right area is clicked and the "interesting item" is found. But in the meantime, a timer runs in the background so the player has to be quick. Also, preferably, each "wrong" click leads to a certain amount of extra time lost...
Any help appreciated.
Last edited by timdonehy200 on Thu Sep 21, 2017 5:41 am, edited 1 time in total.

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: imagebuttons with timer element

#2 Post by xela » Mon Sep 18, 2017 6:20 am

Like what we're doing? Support us at:
Image

timdonehy200
Newbie
Posts: 20
Joined: Sun Mar 12, 2017 6:15 am
Deviantart: timdonehy200
Contact:

Re: imagebuttons with timer element

#3 Post by timdonehy200 » Mon Sep 18, 2017 9:47 am

Thanks for the link, however it's a bit more complicated than that. The timer can return an action (too late) when it runs out. But how could I make a click on an imagebutton affect the timer within its own screen?...
In other words, can the "Action" of an imagebutton be to influence the value of the timer and if so, what would be the "Action" code to achieve this?

Edit. my bad, I believe it will be something like action (SetVariable...)

User avatar
DannyGMaster
Regular
Posts: 113
Joined: Fri Sep 02, 2016 11:07 am
Contact:

Re: imagebuttons with timer element

#4 Post by DannyGMaster » Mon Sep 18, 2017 12:14 pm

timdonehy200 wrote:
Mon Sep 18, 2017 9:47 am
I believe it will be something like action (SetVariable...)
SetVariable could work but not for taking out an x time of seconds from the timer, for that, you could make a variable to hold the time left, and write a function to change that variable's value, it could be something like this (NOT tested, probably will need a bit of tweaking):

EDIT: I posted bad code here, I'm erasing it so it doesn't cause any unexpected trouble. Really, don't know what was I thinking :?
Last edited by DannyGMaster on Mon Sep 18, 2017 3:51 pm, edited 1 time in total.
The silent voice within one's heart whispers the most profound wisdom.

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: imagebuttons with timer element

#5 Post by xela » Mon Sep 18, 2017 12:31 pm

SetVariable could work but not for taking out an x time of seconds from the timer, for that, you could make a variable to hold the time left, and write a function to change that variable's value, it could be something like this (NOT tested, probably will need a bit of tweaking):
That is a bad way of doing it that is not very likely to work. It doesn't have to be complicated, show a timer of a separate screen and hide it with your imagebutton action so timer action does not execute. Or use "If" screen action in your timer conditioned off local screen variable, that should work with everything in one single screen. Both should work perfectly fine.
Like what we're doing? Support us at:
Image

timdonehy200
Newbie
Posts: 20
Joined: Sun Mar 12, 2017 6:15 am
Deviantart: timdonehy200
Contact:

Re: imagebuttons with timer element

#6 Post by timdonehy200 » Mon Sep 18, 2017 1:28 pm

Thanks DannyGMaster, I will try this, looks good!
Why are you saying it won't work xela exactly? Sorry, I'm not a very good coder and don't know what an action conditioned off a local variable means exactly, could you please explain?...
Also,to be clear, I don't want the timer to be "hidden" when the wrong object is chosen, but for the timer to "suddenly" decrease by, say one second as in DannyGmaster's example, on top of decreasing linearly from 10s to 0s. So if you click a wrong imagebutton, you really have even less time to continue searching for the item hidden in one of the remaining imagebuttons, that would be my ideal situation...

User avatar
DannyGMaster
Regular
Posts: 113
Joined: Fri Sep 02, 2016 11:07 am
Contact:

Re: imagebuttons with timer element

#7 Post by DannyGMaster » Mon Sep 18, 2017 2:35 pm

xela wrote:
Mon Sep 18, 2017 12:31 pm
That is a bad way of doing it that is not very likely to work.
I know it probably won't work right off the bat, that's why I wrote this:
DannyGMaster wrote:
Mon Sep 18, 2017 12:14 pm
(NOT tested, probably will need a bit of tweaking)
Reading through timdonehy200's description of what he wanted to achieve, I devised an idea of how this system could be implemented, and shared it, but it was not meant to be a final solution, I should have been more clear on that regard, only wanted to share my thoughts on the matter in hope timdonehy200 would figure out a way his desired result could be achieved. It reminded me of those Hidden Object Detective games where you have a time limit to find all the hidden objects, and when you start clicking randomly on the screen, your time decreases as a penalty.

Xela is right though, we can use local screen variables. An external function was needed because if you decrease the timer too much it will eventually become negative, and that could produce an error, I forgot to mention that.

EDIT: Something must have been seriosly wrong with me, posted bad code here too.

I'm reall sorry for this, it was blatantly obvious that this was a bad approach and I should have tested it a lot more than I did, plus I misunderstood much of the way the code involved worked. I shall reflect upon my mistakes, for now once again I'm sorry tc for confusing you, and xela thank you again for taking the time to explaining it to me.
Last edited by DannyGMaster on Thu Sep 21, 2017 8:16 am, edited 2 times in total.
The silent voice within one's heart whispers the most profound wisdom.

timdonehy200
Newbie
Posts: 20
Joined: Sun Mar 12, 2017 6:15 am
Deviantart: timdonehy200
Contact:

Re: imagebuttons with timer element

#8 Post by timdonehy200 » Mon Sep 18, 2017 3:12 pm

Ah yes, it certainly looks easier, even I can understand that.
Thanks a bunch guys!

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: imagebuttons with timer element

#9 Post by xela » Mon Sep 18, 2017 3:14 pm

timdonehy200 wrote:
Mon Sep 18, 2017 1:28 pm
Thanks DannyGMaster, I will try this, looks good!
Why are you saying it won't work xela exactly? Sorry, I'm not a very good coder and don't know what an action conditioned off a local variable means exactly, could you please explain?...
Also,to be clear, I don't want the timer to be "hidden" when the wrong object is chosen, but for the timer to "suddenly" decrease by, say one second as in DannyGmaster's example, on top of decreasing linearly from 10s to 0s. So if you click a wrong imagebutton, you really have even less time to continue searching for the item hidden in one of the remaining imagebuttons, that would be my ideal situation...
Setting up default value in the screen is what I meant with local*

I also did not pick up on timer handling. The reason I did not expect it to work, is that I was not sure if screen would replace the timers as you intend it to... It would require testing.
Like what we're doing? Support us at:
Image

User avatar
DannyGMaster
Regular
Posts: 113
Joined: Fri Sep 02, 2016 11:07 am
Contact:

Re: imagebuttons with timer element

#10 Post by DannyGMaster » Mon Sep 18, 2017 3:21 pm

I edited my post a little, taking into account xela's suggestions.

viewtopic.php?f=8&t=45818&p=465361#p465361

Can't test it right now, but would this work better?
The silent voice within one's heart whispers the most profound wisdom.

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: imagebuttons with timer element

#11 Post by xela » Mon Sep 18, 2017 3:24 pm

DannyGMaster wrote:
Mon Sep 18, 2017 2:35 pm
Reading through timdonehy200's description of what he wanted to achieve, I devised an idea of how this system could be implemented, and shared it, but it was not meant to be a final solution, I should have been more clear on that regard, only wanted to share my thoughts on the matter in hope timdonehy200 would figure out a way his desired result could be achieved.
Right... and I thought it was ok to point out that your implementation is a bad idea. Even if your guess about how Ren'Py would handle onscreen timer was correct, without special checks, clicking those buttons can simply crash the engine. Any advice that can crash the engine should generally be considered bad :?

Edit:
(it looks more precise after your edit :) )
Like what we're doing? Support us at:
Image

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: imagebuttons with timer element

#12 Post by xela » Mon Sep 18, 2017 3:31 pm

Using screen language, this is likely the most accurate way:

Code: Select all

screen peek2():
    default time_left = 10.0

    text "[time_left]" align .5, .5
    
    if time_left <= 0:
        timer .001 action Return() # Whatever action you want to happen when timer runs out!
    else:
        timer .1 repeat True action SetScreenVariable("time_left", time_left-.1)
    
    textbutton "Reduce":
        action SetScreenVariable('time_left', time_left-1)
    
label start:
    call screen peek2
Like what we're doing? Support us at:
Image

User avatar
DannyGMaster
Regular
Posts: 113
Joined: Fri Sep 02, 2016 11:07 am
Contact:

Re: imagebuttons with timer element

#13 Post by DannyGMaster » Mon Sep 18, 2017 3:39 pm

xela wrote:
Mon Sep 18, 2017 3:24 pm
Right... and I thought it was ok to point out that your implementation is a bad idea. Even if your guess about how Ren'Py would handle onscreen timer was correct, without special checks, clicking those buttons can simply crash the engine. Any advice that can crash the engine should generally be considered bad :?

Edit:
(it looks more precise after your edit :) )
You're very right to point it out and I appreciate it, I'm still learning how this things work too, that's why sometimes I can't help myself, but you're absolutely correct, I should not and will not suggest any more code until I have properly and thoroughly tested it myself. Thank you very much for your concern.

Also your example using screen language is likely the better.
The silent voice within one's heart whispers the most profound wisdom.

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: imagebuttons with timer element

#14 Post by xela » Mon Sep 18, 2017 3:59 pm

DannyGMaster wrote:
Mon Sep 18, 2017 2:35 pm

Code: Select all

init python:

    def changeTimer(var, amount):
        var += amount
        
        if var < 0:
            var = 0
            
screen peek2():

    default time_left = 5.0 #This is a local, screen variable

    #This implements the If action too, it decreases 1 second each time the timer repeats, and stops the timer when it reachs 0.
    timer time_left repeat True action If(time_left > 0, true=SetScreenVariable('time_left', time_left - 1.0), false=[Hide('peek2'), Jump('too_slow')])
    
    imagebutton idle "wrong_button1.png" xalign .5 action Function(changeTimer, time_left, -1.0)
Since you're trying to learn new things*

You cannot control screen variable using a function like that. Functions have their own (local) namespace (that means that what happens inside of a function, stays inside of a function :D ). Global variables or objects passed into a a function are the exceptions.

Timer will also crash on 0, not just the negative numbers.
Like what we're doing? Support us at:
Image

User avatar
DannyGMaster
Regular
Posts: 113
Joined: Fri Sep 02, 2016 11:07 am
Contact:

Re: imagebuttons with timer element

#15 Post by DannyGMaster » Mon Sep 18, 2017 4:06 pm

xela wrote:
Mon Sep 18, 2017 3:59 pm
You cannot control screen variable using a function like that. Functions have their own (local) namespace (that means that what happens inside of a function, stays inside of a function :D ). Global variables or objects passed into a a function are the exceptions.
I know this, that's why I had to use a global variable in my previous implementation, I forgot to change the code in that post too, that should be an If with SetVariable action. Anyway that one is too completely wrong.
xela wrote:
Mon Sep 18, 2017 3:59 pm
Timer will also crash on 0, not just the negative numbers.
This, I didn't know. Thank you for the information!
The silent voice within one's heart whispers the most profound wisdom.

Post Reply

Who is online

Users browsing this forum: Google [Bot]