Python_callback triggering jumps... help!

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
8bitRainbow
Newbie
Posts: 19
Joined: Tue Mar 29, 2016 9:21 pm
Contact:

Python_callback triggering jumps... help!

#1 Post by 8bitRainbow » Wed Nov 22, 2017 4:36 am

I have been at this for 2 days, searched everything I could think of on the forums and tried every result that came close with no success. Hoping someone can help me crack this code.

My intent is to have a continuous check on an NPC's stress level which will lead to an automatic jump to a predetermined "freak out" label if their stress is too high. I've been able to get the python_callback to work for keeping track of stat updates behind the scenes but creating a label jump is stumping me.

Here's my initial method under the NPC class I've created, the one being checked:

Code: Select all

        def jumpcheck(self):
            if self.stress > 2 and self.flight > self.fight:
                renpy.jump(self_leaves)
            elif self.stress > 2 and self.fight > self.flight:
                renpy.jump(self_explodes)
            else:
                renpy.notify("That didn't work")
Here is where the callback takes place (in my test example I'm using NPC "m"):

Code: Select all

    def run_jumpcheck():
        try:
            b1.jumpcheck()
            d.jumpcheck()
            m.jumpcheck()
        except:
            renpy.notify("Nothing's working")
        
    config.python_callbacks.append(run_jumpcheck)
I test this by setting up a series of choices that increase m's stress (adding to either fight or flight mode depending on the stress type). Once m.stress is above 2, I want an automatic jump to take us to a scene of her either exploding (fight) or leaving (flight).

For a while my renpy.notify was showing "Nothing's working" the entire trial, but I've gotten it to a point where it'll show "That didn't work" for the first two choices (where stress is less than 3) and then switch to "Nothing's working" after that. So I know it's trying to work and I can't figure out why it's backing out once the jump becomes possible.

User avatar
Remix
Veteran
Posts: 352
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet
Projects: An un-named 3d anime based trainer game
Contact:

Re: Python_callback triggering jumps... help!

#2 Post by Remix » Wed Nov 22, 2017 5:22 am

How and where are self_leaves and self_explodes defined?
Have you tried the conditional tests with notify rather than jump?

User avatar
8bitRainbow
Newbie
Posts: 19
Joined: Tue Mar 29, 2016 9:21 pm
Contact:

Re: Python_callback triggering jumps... help!

#3 Post by 8bitRainbow » Thu Nov 23, 2017 12:15 am

In the instance of the desired jump for my test example, the labels after start in the game are m_leaves and m_explodes. I'm not sure if I set it up correctly because I'm still learning but my aim was to pass NPC "m" into the name of the label while the callback is checking m.jumpcheck().

Likewise, if NPC "d" or "b1" were at the necessary level of flight stress to pass as true, the automatic jump would be to "d_leaves" or "b1_leaves", with labels already set up for both occurrences.


For your second question, do you mean having it notify me if the conditions are met rather than trying to jump? So far I've only started using notify to let me know when things are failing.

**Update: Ooh, okay, I just tried replacing the jumps with renpy.notify to see if the code was even getting that far and it is! So that's great news, that must mean I'm just not coding the jump correctly.

User avatar
Remix
Veteran
Posts: 352
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet
Projects: An un-named 3d anime based trainer game
Contact:

Re: Python_callback triggering jumps... help!

#4 Post by Remix » Thu Nov 23, 2017 8:31 am

I would strongly suggest setting .leaves and .explodes as attributes of the character classes rather than as global/local variables, then within the method you could use self.leaves and it would evaluate to whatever string that instance held.
This is why I asked about 'self_leaves' which I presume you want to magically transform suddenly into 'b_leaves' if the class instance global variable name happens to be b... (that can be done by iterating the vars() global, testing each versus self and taking the key... who wants to bring their game to a crawl just because they need to iterate a few thousand variables though... hence, no sample posted)

Basically you want something of the fashion:
b = SomeClass( name="Bob", leaves="a_leave_label", explodes="explode_label", ... etc )
have the class set those parameters as attributes ... self.leaves = leaves
then within the method access them as self.leaves etc (self.leaves would then be "a_leave_label" )

User avatar
8bitRainbow
Newbie
Posts: 19
Joined: Tue Mar 29, 2016 9:21 pm
Contact:

Re: Python_callback triggering jumps... help!

#5 Post by 8bitRainbow » Thu Nov 23, 2017 5:30 pm

I think I understand most of that and it sounds like a good option, only how to I activate the jump to label? While testing my code some more, I attempted to set up a non-variable label - just a standard renpy.jump(m_leaves) and it failed. When I had notify as the end result, I was notified that everything works up to that point. If a jump is there in its place, the jump should be reached. Instead it makes everything stop working.

I've seen examples in this forum of jumps happening from within python functions so I have no idea why my version isn't working.

User avatar
Remix
Veteran
Posts: 352
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet
Projects: An un-named 3d anime based trainer game
Contact:

Re: Python_callback triggering jumps... help!

#6 Post by Remix » Fri Nov 24, 2017 1:30 am

The syntax is renpy.jump( "label_name_as_quoted_string" ) as far as I remember

Without quotes it would be looking for a global variable with that name

User avatar
8bitRainbow
Newbie
Posts: 19
Joined: Tue Mar 29, 2016 9:21 pm
Contact:

Re: Python_callback triggering jumps... help!

#7 Post by 8bitRainbow » Fri Nov 24, 2017 3:30 pm

I finally figured it out. Had tried it quoted and not, jump and call, every which way with no success. But I just took the "try, except" element out of it and brought the main function out of the NPC class system, set it up directly with the NPC "m" and it worked! I guess it was having trouble passing an NPC in from self.

Here is the working code:

Code: Select all

def m_jumpcheck():
        if m.stress > 4 and m.flight > m.fight:
            renpy.jump("m_leaves")
        elif m.stress > 4 and m.fight > m.flight:
            renpy.jump("m_explodes")
        else:
            pass
            
            
    config.python_callbacks.append(m_jumpcheck)
Simple is beautiful lol

Post Reply

Who is online

Users browsing this forum: Donmai