Jump leads to NameError and mis-routing issue

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
verysunshine
Veteran
Posts: 339
Joined: Wed Sep 24, 2014 5:03 pm
Organization: Wild Rose Interactive
Contact:

Jump leads to NameError and mis-routing issue

#1 Post by verysunshine »

This section of code only seems to go to the correct place part of the time. It has four possible outcomes:

One: The input doesn't match any verbs, so jump to noVerb. This goes to the correct label, but I get a nameError.

Two: The input matches a verb, but it doesn't match a noun, so jump to noNoun. This goes to badCombo instead.

Three: There are matches for the verb and the noun, but there's no label with that name, so jump to badCombo. This also has a nameError.

Four: The inputs are valid and there is a label. This works without issue.

Code: Select all

init python:
    verbcount = 0
    nouncount = 0
    getJump = ""
    verbList = ["talk", "wait", "dance"]
    nounList = ["sophia", "ground"]
    knownVerbs = []
    knownNouns = []
    cknownVerbs = len(knownVerbs)
    cknownNouns = len(knownNouns)
    cnounList = len(nounList)
    cverbList = len(verbList)

label start:
    
    python:
        getJump = ""
        verbcount = 0
        nouncount = 0
        
        textinput = renpy.input(prompt='What should I do?', default='', allow=None, exclude='{}', length=30, with_none=None, pixel_width=None)
        textinput = textinput.lower()
        for verb in verbList:
            if verb in textinput:
                
                if verb not in knownVerbs:
                    knownVerbs.append(verb)
                break
  
            else:
                verbcount = verbcount + 1
                if verbcount > len(nounList):
                    renpy.jump(noVerb)
                
        for noun in nounList:
            if noun in textinput:
                if noun not in knownNouns:
                    knownNouns.append(noun)
                getJump = verb + "_" + noun
                break
            else:
                nouncount = nouncount + 1
                if nouncount > len(nounList):
                    renpy.jump(noNoun)
                
        if renpy.has_label(getJump):
            renpy.jump(getJump)
        else:
            renpy.jump(badCombo)
                  
label noVerb:
    "I become lost in my own thoughts and anxiety. What was I trying to do?"
    
    return
label noNoun:
    "I want to [verb], but my mind goes blank when I try to think why."
    return
label badCombo:
    "I can feel my mind crumbling in on itself."
    return
    
label talk_sophia:
    m "Hey, Sophia."
    return
label talk_ground:
    "I can't bring myself to look at her. I stare at the ground."
    m "Sophia..."
    return
As this is for a game jam, I'd appreciate it if you could explain the problems with words instead of just giving code.

I know I should be using "call" instead of "jump". I'll change it back once this bug is fixed.

Build the basics first, then add all the fun bits.

Please check out my games on my itch.io page!

User avatar
gas
Miko-Class Veteran
Posts: 842
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: Jump leads to NameError and mis-routing issue

#2 Post by gas »

I'll go differently, splitting the text and using temporary variables to store a flag for matching.
If the verb is there, set the verb flag to True. The same for subject.
Then finally use conditionals to decide where to jump.
Your progressive check method is prone to false positives.
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll not be notified about. << now red so probably you'll see it.

10 ? "RENPY"
20 GOTO 10

RUN

verysunshine
Veteran
Posts: 339
Joined: Wed Sep 24, 2014 5:03 pm
Organization: Wild Rose Interactive
Contact:

Re: Jump leads to NameError and mis-routing issue

#3 Post by verysunshine »

So I split the text up, presumably at the spaces.

If the first item in the split text matches a word in the verb list, we set a flag to true and move on. If the item doesn't, give an error.

Then, we see if the second item in the split text matches a verb, with the same process.

Once we have both, we put them together and see if it's a label. If it is, move on. Otherwise, give a different error.

It seems like setting a flag to true would result in the label being the same for every valid input.

Build the basics first, then add all the fun bits.

Please check out my games on my itch.io page!

verysunshine
Veteran
Posts: 339
Joined: Wed Sep 24, 2014 5:03 pm
Organization: Wild Rose Interactive
Contact:

Re: Jump leads to NameError and mis-routing issue

#4 Post by verysunshine »

The reason I had name exception errors is because renpy.jump and renpy.call expect strings or variables, and strings need to be in quotation marks. This means "renpy.jump(noVerb)" should be "renpy.jump("noVerb")". Thanks to renpytom on the Discord for pointing that out.

The code does have false positives if the player inputs nothing. Why is that?

Build the basics first, then add all the fun bits.

Please check out my games on my itch.io page!

Crazy Li
Regular
Posts: 113
Joined: Fri Jan 03, 2014 3:35 pm
Contact:

Re: Jump leads to NameError and mis-routing issue

#5 Post by Crazy Li »

Where does it go if they input nothing? Does it perhaps push to noVerb? If there's nothing stopping your code from proceeding, it'll just keep going. The presence of a label doesn't block you out or anything. I'm assuming the non entry just skips through what you have and goes to the next piece of code available, which would likely be your first label. If that's not what it's doing, then obviously it's something else, but without more info, I can't really make much of a guess.

verysunshine
Veteran
Posts: 339
Joined: Wed Sep 24, 2014 5:03 pm
Organization: Wild Rose Interactive
Contact:

Re: Jump leads to NameError and mis-routing issue

#6 Post by verysunshine »

The final code ended up looking like this:

Code: Select all

label maingame:
    
    python:
        getJump = ""
        verbcount = 0
        nouncount = 0
        
        textinput = renpy.input(prompt='What should I do?', default='', allow=None, exclude='{}', length=30, with_none=None, pixel_width=None)
        textinput = textinput.lower()
        for verb in verbList:
            if verb in textinput:
                
                if verb not in knownVerbs:
                    knownVerbs.append(verb)
                break
  
            else:
                verbcount = verbcount + 1
                if verbcount > len(nounList):
                    renpy.call("noVerb")
                
        for noun in nounList:
            if noun in textinput:
                if noun not in knownNouns:
                    knownNouns.append(noun)
                getJump = verb + "_" + noun
                if renpy.has_label(getJump):
                    renpy.call(getJump)
                else:
                    renpy.call("noMatch")
            else:
                nouncount = nouncount + 1
                if nouncount > len(nounList):
                    renpy.call("noVerb")
                    
    return #this ends the game
When a player inputs a verb and no noun, the game goes to the return function at the end of this statement and back to the main menu. This happens if nothing is input.

When there is a noun and no verb, the game uses the last verb in the list, which is "dance".

If a player inputs an incorrect noun or verb, the code correctly moves to "noVerb".

Build the basics first, then add all the fun bits.

Please check out my games on my itch.io page!

Crazy Li
Regular
Posts: 113
Joined: Fri Jan 03, 2014 3:35 pm
Contact:

Re: Jump leads to NameError and mis-routing issue

#7 Post by Crazy Li »

Clearly code is being skipped and it's jumping to the first thing that it's not blocked out of. In the case of the first examples, that's just a return, since nothing else is being found as valid. In the second example, the issue seems a bit more confusing. I feel like your code is sending it to the jump for verb + noun before even checking if there is a verb when it finds that there is a noun. Maybe the order of checks is wrong?

Post Reply

Who is online

Users browsing this forum: Alex