What am I doing wrong here? trying to use python within rpy

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.
User avatar
Megaman Z
Miko-Class Veteran
Posts: 829
Joined: Sun Feb 20, 2005 8:45 pm
Projects: NaNoRenO 2016, Ren'Py tutorial series
Location: USA

Re: What am I doing wrong here? trying to use python within

#16 Post by Megaman Z » Mon Nov 18, 2013 11:05 pm

I've been slogging through the last full script post you had spot-fixing it to where it's working as you're intending it to work.

Reverting back to that code, the block at line 80 is the main culprit for two reasons:
First, it's an init block positioned during the script itself (a bit of a no-no). all init blocks are executed immediately - before the ren'py window opens. They are also executed every time the game is "reset" (i.e. on new games).
Second, it's an infinite loop due to a small oversight (or misunderstanding of syntax) on your part. You don't check bully for the value of 'm', because there is no key 'm' whatsoever. I am assuming you are checking for a male character, in which case the correct check would be while camper_gender[bully] != 'm' (the equivalent check for a female character, btw, is while camper_gender[bully] != 'f').

Now, after doing that, the next thing you'll hit is ren'py claiming dispo_counter doesn't exist, which is technically true, because you misspelled it in the init python block at the top of the script (you missed the 'n' in "counter").

Another thing to note: I would do any imports inside an init python: block. This means moving the from random import choice line up to the init python: block at around line 14.

Anyways, the corrected script (with my notes), if you feel like copy-pasting it, comes out to this:

Code: Select all

# You can place the script of your game in this file.

# Declare images below this line, using the image statement.
# eg. image eileen happy = "eileen_happy.png"

# Declare characters used by this game.

define pk = Character('Pickett', color="#c8ffc8")
# Python Setup Dicts and other stuff.
init python:

    from random import choice # Put this in an init python block. ~KZ
    major_npcs = {'bf' : 'Barry Fandling', 'sf' : 'Sherry Fandling',
                  'ag' : 'Angles Gator', 't' : 'Troid', 'pw' : 'Playton Williams',
                  'btf' : 'Barret Falster'}

    campers = {'pb' : 'Pooder Bennet', 'jf' : 'Jupiter Fargo',
               'rb' : 'Randy Buffet', 'bl' : 'Botany Lynn',
               'bt' : 'Boris Tortavich', 'tn' : 'Trinda Noober',
               'fj' : 'Freetus Jaunders', 'nt' : 'Ninar Tetras',
               'gm' : 'Gloobin Marfo', 'nk' : 'Niche Kaguya',
               'bd' : 'Brent Drago', 'vt' : 'Volga Toober',
               'kt' : 'Kinser Talebearing', 'br' : 'Bnola Rae',
               'nb' : 'Nugget Beano', 'yk' : 'Yeldstat Krong',
               'gy' : 'Gelliot Yabelor', 'il' : 'Illetia Dorfson',
               'ct' : 'Can Tabber', 'tv' : 'Trinoba Vyder'}

    camper_gender = {'pb' : 'm', 'jf' : 'f',
                     'rb' : 'm', 'bl' : 'f',
                     'bt' : 'm', 'tn' : 'f',
                     'fj' : 'm', 'nt' : 'f',
                     'gm' : 'm', 'nk' : 'f',
                     'bd' : 'm', 'vt' : 'f',
                     'kt' : 'm', 'br' : 'f',
                     'nb' : 'm', 'yk' : 'f',
                     'gy' : 'm', 'il' : 'f',
                     'ct' : 'm', 'tv' : 'f'}

    #Setting Some Disposistion values for characters in a dict
    #Fixed typo ~KZ
    dispo_counter = {'pb' : 0, 'jf' : 0,
                    'rb' : 0, 'bl' : 0,
                    'bt' : 0, 'tn' : 0,
                    'fj' : 0, 'nt' : 0,
                    'gm' : 0, 'nk' : 0,
                    'bd' : 0, 'vt' : 0,
                    'kt' : 0, 'br' : 0,
                    'nb' : 0, 'yk' : 0,
                    'gy' : 0, 'il' : 0,
                    'ct' : 0, 'tv' : 0}

# The game starts here.
label start:

    scene black
    with dissolve

    show text 'The Year Is 199X' with Pause(3.5)

    scene black
    with dissolve
    show text 'A 10 Year Old Boy Is Leaving For Summer Camp' with Pause(3.5)
    scene black
    with dissolve

    show text 'His Name, Is Pickett, & He Is The Hero Of This Story' with Pause(3.5)

    scene black
    with dissolve

    show text 'His Parents Shove Him On The Bus\nHe Takes A Seat Alone.' with Pause(3.5)

    scene black
    with dissolve

    #bully python code

        bully = choice(campers.keys())

        while camper_gender[bully] != 'm': #Check camper_gender[bully], not bully. ~KZ
            bully = choice(campers.keys())

        dispo_counter[bully] -= 1
    'Bully' 'Hey you kid, is that game kid? What is that?'

I have touched exactly nothing outside of the erroneous lines. Hopefully that works on your end.

[edit: put those pause(3.5) statements back in, I had taken them out while debugging this]
~Kitsune Zeta

pk space jam
Posts: 22
Joined: Mon Nov 18, 2013 2:46 am

Re: What am I doing wrong here? trying to use python within

#17 Post by pk space jam » Mon Nov 18, 2013 11:12 pm

Ah I just saw this! But I ended up fixing it on my own seeing the exact same issue was gonna post my solution up here but your code looks better than mine! Thanks so much for the help, I really appreciate it. Nice community you guys got here!

User avatar
Eileen-Class Veteran
Posts: 1406
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love, unknown
Organization: Pilgrim Creations
Github: xavi-mat
itch: xavimat
Location: Italy

Re: What am I doing wrong here? trying to use python within

#18 Post by xavimat » Tue Nov 19, 2013 3:18 am

I'll add, you don't need to import random. You can use renpy.random.choice() instead. (It has its benefits when rolling back and forward).
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Cops&Robbers A two-player experiment
Fear&Love Why can’t we simply express our feelings? Why am I afraid to say ‘I love you’?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

Post Reply

Who is online

Users browsing this forum: Google [Bot]