Weird inconsistent error when displaying information with square brackets

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
Draite
Regular
Posts: 26
Joined: Tue Oct 08, 2019 9:10 pm
Contact:

Weird inconsistent error when displaying information with square brackets

#1 Post by Draite »

I just ran into a weird issue that I don't understand. I've been using "[pc.profile[Name]]" to display the player name during dialogue which works fine. But then one instance of it started throwing this error

Code: Select all

File "game/scenes_vergesseneburg.rpy", line 556, in script
    player "\"Hi. I'm [pc.profile[Name]], I'm a little new here. What do you have?\""
NameError: name 'Name' is not defined
This throws the error

Code: Select all

player "\"Hi. I'm [pc.profile[Name]]. I'm a little new here. What do you have?\""
However earlier in the game, this works fine

Code: Select all

player "\"You can call me [pc.profile[Name]]. It's not an exciting story, I was simply in the wrong place at the wrong time. I was accused of murder and that was enough for the Legion. Within moments of the accusation I was put in chains. A sham of a trial the day after and now I'm here.\""
Am I missing something? Or is this a bug with Ren'Py? I'm really just baffled at this point.

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: Weird inconsistent error when displaying information with square brackets

#2 Post by hell_oh_world »

i tend to avoid interpolating complex variables such as dicts, i usually assign it first then interpolate it. i also use objects for such cases... depends on what you want.

Code: Select all

$ name = pc.profile["Name"]
player "\"Hi. I'm [name]. I'm a little new here. What do you have?\""

Draite
Regular
Posts: 26
Joined: Tue Oct 08, 2019 9:10 pm
Contact:

Re: Weird inconsistent error when displaying information with square brackets

#3 Post by Draite »

That doesn't explain why it works in one case and not the other though. I don't want to dig through every script and change everything without understanding why this doesn't work, and why that would fix it.

rames44
Veteran
Posts: 233
Joined: Sun May 29, 2016 4:38 pm
Contact:

Re: Weird inconsistent error when displaying information with square brackets

#4 Post by rames44 »

This is probably a Ren’py parsing quirk. Given what you’ve put in, Name _should_ be treated as a variable, not a string.

But this may not, effectively, be valid Ren’py syntax - there are limits to what it’s designed to interpolate. So the fact that it worked at all may be the bug.

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Weird inconsistent error when displaying information with square brackets

#5 Post by PyTom »

Can I have a full traceback?

In this thing, Name should be being processed as a string - inside the outermost brackets, it's used as a dictionary key. My guess is that something else is processing your string, such that pc.profile is not being interpolated, and since it just sees [Name], Ren'Py is trying to get at that variable.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

Draite
Regular
Posts: 26
Joined: Tue Oct 08, 2019 9:10 pm
Contact:

Re: Weird inconsistent error when displaying information with square brackets

#6 Post by Draite »

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 984, in script call
    call scene_battle_setup pass (_en = _enemy) from _call_scene_battle_setup
  File "game/script.rpy", line 1103, in script call
    call scene_battle_loop(_weather_active,_surprise) from _call_scene_battle_loop
  File "game/script.rpy", line 1317, in script call
    call scene_battle_end pass (_e) from _call_scene_battle_end # from _call_scene_battle_end
  File "game/encounter_forest.rpy", line 75, in script call
    call scene_battle_setup pass (_en = _enemy,_ambush = True) from _call_scene_battle_setup_6
  File "game/script.rpy", line 1103, in script call
    call scene_battle_loop(_weather_active,_surprise) from _call_scene_battle_loop
  File "game/script.rpy", line 1317, in script call
    call scene_battle_end pass (_e) from _call_scene_battle_end # from _call_scene_battle_end
  File "game/encounter_forest.rpy", line 30, in script call
    call scene_battle_setup pass (_en = _enemy,_con = chooseCondition(),_ambush = False,_eq_right = _eq_right,_eq_acc = _eq_acc) from _call_scene_battle_setup_3
  File "game/script.rpy", line 1103, in script call
    call scene_battle_loop(_weather_active,_surprise) from _call_scene_battle_loop
  File "game/script.rpy", line 1317, in script call
    call scene_battle_end pass (_e) from _call_scene_battle_end # from _call_scene_battle_end
  File "game/encounter_forest.rpy", line 55, in script call
    call scene_battle_setup pass (_en = _enemy,_con = chooseCondition(),_ambush = False) from _call_scene_battle_setup_11
  File "game/script.rpy", line 1103, in script call
    call scene_battle_loop(_weather_active,_surprise) from _call_scene_battle_loop
  File "game/script.rpy", line 1317, in script call
    call scene_battle_end pass (_e) from _call_scene_battle_end # from _call_scene_battle_end
  File "game/encounter_forest.rpy", line 30, in script call
    call scene_battle_setup pass (_en = _enemy,_con = chooseCondition(),_ambush = False,_eq_right = _eq_right,_eq_acc = _eq_acc) from _call_scene_battle_setup_3
  File "game/script.rpy", line 1103, in script call
    call scene_battle_loop(_weather_active,_surprise) from _call_scene_battle_loop
  File "game/script.rpy", line 1317, in script call
    call scene_battle_end pass (_e) from _call_scene_battle_end # from _call_scene_battle_end
  File "game/scenes_vergesseneburg.rpy", line 556, in script
    player "\"Hi. I'm [pc.profile[Name]]. I'm a little new here. What do you have?\""
NameError: name 'Name' is not defined

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 984, in script call
    call scene_battle_setup pass (_en = _enemy) from _call_scene_battle_setup
  File "game/script.rpy", line 1103, in script call
    call scene_battle_loop(_weather_active,_surprise) from _call_scene_battle_loop
  File "game/script.rpy", line 1317, in script call
    call scene_battle_end pass (_e) from _call_scene_battle_end # from _call_scene_battle_end
  File "game/encounter_forest.rpy", line 75, in script call
    call scene_battle_setup pass (_en = _enemy,_ambush = True) from _call_scene_battle_setup_6
  File "game/script.rpy", line 1103, in script call
    call scene_battle_loop(_weather_active,_surprise) from _call_scene_battle_loop
  File "game/script.rpy", line 1317, in script call
    call scene_battle_end pass (_e) from _call_scene_battle_end # from _call_scene_battle_end
  File "game/encounter_forest.rpy", line 30, in script call
    call scene_battle_setup pass (_en = _enemy,_con = chooseCondition(),_ambush = False,_eq_right = _eq_right,_eq_acc = _eq_acc) from _call_scene_battle_setup_3
  File "game/script.rpy", line 1103, in script call
    call scene_battle_loop(_weather_active,_surprise) from _call_scene_battle_loop
  File "game/script.rpy", line 1317, in script call
    call scene_battle_end pass (_e) from _call_scene_battle_end # from _call_scene_battle_end
  File "game/encounter_forest.rpy", line 55, in script call
    call scene_battle_setup pass (_en = _enemy,_con = chooseCondition(),_ambush = False) from _call_scene_battle_setup_11
  File "game/script.rpy", line 1103, in script call
    call scene_battle_loop(_weather_active,_surprise) from _call_scene_battle_loop
  File "game/script.rpy", line 1317, in script call
    call scene_battle_end pass (_e) from _call_scene_battle_end # from _call_scene_battle_end
  File "game/encounter_forest.rpy", line 30, in script call
    call scene_battle_setup pass (_en = _enemy,_con = chooseCondition(),_ambush = False,_eq_right = _eq_right,_eq_acc = _eq_acc) from _call_scene_battle_setup_3
  File "game/script.rpy", line 1103, in script call
    call scene_battle_loop(_weather_active,_surprise) from _call_scene_battle_loop
  File "game/script.rpy", line 1317, in script call
    call scene_battle_end pass (_e) from _call_scene_battle_end # from _call_scene_battle_end
  File "game/scenes_vergesseneburg.rpy", line 556, in script
    player "\"Hi. I'm [pc.profile[Name]]. I'm a little new here. What do you have?\""
  File "C:\Users\User\Downloads\renpy-7.3.2-sdk\renpy\ast.py", line 706, in execute
    renpy.exports.say(who, what, *args, **kwargs)
  File "C:\Users\User\Downloads\renpy-7.3.2-sdk\renpy\exports.py", line 1336, in say
    who(what, *args, **kwargs)
  File "C:\Users\User\Downloads\renpy-7.3.2-sdk\renpy\character.py", line 1112, in __call__
    who = renpy.python.py_eval(who)
  File "C:\Users\User\Downloads\renpy-7.3.2-sdk\renpy\python.py", line 2035, in py_eval
    return py_eval_bytecode(code, globals, locals)
  File "C:\Users\User\Downloads\renpy-7.3.2-sdk\renpy\python.py", line 2028, in py_eval_bytecode
    return eval(bytecode, globals, locals)
  File "<none>", line 1, in <module>
NameError: name 'Name' is not defined

Windows-8-6.2.9200
Ren'Py 7.3.2.320
Love Monsters and Order 1.4d
Mon Jun 08 13:34:16 2020
I don't know why the traceback always seems to reference the battle loop. But this is it. Thanks for taking the time to look at this :)

Draite
Regular
Posts: 26
Joined: Tue Oct 08, 2019 9:10 pm
Contact:

Re: Weird inconsistent error when displaying information with square brackets

#7 Post by Draite »

Now it's not working anywhere and I don't know what I've changed or done.
I've tried adding a new variable like hell_oh_world suggested

Code: Select all

default pc_name = pc.profile["Name"]
but it still gives the error that Name is undefined.

Code: Select all

class Mob:
        def __init__(self, hp, morale, stamina, combat, survival, acumen, glamour,name,race,gender,sign):
	        ## General purpose information with little or no mechanical influence
        	self.profile = {'Name' : name, 'Race' : race, 'Gender' : gender}
Is there any reason this could be happening and/or a way I can fix it. If the explanation is just that RenPy is inconsistent that's fine. I just need a solution at this point.

Draite
Regular
Posts: 26
Joined: Tue Oct 08, 2019 9:10 pm
Contact:

Re: Weird inconsistent error when displaying information with square brackets

#8 Post by Draite »

This works

Code: Select all

"You're a [pc.profile[Gender]] [pc.profile[Race]], named [pc_name]. Does that sound right?"
But this isn't working

Code: Select all

File "game/scenes_last_drop.rpy", line 396, in script
    player "\"I'm [pc_name], I'm nothing special really. The Legion arrested me for whatever crimes they felt I committed and now I'm here. I'm actually surprised we're just allowed to roam the island and that they allow you to run this... business?\""
NameError: name 'Name' is not defined
I really can't figure out what's going on

rames44
Veteran
Posts: 233
Joined: Sun May 29, 2016 4:38 pm
Contact:

Re: Weird inconsistent error when displaying information with square brackets

#9 Post by rames44 »

Your best bet is to put together the absolutely minimum script/game you can that demonstrates the problem and give it to PyTom With that, he should be able to debug it. But having a (non) working sample takes lots of guesswork out of that process for him.

Draite
Regular
Posts: 26
Joined: Tue Oct 08, 2019 9:10 pm
Contact:

Re: Weird inconsistent error when displaying information with square brackets

#10 Post by Draite »

Okay, so as I try and break this down piece by piece it's just getting worse. The barest minimum amount of coding I'm able to break this down to and get an error is:

Code: Select all

default pc = Mob(50,50,2,40,40,40,40,"Player","Human","Male",star.sword)
define player = Character(pc.profile['Name'], dynamic = True)

Code: Select all

def __init__(self, hp, morale, stamina, combat, survival, acumen, glamour,name,race,gender,sign):
            self.hp = hp
            self.max_hp = hp
            self.morale= 0
            self.max_morale= morale
            self.stamina = stamina
            self.max_stamina = stamina
            self.survival = survival
            self.combat = combat
            self.acumen = acumen
            self.glamour = glamour
            self.sign = sign
            self.level = 1

            ## Elemental damage resistance lists take dmg_type objects
            self.dmg_resist = []
            self.dmg_immune = []
            self.dmg_vulnerability = []

            self.buff = {
            # [Active,Minutes,Perma,Sprite]
            "Regenerate" : [False,0,False],
            "Healthy" : [False,0,False],
            "Sturdy" : [False,0,False],
            "Poison" : [False,0,False],
            "Curse" : [False,0,False],
            "Wet" : [False,0,False], # Wet characters are more vulnerable to cold damage and lightning damage but resist fire damage,
            "Hot" : [False,0,False], # Hot characters are more vulnerable to fire damage but resist cold
            "Slow" : [False,0,False],
            "Fast" : [False,0,False],
            "--" : [False,0,False]
            }
            ## General purpose information with little or no mechanical influence
            self.profile = {'Name' : name, 'Race' : race, 'Gender' : gender}
            ## Weapons
            self.hands = {'Right' : None, 'Left' : None}
            ## Equipment
            self.equipment = {'Chest' : None, 'Legs' : None, 'Acc' : None}
            ## Skills
            self.skills = [skill_strike, None, None, None, None,None]
Which gives this error

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 95, in script
    define player = Character(pc.profile['Name'], dynamic = True)
  File "game/script.rpy", line 95, in <module>
    define player = Character(pc.profile['Name'], dynamic = True)
NameError: name 'pc' is not defined

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 95, in script
    define player = Character(pc.profile['Name'], dynamic = True)
  File "C:\Users\User\Downloads\renpy-7.3.2-sdk\renpy\ast.py", line 2106, in execute
    value = renpy.python.py_eval_bytecode(self.code.bytecode)
  File "C:\Users\User\Downloads\renpy-7.3.2-sdk\renpy\python.py", line 2028, in py_eval_bytecode
    return eval(bytecode, globals, locals)
  File "game/script.rpy", line 95, in <module>
    define player = Character(pc.profile['Name'], dynamic = True)
NameError: name 'pc' is not defined

Windows-8-6.2.9200
Ren'Py 7.3.2.320
Love Monsters and Order 1.4d
Wed Jun 10 12:30:26 2020

User avatar
MaydohMaydoh
Regular
Posts: 165
Joined: Mon Jul 09, 2018 5:49 am
Projects: Fuwa Fuwa Panic
Tumblr: maydohmaydoh
Location: The Satellite of Love
Contact:

Re: Weird inconsistent error when displaying information with square brackets

#11 Post by MaydohMaydoh »

Renpy creates defined variables during the init stage and defaulted variables when the game starts.

Also from what I can see, your problem is caused by the character definition, not the interpolation. Dynamic shouldn't be set to True.

Draite
Regular
Posts: 26
Joined: Tue Oct 08, 2019 9:10 pm
Contact:

Re: Weird inconsistent error when displaying information with square brackets

#12 Post by Draite »

Can you show me how it should be written?

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: Weird inconsistent error when displaying information with square brackets

#13 Post by hell_oh_world »

Maydorah's statement made sense. When you use dynamic character you don't actually refer to the variable itself, you need to specify the name as the variable name and not the variable itself.

Code: Select all

default name = ""
define e = Character ("name", dynamic=True)
label start:
  $ name ="Eileen"
  e "I'm Eileen, nice to meet you."
Unless, pc.player["Name"] returns a string for the variable name to use and that variable is default then it should work.

Post Reply

Who is online

Users browsing this forum: Google [Bot]