Unable to empty dictionary (referred to as a string?)

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
SpoilerDuck
Newbie
Posts: 17
Joined: Thu Apr 07, 2011 10:27 am
Contact:

Unable to empty dictionary (referred to as a string?)

#1 Post by SpoilerDuck »

Hi guys, this is more a Python question than a Renpy question but I figure this is as good a place as any. So I'm building a point and click interface using a similar system to that of a messaging interface:

Code: Select all

init python:
    class Object:
        def __init__(self, path, target, xp, yp):
            self.path = path
            self.target = target
            self.xp = xp
            self.yp = yp
        
    class Room:
        def __init__(self):
            self.objects = {}
                       
        def reveal(self, object):
            self.objects[object] = object
            return True
            
        def show_objects(self):
            for object in self.objects:
                ui.imagebutton(object.path, object.path, clicked=renpy.jump(object.target), xalign=object.xp, yalign=object.yp)
                
        def clear(self):
             Fade(.5)
             self.objects.clear()

    #Variables
    room = Room()
  
    #The objects themselves  
    wardrobe = Object("obj/wardrobe.png", "wardrobe", 0.3, 0.4)

    def build_room(room):
        room = rm
        #Clear and create new scene
        renpy.scene()
        renpy.show("bg " + room)
        show_objects(room)
        Fade(.5)
I have no idea if that's going to work or not, as I can't even get far enough into the program to test it. Renpy is taking issue with my 'clear' command, which is called whenever you face a new direction:

Code: Select all

label north:
    $ room.clear
    $ objects.reveal(wardrobe)
    $ build_room("north")
    jump navigation
The theory being that first it empties the dictionary (so any objects associated with, say, 'east' would be removed) then it'd add any objects that are meant to be in view for 'north'.

Just as I start the game, I get the following error:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/build_room.rpy", line 43, in script
        $ room.clear
  File "game/build_room.rpy", line 43, in python
        $ room.clear
AttributeError: 'str' object has no attribute 'clear'
So I get that somehow I've ended up defining the variable 'room' as a string, but I don't know how. I thought curly brackets indicate a dictionary? Where am I going wrong with this?
I also get that I could be entirely wrong with all the code, but as I can't get past the initial error relating to the 'clear' function, I can't test it. I know the build_room function works, but little else. If anyone sees any obvious problems with my entire line of thinking then I'd also appreciate you telling me that.

User avatar
SleepKirby
Veteran
Posts: 255
Joined: Mon Aug 09, 2010 10:02 pm
Projects: Eastern Starlight Romance, Touhou Mecha
Organization: Dai-Sukima Dan
Location: California, USA
Contact:

Re: Unable to empty dictionary (referred to as a string?)

#2 Post by SleepKirby »

All I know is that you probably want to say

Code: Select all

room.clear()
instead of

Code: Select all

room.clear
but that doesn't seem to relate to room being recognized as a string. Just in case, have you tried doing a Find through your files (e.g. Ctrl+Shift+F in JEdit) to make sure you're not defining room as anything else?

SpoilerDuck
Newbie
Posts: 17
Joined: Thu Apr 07, 2011 10:27 am
Contact:

Re: Unable to empty dictionary (referred to as a string?)

#3 Post by SpoilerDuck »

SleepKirby wrote:All I know is that you probably want to say

Code: Select all

room.clear()
instead of

Code: Select all

room.clear
But that's the thing - room.clear calls a separate function that does a clear() and add a fade. Would using a different name for the function, like 'empty' be advisable?

But yeah, that still doesn't explain the string thing.
SleepKirby wrote:Just in case, have you tried doing a Find through your files (e.g. Ctrl+Shift+F in JEdit) to make sure you're not defining room as anything else?
Yeah, looks like I was in build_room. So that's now changed to:

Code: Select all

    def build_room(rm):
        #Clear and create new scene
        renpy.scene()
        renpy.show("bg " + rm)
        show_objects(rm)
        Fade(.5)
But still have the issue of 'room' being defined as a string.

edit - You were completely right. In another file I was using a variable "room" to check what direction the player was facing. That's cleared that up. Now to see if the rest of it works...

edit2 - It didn't. Well, sort of. Adding and displaying objects wasn't a problem, but clearing the dictionary each time was. But that's fixed now, too. Onwards, onwards.

User avatar
SleepKirby
Veteran
Posts: 255
Joined: Mon Aug 09, 2010 10:02 pm
Projects: Eastern Starlight Romance, Touhou Mecha
Organization: Dai-Sukima Dan
Location: California, USA
Contact:

Re: Unable to empty dictionary (referred to as a string?)

#4 Post by SleepKirby »

What I meant is that in Python, room.clear isn't a function call - it's the function itself. room.clear() is a function call.

There's no issue with Room having a function called "clear", as long as it doesn't confuse you. If you say x.clear(), where x is a Room, then you'll call Room's clear function. If x is a dictionary, then you'll call the built-in dictionary clear function.

(Either way, good to hear things are working!)

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]