[SOLVED] Failing at adding variables in a search items mini-game

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
Posts: 4
Joined: Wed Feb 26, 2020 1:18 pm

[SOLVED] Failing at adding variables in a search items mini-game

#1 Post by Venecho » Wed Feb 26, 2020 1:49 pm


I've found, in the cookbook section, a free to use search items mini-game, and I wanted to use the base code to create a mini game when you can take care of your pets by simply clicking on some part of the screen (clicking on a fork clean the place, clicking on a manger feed them, etc.)

So, I thought I would use variables for that : when you click on a certain spot, it changes the variable's value.

The thing is, I know almost nothing in python, and I just can't seem to make it work. That's why I'm asking here for your help today.

Here's what the script.rpy file looks like :

Code: Select all

image bg locker = "locker_empty.png"

init python:
    locker_items = []
    locker_items.append(Item("Food", 240,454,56,101))
    locker_items.append(Item("Pet 1",528,73,91,129))
    locker_items.append(Item("Pet 2",515,296,98,99))

# The game starts here.
label start:

    $ foodg = 0

    $ start = renpy.time.time()
    # this sets all the items back to not found
    $ resetItems(locker_items)
    # which image set to use
    $ hidden_files = "locker_%s.png"
    # randomize the list and pick 5 items
    $ hidden_items = renpy.random.sample(locker_items,4)
    # set number of hints
    $ num_hints = 3
    # set number of extra clicks
    $ num_clicks = 0

    scene bg locker
    "Test" "test"

    $ showitems = True
    call screen hidden_object

label result:

    if foodg == 1:
        "test" "animals fed"
        "test" "animals not fed"

and here's the hidden_code.rpy associated:

Code: Select all

init python:
    class registerClick(Action):
        def __call__(self):
            global num_clicks
            num_clicks += 1

    class getHint(Action):
        def __call__(self):
            global num_hints
            num_hints -= 1
            for index,i in enumerate(hidden_items):
                if i.found == False and i.hint == False:
                    i.hint = True

        def get_sensitive(self):
            global num_hints
            return num_hints > 0

    class SetItem(Action):

        def __init__(self, object, field, value):
            self.object = object
            self.field = field
            self.value = value

        def __call__(self):
            setattr(self.object, self.field, self.value)

        def get_selected(self):
            return getattr(self.object, "hint") == True

    class Item:
        def __init__(self, name, x,y,w,h,hint=False):
            self.name = name
            self.x = x
            self.y = y
            self.w = w
            self.h = h
            self.found = False
            self.hint = hint

    showitems = False

    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

screen hidden_object:
    tag hidden

        auto hidden_files
        cache False
        imagebutton auto "empty_%s.png" action registerClick()
        for index, item in enumerate(hidden_items):
            hotspot (item.x,item.y,item.w,item.h) action If(hidden_items[index].found==False, SetItem(hidden_items[index],"found",True), None)
        if locker_items[1]==True:
            $ foodg = 1
        textbutton "Done" xalign 1.0 yalign 0.1 action Jump("result")
As you can see, I tried to change foodg value here :

Code: Select all

 if locker_items[1]==True:
            $ foodg = 1
I tried many things but I think I'm just not comprehending python enough do that on my own.

Thank you for taking the time to read it all, and thank you in advance if you decide to help me!

(and sorry for the broken english, it's not my native language, I'm trying my best!)
Last edited by Venecho on Sat Feb 29, 2020 12:43 am, edited 1 time in total.

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

Re: Failing at adding variables in a search items mini-game

#2 Post by Alex » Wed Feb 26, 2020 4:32 pm

If you need some active parts of screen, then try imagemap or a number of imagebuttons - they are suit well for player's interaction with the game.

Check tutorial game - section about screens and screen language, and some documentation
https://www.renpy.org/doc/html/screens. ... statements

Also, this might help you too - viewtopic.php?f=51&t=18047

Posts: 4
Joined: Wed Feb 26, 2020 1:18 pm

Re: Failing at adding variables in a search items mini-game

#3 Post by Venecho » Thu Feb 27, 2020 4:06 am

So, you think I should start from scrap again because this code isn't appropriate for what i'm trying to do?
I'm very new to this so using the previous code seemed easier - well, if you tell me there's no way I can make this code work, I'll try understanding screens then!

Thanks for all the links and your help!

User avatar
Miko-Class Veteran
Posts: 620
Joined: Mon Jan 26, 2009 7:21 pm

Re: Failing at adding variables in a search items mini-game

#4 Post by gas » Thu Feb 27, 2020 2:39 pm

Some more hint.

Create all the needed variables and assign them a default value (you can change it later).
Create a loop (that's mean a label that jump back on itself, or exit away on an input)
Call a screen with interaction buttons, that Return() an action.
Have your code manage the Return() value (for example, determine the clean if the action Return("clean"))

A very proxy code to indepth:

Code: Select all

label grooming:
    call screen dog_grooming
    $ act = _return
    if act == "clean":
        # do something
    if act == "play":
        # do something
    if act == "exit":
        jump away # go to another label and end the minigame
    jump grooming

screen dog_grooming():
        imagebutton auto "clean_%s.png" action Return("clean")
        imagebutton auto "play_%s.png" action Return("play")
        imagebutton auto "exit_%s.png" action Return("exit")
10 ? "RENPY"
20 GOTO 10


Posts: 4
Joined: Wed Feb 26, 2020 1:18 pm

Re: Failing at adding variables in a search items mini-game

#5 Post by Venecho » Sat Feb 29, 2020 12:43 am

Thank you so very much!

Thanks to your code (which I reshaped a bit, using while as you suggested and ui.imagebutton) and the tutorials Alex provided, I managed to write a script that works perfectly (and now, I understand screens, so that's definitely for the better).

Again, thank you both for your time and effort, it was really much appreciated!

Post Reply

Who is online

Users browsing this forum: CmdrMichael, Google [Bot]