[Solved]String Slicing does not work

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
yawnie
Newbie
Posts: 7
Joined: Mon Jan 15, 2018 11:29 am
Contact:

[Solved]String Slicing does not work

#1 Post by yawnie »

Hi, I'm trying to slice a string but renpy is telling me that g1_name isn't defined -- what's weird is that g1_name does have a value at that point. When screen group1_subjects is called, the user is encouraged to choose a textbutton (for example "HL English Literature") and it changes the value of g1_name. I added "[g1_name]" in the middle just to make sure and it does give me a String value ("HL English Literature"). I want to get the first two letters of g1_name to see if it's HL or SL, but status gives me nothing.

Code: Select all

label choose_subjects:
    label choose_g1:
        call screen group1_subjects
        "[g1_name]"
        default status = gl_name[:2]
        "[status]"

Here is the traceback:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "renpy/common/00start.rpy", line 188, in script
    python:
  File "renpy/common/00start.rpy", line 189, in <module>
    renpy.execute_default_statement(True)
  File "game/script.rpy", line 84, in set_default
    default status = gl_name[:2]
  File "game/script.rpy", line 84, in <module>
    default status = gl_name[:2]
NameError: name 'gl_name' is not defined

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

Full traceback:
  File "/Applications/renpy-6.99.13-sdk 2/renpy/bootstrap.py", line 305, in bootstrap
    renpy.main.main()
  File "/Applications/renpy-6.99.13-sdk 2/renpy/main.py", line 499, in main
    run(restart)
  File "/Applications/renpy-6.99.13-sdk 2/renpy/main.py", line 147, in run
    renpy.execution.run_context(True)
  File "/Applications/renpy-6.99.13-sdk 2/renpy/execution.py", line 795, in run_context
    context.run()
  File "renpy/common/00start.rpy", line 188, in script
    python:
  File "/Applications/renpy-6.99.13-sdk 2/renpy/ast.py", line 827, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "/Applications/renpy-6.99.13-sdk 2/renpy/python.py", line 1764, in py_exec_bytecode
    exec bytecode in globals, locals
  File "renpy/common/00start.rpy", line 189, in <module>
    renpy.execute_default_statement(True)
  File "/Applications/renpy-6.99.13-sdk 2/renpy/exports.py", line 3196, in execute_default_statement
    i.set_default(start)
  File "game/script.rpy", line 84, in set_default
    default status = gl_name[:2]
  File "/Applications/renpy-6.99.13-sdk 2/renpy/python.py", line 1788, in py_eval_bytecode
    return eval(bytecode, globals, locals)
  File "game/script.rpy", line 84, in <module>
    default status = gl_name[:2]
NameError: name 'gl_name' is not defined

Darwin-16.7.0-x86_64-i386-64bit
Ren'Py 6.99.13.2919
The IB Simulator dev 1.0
Thank you in advance!
Last edited by yawnie on Tue Jan 16, 2018 3:48 pm, edited 1 time in total.

User avatar
Empish
Veteran
Posts: 221
Joined: Thu Jan 14, 2016 9:52 pm
Projects: Efemural Hearts, It Ends With Graduation
itch: empish
Contact:

Re: String Slicing does not work

#2 Post by Empish »

It looks to me like you made a subtle typo. Using lowercase "l" instead of the number "1". I could be wrong, but they look slightly different to me on here.

yawnie
Newbie
Posts: 7
Joined: Mon Jan 15, 2018 11:29 am
Contact:

Re: String Slicing does not work

#3 Post by yawnie »

Empish wrote: Tue Jan 16, 2018 1:21 pm It looks to me like you made a subtle typo. Using lowercase "l" instead of the number "1". I could be wrong, but they look slightly different to me on here.
Ahh. Oh well. The problem is that I fixed the typo and it's still not working.

Code: Select all

label choose_subjects:
    label choose_g1:
        call screen group1_subjects
        "[g1_name]"
        default status = g1_name[:2]
        "[status]"
"[g1_name]" is displayed correctly, but "[status]" literally gives me nothing. It's just blank.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2384
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: String Slicing does not work

#4 Post by Ocelot »

default statements are executed during initialization stage. What is the value of g1_name at that moment?
< < insert Rick Cook quote here > >

yawnie
Newbie
Posts: 7
Joined: Mon Jan 15, 2018 11:29 am
Contact:

Re: String Slicing does not work

#5 Post by yawnie »

Ocelot wrote: Tue Jan 16, 2018 2:17 pm default statements are executed during initialization stage. What is the value of g1_name at that moment?
Aha! I changed it so that default status ="" and $status = g1_name[:2] and status now has the value of "HL". Thank you!

It's still not giving me what I want though...

Code: Select all

label choose_subjects:
    label choose_g1:
        call screen group1_subjects
        "[g1_name]"
        $status = g1_name[:2]
        "[status]"

        if g1_name is "":
            "Please select a subject!"
            jump choose_g1
        if status is "HL":
             $hl_count+=1
        if status is "SL":
             $sl_count+=1
        else:
            "ERROR"
It may be a syntax error since I'm new to python, but I don't see why it's giving me "ERROR."

yawnie
Newbie
Posts: 7
Joined: Mon Jan 15, 2018 11:29 am
Contact:

Re: String Slicing does not work

#6 Post by yawnie »

Oh, maybe it's an indentation error? hl_count is giving me the correct value. Hmm...

I changed the second and third if statements to elifs and they seem to be working! Thank y'all for your help. :)

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2384
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: [Solved]String Slicing does not work

#7 Post by Ocelot »

if status is "HL":
Use == instead of is here.

== is equality, is is identity. Identity is way stronger than equality. It requires variable to not just have same values, but to be the same object.
< < insert Rick Cook quote here > >

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot]