Trouble with 'Definitions' [Solved]

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
User avatar
OokamiKasumi
Eileen-Class Veteran
Posts: 1779
Joined: Thu Oct 14, 2010 3:53 am
Completed: 14 games released -- and Counting.
Organization: DarkErotica Games
Deviantart: OokamiKasumi
Location: NC, USA
Contact:

Trouble with 'Definitions' [Solved]

#1 Post by OokamiKasumi »

This is an adaptation of SusanTheCat's Hidden Object code http://lemmasoft.renai.us/forums/viewto ... 46#p149913.

It runs perfectly by itself, but I can't seem to put an opening scene at the beginning to use it for a "Search the Haunted House" game. When I try to add an opening scene, or even just an image, I get something like this:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 33, in script
        scene bg wallpaper with fade
  File "game/screens.rpy", line 606, in python
                for index,i in enumerate(hidden_items):
NameError: global name 'hidden_items' is not defined

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

Full traceback:
  File "I:\!Kenetic Novels\RenPy\renpy-6.12.2\renpy\execution.py", line 261, in run
  File "I:\!Kenetic Novels\RenPy\renpy-6.12.2\renpy\ast.py", line 1000, in execute
  File "I:\!Kenetic Novels\RenPy\renpy-6.12.2\renpy\exports.py", line 870, in with_statement
  File "I:\!Kenetic Novels\RenPy\renpy-6.12.2\renpy\display\core.py", line 1445, in do_with
  File "I:\!Kenetic Novels\RenPy\renpy-6.12.2\renpy\display\core.py", line 1466, in with_none
  File "I:\!Kenetic Novels\RenPy\renpy-6.12.2\renpy\display\core.py", line 1571, in compute_overlay
  File "game/screens.rpy", line 606, in display_items_overlay
NameError: global name 'hidden_items' is not defined

Windows-post2008Server-6.1.7601-SP1
Ren'Py 6.12.2.1531
Antiques 0.0
I know I'm missing a piece of code somewhere, but I can't see where. Please forgive my ignorance.

This is the code I am using.

On the Screens page:

Code: Select all

#################################################
#Hidden Object Game

screen hidden_object:
    tag hidden
    imagemap:
        auto hidden_files
        for index, item in enumerate(hidden_items):
            hotspot (item.x,item.y,item.w,item.h) action If(hidden_items[index].found==False, SetField(hidden_items[index],"found",True), None)
        if is_all_found():
            textbutton "All Objects Found!" xalign 0.5 yalign 0.5 action Return("Completed") 
            
init -2 python:
    class Item:
        def __init__(self, name, x,y,w,h):
            self.name = name
            self.x = x
            self.y = y
            self.w = w
            self.h = h
            self.found = False
            
    showitems = True
    config.imagemap_cache = False
    
    parlor_items = []
    parlor_items.append(Item("landscape,", 852,241,128,189))
    parlor_items.append(Item("phonograph,", 834,493,149,183))
    parlor_items.append(Item("hat,", 782,407,78,73))
    parlor_items.append(Item("cake,", 786,615,50,36))
    parlor_items.append(Item("phone,", 721,492,105,85))
    parlor_items.append(Item("tick-tock,", 656,379,122,91))
    parlor_items.append(Item("fish,", 684,155,104,47))
    parlor_items.append(Item("boxing,", 611,244,60,128))
    parlor_items.append(Item("cane,", 589,366,29,148))
    parlor_items.append(Item("horse,", 355,501,244,171))
    parlor_items.append(Item("tea,", 295,486,75,43))
    parlor_items.append(Item("bottle,", 371,287,42,95))
    parlor_items.append(Item("car,", 220,164,122,60))
    parlor_items.append(Item("perfumes,", 285,562,69,52)) 
    parlor_items.append(Item("books,", 183,580,101,98))
    parlor_items.append(Item("cheese,", 74,631,87,48))
    parlor_items.append(Item("pillow,", 161,465,114,93))
    parlor_items.append(Item("cat,", 73,466,114,90))
    parlor_items.append(Item("vase,", 38,556,36,122))
    parlor_items.append(Item("Lady and Gent,", 36,244,136,182))


    def display_items_overlay():
              
        if showitems:
            ui.frame(id="obj_list")
            ui.hbox(id="display_hbox",spacing=5,box_wrap = True)
            for index,i in enumerate(hidden_items):
                inventory_prefix = ""
                inventory_suffix = ""
                item_name = i.name
                item_state = i.found
                if item_state == True:
                    inventory_prefix = "{s}"
                    inventory_suffix = "{/s}"
                item_text = inventory_prefix+item_name+inventory_suffix
                item_index = "object_%d" % (index)
                ui.text(item_text,id=item_index)
            ui.close()
            
    config.overlay_functions.append(display_items_overlay)
    
    def is_all_found():
        for i in hidden_items:
            if i.found == False:
                return False
        return True
        
    def resetItems(in_items):
        for i in in_items:
            i.found = False
On the Script page:

Code: Select all

init:
    image bg bedroom = "bedroom_empty.png"
    image bg parlor = "parlor_empty.png"
    image bg study = "study_empty.png"

    image bg wallpaper = "bg_01.jpg" 
    image bg wood = "bg_02.jpg"

    image Miss01 = "Miss01.png"
    
    image splash = "000.png"
    
    $renpy.music.register_channel("nature", "music", True) 
    
    
label splash:
    $ renpy.pause(0)
    
    scene black
    with Pause(0.5)

    show splash with dissolve
    with Pause(2.0)

    scene black with dissolve
    with Pause(1.0)

    return


label start:
    
    scene bg wallpaper with fade
    show Miss01 at right with dissolve 
    "This is a little hidden object game. Ready?"
    "Oh, one more thing! You're being timed. Enjoy!"
    
label game:    
    
    $ game = renpy.time.time()
    $ resetItems(parlor_items)
    $ hidden_files = "parlor_%s.png"
    $ hidden_items = renpy.random.sample(parlor_items,8)
    
    call screen hidden_object
    
    scene bg parlor
    $ elapsed = (renpy.time.time() - game)
    "Result: %(_return)s in %(elapsed)d seconds!"                   
    
    "All done."
    
label end:   
     scene bg wallpaper with fade
     show Miss01 at right with dissolve
    
     "I hope you enjoyed that. It's still in the experimental stage, so it's not very impressive at the moment. Even so, would you like to play again?"
    
menu:
    "Play again?":
         jump game2
    
    "No thanks. Show me the credits.":
        return
Can someone please tell me what I'm missing and where it needs to go?
Last edited by OokamiKasumi on Mon Aug 29, 2011 7:24 pm, edited 1 time in total.
Ookami Kasumi ~ Purveyor of fine Smut.
Most recent Games Completed: For ALL my completed games visit: DarkErotica Games

"No amount of great animation will save a bad story." -- John Lasseter of Pixar

User avatar
Alex
Lemma-Class Veteran
Posts: 3094
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Trouble with Definitions

#2 Post by Alex »

Well, you have an overlay function "display_items_overlay" and if variable "showitems" set to "True", this function will show you hidden items from a list "hidden_items".
"showitems" is set to "True" in "init -2 python:" block, so when you trying to show something onscreen, the function trying to show hidden items, but you set the list of them some lines after - that cause the error.
To solve it, change the value of "showitems" to "False" in "init -2 python:" block and add a line <$ showitems = True> right before you call a "hidden_object" screen.

User avatar
OokamiKasumi
Eileen-Class Veteran
Posts: 1779
Joined: Thu Oct 14, 2010 3:53 am
Completed: 14 games released -- and Counting.
Organization: DarkErotica Games
Deviantart: OokamiKasumi
Location: NC, USA
Contact:

Re: Trouble with Definitions [Solved]

#3 Post by OokamiKasumi »

Alex wrote:Well, you have an overlay function "display_items_overlay" and if variable "showitems" set to "True", this function will show you hidden items from a list "hidden_items".

"showitems" is set to "True" in "init -2 python:" block, so when you trying to show something onscreen, the function trying to show hidden items, but you set the list of them some lines after - that cause the error.

To solve it, change the value of "showitems" to "False" in "init -2 python:" block and add a line <$ showitems = True> right before you call a "hidden_object" screen.
Alex, that worked perfectly!
-- Thank you so much!!!
Ookami Kasumi ~ Purveyor of fine Smut.
Most recent Games Completed: For ALL my completed games visit: DarkErotica Games

"No amount of great animation will save a bad story." -- John Lasseter of Pixar

User avatar
AERenoir
Veteran
Posts: 320
Joined: Fri May 27, 2011 8:23 pm
Contact:

Re: Trouble with 'Definitions' [Solved]

#4 Post by AERenoir »

Ooh, I also want to have a Hidden Object mini game in my game. Can you help me? I still don't quite get some things about the Screen and Scripts page.

Where do I declare the Hidden Object game in the Screen page? Can I just dump the entire code in the Script page? Does the init number, like "init -2 python" or "init 1 python" matter? Thanks.

User avatar
Alex
Lemma-Class Veteran
Posts: 3094
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Trouble with 'Definitions' [Solved]

#5 Post by Alex »

It doesn't metter where you'll put your code - in "screens.rpy" or "script.rpy" (for your convinience you even can make separate "some_name_file.rpy" and put all the code there). You can read about it there - http://www.renpy.org/wiki/renpy/FAQ#How ... e_files.3F
But be shure, that at the end of all you'll have only one "label start:" (or you'll get an error).
And numbers in the names of "init" blocks are show in what order they will be executed (-2 earlier than 1).

Post Reply

Who is online

Users browsing this forum: Google [Bot]