Making a button that calls a method of a class?

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
Zherot
Regular
Posts: 91
Joined: Tue Aug 08, 2017 9:10 pm
Contact:

Making a button that calls a method of a class?

#1 Post by Zherot »

I made a class called Time that has a method that advances the time, but i don't know how to make a button that just does the method instead of having to create a label and then actually call the method to advance time.

I just want to click the button and the method is executed... but unfortunatelly i don't know how, right now i have the line of code like this:

Code: Select all

textbutton "Advance time 1 hour" action Time.one_hour_advance()
Time is an instance of my Time class and one_hour_advance() is my method inside that class, this gives me an error when i press the button because clearly im doing something wrong here.

Can someone give me a bit of help with this?

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Making a button that calls a method of a class?

#2 Post by kivik »

This is what you want: https://www.renpy.org/doc/html/screen_a ... l#Function

That page lists all the actions you can use from any screen inputs, nothing else besides what's listed on that page would work.

Unfortunately due to lack of examples, it can be confusing to use the Function() action, in your case it should be:

Code: Select all

textbutton "Advance time 1 hour" action Function(Time.one_hour_advance)
You have to take out the brackets basically. Meanwhile if you have arguments for your function, you can pass them as arguments from the second argument onwards. I can't remember if you need to name your arguments when you pass them, but I tend to do them out of habit for easy reference later.

Code: Select all

textbutton "Advance time 1 hour" action Function(Time.one_hour_advance, argument=value, argument2=value2)

Zherot
Regular
Posts: 91
Joined: Tue Aug 08, 2017 9:10 pm
Contact:

Re: Making a button that calls a method of a class?

#3 Post by Zherot »

Checking you answer later, from a first quick glance it seems it is really what i wanted, i'll try to implement this later, thank you.

Zherot
Regular
Posts: 91
Joined: Tue Aug 08, 2017 9:10 pm
Contact:

Re: Making a button that calls a method of a class?

#4 Post by Zherot »

I tried using the code line but for some weird reason the game is not doing whatever the class method is supposed to do but instead jumps to another label even though i never indicated to do that in the code.

Zherot
Regular
Posts: 91
Joined: Tue Aug 08, 2017 9:10 pm
Contact:

Re: Making a button that calls a method of a class?

#5 Post by Zherot »

I just want to increase the time and not jump to another label or anything or do anything at all, is that even possible?

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Making a button that calls a method of a class?

#6 Post by kivik »

We're going to have to see more code to figure out what's going on. Sounds like something else is causing it to jump to another label.

Post the code for your screen and your class.

Zherot
Regular
Posts: 91
Joined: Tue Aug 08, 2017 9:10 pm
Contact:

Re: Making a button that calls a method of a class?

#7 Post by Zherot »

False alarm i was so stupid... i was calling the other thing myself, i deleted it and now it is not jumping to that label... i had a call to a screen and i wasn't even aware of it hahaha.

Still my function does not increase the time i'll do more checking.

Zherot
Regular
Posts: 91
Joined: Tue Aug 08, 2017 9:10 pm
Contact:

Re: Making a button that calls a method of a class?

#8 Post by Zherot »

Having the code like this now just jumps to the main menu when i press the Advance time button:

Code: Select all

screen menu:

    vbox:
        xalign 1.0
        yalign 0.5
        
        textbutton "Home entrance" action Jump("home1")
        textbutton "Testing Classes" action Jump("testing")

    vbox:
        xalign 0.0
        yalign 0.5
        textbutton "Advance time 1 hour" action Function(Time.one_hour_advance)

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Making a button that calls a method of a class?

#9 Post by kivik »

What's your main code block that calls the screen? Does it do anything else after the calling the screen?

Zherot
Regular
Posts: 91
Joined: Tue Aug 08, 2017 9:10 pm
Contact:

Re: Making a button that calls a method of a class?

#10 Post by Zherot »

kivik wrote: Mon May 21, 2018 6:44 pm What's your main code block that calls the screen? Does it do anything else after the calling the screen?
Not right now it just calls the screen and that's it, i don't reall want to do anything but increase the time.

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Making a button that calls a method of a class?

#11 Post by kivik »

Well if it doesn't do anything else after calling the screen - then it'll return to the main menu after...

You need to add something like a narration line to stop the game from ending:

Code: Select all

label start:
    "[Time.time]" # or whatever the attribute is to get the current time
    call screen menu
    "[Time.time]" # check if the time has increased
    return

Zherot
Regular
Posts: 91
Joined: Tue Aug 08, 2017 9:10 pm
Contact:

Re: Making a button that calls a method of a class?

#12 Post by Zherot »

kivik wrote: Mon May 21, 2018 6:56 pm Well if it doesn't do anything else after calling the screen - then it'll return to the main menu after...

You need to add something like a narration line to stop the game from ending:

Code: Select all

label start:
    "[Time.time]" # or whatever the attribute is to get the current time
    call screen menu
    "[Time.time]" # check if the time has increased
    return
Hmmm, how can i just add a button that increases the time and just does that without the need to do anything like that?

Zherot
Regular
Posts: 91
Joined: Tue Aug 08, 2017 9:10 pm
Contact:

Re: Making a button that calls a method of a class?

#13 Post by Zherot »

I checked the way you suggested and it is actually working but i need to stay in the game after increasing the time and not just going to the main menu.

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Making a button that calls a method of a class?

#14 Post by kivik »

You just need to add content to the game, put whatever needs to happen next in the game - the time has advanced, you just need to write the rest of the game.

Zherot
Regular
Posts: 91
Joined: Tue Aug 08, 2017 9:10 pm
Contact:

Re: Making a button that calls a method of a class?

#15 Post by Zherot »

I just surrendered and made a label for it, is the most logical answer to be honest and now that i tested it is what i wanted:

Code: Select all

label time_increase:

    python:
        Time.one_hour_advance()

    "[Time.hour]"
    "[Time.hourText]"
    "[Time.day]"
    "[Time.dayText]"
    call screen kitchen_menu
I can indefinitely increase the time and test my class like this.

Post Reply

Who is online

Users browsing this forum: Amazon [Bot], MisterPinetree