Page 1 of 2

location system with list and imagebutton

Posted: Sun Apr 25, 2021 12:47 pm
by Psion
Hi, i'm trying to make a location system with list and immagebutton, so that idle and hover location picked up automatically from names of those locations. But i thing my syntax wrong :(
Can you help me pls?

Code: Select all

init python:
    class place(object):
        def __init__ (self, name, locs, mapped, unlocked):
            self.name = name
            self.locs = locs
            self.mapped = mapped
            self.unlocked = unlocked

    Rooms = []
    #home
    Rooms.append(place("Porch", ["Hallway"], True, True))
    Rooms.append(place("Hallway", ["Porch", "Living room"], False, True))

    location = Rooms[0].name.lower()
    

    def BGDeclare():
        global locations
        global BGimage
        BGimage = location.lower()
        BGimage = BGimage.replace(" ", "_")
        BGimage = "places/" + BGimage + ".jpg"

screen BGIMAGE():
    $ BGDeclare()
    add BGimage
    for q in Rooms:
        $ TempName = "places/" + q.locs + "_%.png"
        imagebutton:
            idle TempName
            hover TempName
            focus_mask True
            action SetVariable("clickType", "mapSelect"), Return(q.locs)
it show error:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 18, in script
    $ UIreturn = renpy.call_screen("MainUI")
  File "game/script.rpy", line 18, in <module>
    $ UIreturn = renpy.call_screen("MainUI")
  File "game/screens/MainUI.rpy", line 1, in execute
    screen MainUI():
  File "game/screens/MainUI.rpy", line 1, in execute
    screen MainUI():
  File "game/screens/MainUI.rpy", line 2, in execute
    use BGIMAGE
  File "game/screens/BGimage.rpy", line 1, in execute
    screen BGIMAGE():
  File "game/screens/BGimage.rpy", line 1, in execute
    screen BGIMAGE():
  File "game/screens/BGimage.rpy", line 4, in execute
    for q in Rooms:
  File "game/screens/BGimage.rpy", line 5, in execute
    $ TempName = "places/" + q.locs + "_%.png"
  File "game/screens/BGimage.rpy", line 5, in <module>
    $ TempName = "places/" + q.locs + "_%.png"
TypeError: coercing to Unicode: need string or buffer, RevertableList found

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

Full traceback:
  File "game/script.rpy", line 18, in script
    $ UIreturn = renpy.call_screen("MainUI")
  File "renpy/ast.py", line 913, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "renpy/python.py", line 2111, in py_exec_bytecode
    exec(bytecode, globals, locals)
  File "game/script.rpy", line 18, in <module>
    $ UIreturn = renpy.call_screen("MainUI")
  File "renpy/exports.py", line 2969, in call_screen
    rv = renpy.ui.interact(mouse="screen", type="screen", roll_forward=roll_forward)
  File "renpy/ui.py", line 298, in interact
    rv = renpy.game.interface.interact(roll_forward=roll_forward, **kwargs)
  File "renpy/display/core.py", line 2978, in interact
    repeat, rv = self.interact_core(preloads=preloads, trans_pause=trans_pause, **kwargs)
  File "renpy/display/core.py", line 3378, in interact_core
    root_widget.visit_all(lambda i : i.per_interact())
  File "renpy/display/core.py", line 566, in visit_all
    d.visit_all(callback, seen)
  File "renpy/display/core.py", line 566, in visit_all
    d.visit_all(callback, seen)
  File "renpy/display/core.py", line 566, in visit_all
    d.visit_all(callback, seen)
  File "renpy/display/screen.py", line 432, in visit_all
    callback(self)
  File "renpy/display/core.py", line 3378, in <lambda>
    root_widget.visit_all(lambda i : i.per_interact())
  File "renpy/display/screen.py", line 443, in per_interact
    self.update()
  File "renpy/display/screen.py", line 631, in update
    self.screen.function(**self.scope)
  File "game/screens/MainUI.rpy", line 1, in execute
    screen MainUI():
  File "game/screens/MainUI.rpy", line 1, in execute
    screen MainUI():
  File "game/screens/MainUI.rpy", line 2, in execute
    use BGIMAGE
  File "game/screens/BGimage.rpy", line 1, in execute
    screen BGIMAGE():
  File "game/screens/BGimage.rpy", line 1, in execute
    screen BGIMAGE():
  File "game/screens/BGimage.rpy", line 4, in execute
    for q in Rooms:
  File "game/screens/BGimage.rpy", line 5, in execute
    $ TempName = "places/" + q.locs + "_%.png"
  File "game/screens/BGimage.rpy", line 5, in <module>
    $ TempName = "places/" + q.locs + "_%.png"
TypeError: coercing to Unicode: need string or buffer, RevertableList found


Re: location system with list and imagebutton

Posted: Sun Apr 25, 2021 3:47 pm
by Ocelot
idle and hover properties take full image path. No substitutions allowed. If you want to use automatic substitution, you need to use auto property.

Anpther problem: q.locs is a list of strings, not a single string. You want to add string to strin, not string to list.

Re: location system with list and imagebutton

Posted: Sun Apr 25, 2021 4:48 pm
by Psion
Auto worked, tnx.

Code: Select all

screen BGIMAGE():
    $ BGDeclare()
    add BGimage
    for q in Rooms:
        $ TempName = "places/" + str(q.locs) + "_%s.png"
        imagebutton auto TempName focus_mask True action SetVariable("clickType", "mapSelect"), Return(q.locs)
But there another error occurred :(

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 18, in script
    $ UIreturn = renpy.call_screen("MainUI")
  File "game/script.rpy", line 18, in <module>
    $ UIreturn = renpy.call_screen("MainUI")
  File "game/screens/MainUI.rpy", line 1, in execute
    screen MainUI():
  File "game/screens/MainUI.rpy", line 1, in execute
    screen MainUI():
  File "game/screens/MainUI.rpy", line 2, in execute
    use BGIMAGE
  File "game/screens/BGimage.rpy", line 1, in execute
    screen BGIMAGE():
  File "game/screens/BGimage.rpy", line 1, in execute
    screen BGIMAGE():
  File "game/screens/BGimage.rpy", line 4, in execute
    for q in Rooms:
  File "game/screens/BGimage.rpy", line 6, in execute
    imagebutton auto TempName focus_mask True action SetVariable("clickType", "mapSelect"), Return(q.locs)
Exception: Imagebutton does not have a idle image. (auto=u"places/[u'Hallway']_%s.png").

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

Full traceback:
  File "game/script.rpy", line 18, in script
    $ UIreturn = renpy.call_screen("MainUI")
  File "renpy/ast.py", line 913, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "renpy/python.py", line 2111, in py_exec_bytecode
    exec(bytecode, globals, locals)
  File "game/script.rpy", line 18, in <module>
    $ UIreturn = renpy.call_screen("MainUI")
  File "renpy/exports.py", line 2969, in call_screen
    rv = renpy.ui.interact(mouse="screen", type="screen", roll_forward=roll_forward)
  File "renpy/ui.py", line 298, in interact
    rv = renpy.game.interface.interact(roll_forward=roll_forward, **kwargs)
  File "renpy/display/core.py", line 2978, in interact
    repeat, rv = self.interact_core(preloads=preloads, trans_pause=trans_pause, **kwargs)
  File "renpy/display/core.py", line 3378, in interact_core
    root_widget.visit_all(lambda i : i.per_interact())
  File "renpy/display/core.py", line 566, in visit_all
    d.visit_all(callback, seen)
  File "renpy/display/core.py", line 566, in visit_all
    d.visit_all(callback, seen)
  File "renpy/display/core.py", line 566, in visit_all
    d.visit_all(callback, seen)
  File "renpy/display/screen.py", line 432, in visit_all
    callback(self)
  File "renpy/display/core.py", line 3378, in <lambda>
    root_widget.visit_all(lambda i : i.per_interact())
  File "renpy/display/screen.py", line 443, in per_interact
    self.update()
  File "renpy/display/screen.py", line 631, in update
    self.screen.function(**self.scope)
  File "game/screens/MainUI.rpy", line 1, in execute
    screen MainUI():
  File "game/screens/MainUI.rpy", line 1, in execute
    screen MainUI():
  File "game/screens/MainUI.rpy", line 2, in execute
    use BGIMAGE
  File "game/screens/BGimage.rpy", line 1, in execute
    screen BGIMAGE():
  File "game/screens/BGimage.rpy", line 1, in execute
    screen BGIMAGE():
  File "game/screens/BGimage.rpy", line 4, in execute
    for q in Rooms:
  File "game/screens/BGimage.rpy", line 6, in execute
    imagebutton auto TempName focus_mask True action SetVariable("clickType", "mapSelect"), Return(q.locs)
  File "renpy/ui.py", line 952, in _imagebutton
    idle = choice(idle, idle_image, "idle", required=True)
  File "renpy/ui.py", line 946, in choice
    raise Exception("Imagebutton does not have a %s image. (auto=%r)." % (name, auto))
Exception: Imagebutton does not have a idle image. (auto=u"places/[u'Hallway']_%s.png").


Re: location system with list and imagebutton

Posted: Sun Apr 25, 2021 5:09 pm
by Ocelot
Do you have image called [u'Hallway']_idle.png in your images/places folder?

Re: location system with list and imagebutton

Posted: Sun Apr 25, 2021 5:43 pm
by Psion
I have hallway_idle.png, no idea where it got "u" and everything from...

Re: location system with list and imagebutton

Posted: Sun Apr 25, 2021 5:56 pm
by Ocelot
It tells you what image it tries to load in error message. Hint: str( ["Hallway"] ) == [u'Hallway']. You need to extract string you need from your list. Because for Hallway entry your list will contain two strings and you will have to choose which one to use.

Re: location system with list and imagebutton

Posted: Sun Apr 25, 2021 6:57 pm
by Psion
like this?

Code: Select all

screen BGIMAGE():
    $ BGDeclare()
    add BGimage
    for q in Rooms:
        $ TempName = "places/" + str(q.locs[0]) + "_%s.png"
        imagebutton auto TempName focus_mask True action SetVariable("clickType", "mapSelect"), Return(q.locs)
But i'm pretty sure it not gonna work with 2 or more units in list. I guess there should be some function for calling each sublocation.

Also i now notice that it search for locs in all roms ... When i need to search only in one that player in now.

Re: location system with list and imagebutton

Posted: Mon Apr 26, 2021 2:21 am
by Ocelot

Code: Select all

for q in Rooms
means "for each room in Rooms list". Nowhere it says anything about room where player is. If I understood you correctly, you need:
1) Find room, where player is.
2) Iterate over that room .locs creating imagebuttin for every string in list.

Re: location system with list and imagebutton

Posted: Mon Apr 26, 2021 5:22 am
by Psion
1) Find room, where player is.
i think "if q.name.lower() == location" should fix that:

Code: Select all

screen BGIMAGE():
    $ BGDeclare()
    add BGimage
    for q in Rooms:
        if q.name.lower() == location:
            $ TempName = "places/" + str(q.locs[0]) + "_%s.png"
            imagebutton auto TempName focus_mask True action SetVariable("clickType", "mapSelect"), Return(q.locs)
2) Iterate over that room .locs creating imagebuttin for every string in list.
something like this?

Code: Select all

screen BGIMAGE():
    $ BGDeclare()
    add BGimage
    for q in Rooms:
        if q.name.lower() == location:
            for w in q.locs:
                $ TempName = "places/to_" + str(w.lower()) + "_%s.png"
                imagebutton auto TempName focus_mask True action SetVariable("clickType", "mapSelect"), Return(q.locs)
it work fine with one location but when i tried more:
Rooms.append(place("Hallway", ["Porch", "Living room"], False, True))
it give me again same error:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 18, in script
    $ UIreturn = renpy.call_screen("MainUI")
  File "game/script.rpy", line 18, in <module>
    $ UIreturn = renpy.call_screen("MainUI")
  File "game/screens/MainUI.rpy", line 1, in execute
    screen MainUI():
  File "game/screens/MainUI.rpy", line 1, in execute
    screen MainUI():
  File "game/screens/MainUI.rpy", line 2, in execute
    use BGIMAGE
  File "game/screens/BGimage.rpy", line 1, in execute
    screen BGIMAGE():
  File "game/screens/BGimage.rpy", line 1, in execute
    screen BGIMAGE():
  File "game/screens/BGimage.rpy", line 4, in execute
    for q in Rooms:
  File "game/screens/BGimage.rpy", line 5, in execute
    if q.name.lower() == location:
  File "game/screens/BGimage.rpy", line 6, in execute
    for w in q.locs:
  File "game/screens/BGimage.rpy", line 8, in execute
    imagebutton auto TempName focus_mask True action SetVariable("clickType", "mapSelect"), Return(q.locs)
Exception: Imagebutton does not have a idle image. (auto=u'places/to_living room_%s.png').

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

Full traceback:
  File "game/script.rpy", line 18, in script
    $ UIreturn = renpy.call_screen("MainUI")
  File "renpy/ast.py", line 913, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "renpy/python.py", line 2111, in py_exec_bytecode
    exec(bytecode, globals, locals)
  File "game/script.rpy", line 18, in <module>
    $ UIreturn = renpy.call_screen("MainUI")
  File "renpy/exports.py", line 2969, in call_screen
    rv = renpy.ui.interact(mouse="screen", type="screen", roll_forward=roll_forward)
  File "renpy/ui.py", line 298, in interact
    rv = renpy.game.interface.interact(roll_forward=roll_forward, **kwargs)
  File "renpy/display/core.py", line 2978, in interact
    repeat, rv = self.interact_core(preloads=preloads, trans_pause=trans_pause, **kwargs)
  File "renpy/display/core.py", line 3378, in interact_core
    root_widget.visit_all(lambda i : i.per_interact())
  File "renpy/display/core.py", line 566, in visit_all
    d.visit_all(callback, seen)
  File "renpy/display/core.py", line 566, in visit_all
    d.visit_all(callback, seen)
  File "renpy/display/core.py", line 566, in visit_all
    d.visit_all(callback, seen)
  File "renpy/display/screen.py", line 432, in visit_all
    callback(self)
  File "renpy/display/core.py", line 3378, in <lambda>
    root_widget.visit_all(lambda i : i.per_interact())
  File "renpy/display/screen.py", line 443, in per_interact
    self.update()
  File "renpy/display/screen.py", line 631, in update
    self.screen.function(**self.scope)
  File "game/screens/MainUI.rpy", line 1, in execute
    screen MainUI():
  File "game/screens/MainUI.rpy", line 1, in execute
    screen MainUI():
  File "game/screens/MainUI.rpy", line 2, in execute
    use BGIMAGE
  File "game/screens/BGimage.rpy", line 1, in execute
    screen BGIMAGE():
  File "game/screens/BGimage.rpy", line 1, in execute
    screen BGIMAGE():
  File "game/screens/BGimage.rpy", line 4, in execute
    for q in Rooms:
  File "game/screens/BGimage.rpy", line 5, in execute
    if q.name.lower() == location:
  File "game/screens/BGimage.rpy", line 6, in execute
    for w in q.locs:
  File "game/screens/BGimage.rpy", line 8, in execute
    imagebutton auto TempName focus_mask True action SetVariable("clickType", "mapSelect"), Return(q.locs)
  File "renpy/ui.py", line 952, in _imagebutton
    idle = choice(idle, idle_image, "idle", required=True)
  File "renpy/ui.py", line 946, in choice
    raise Exception("Imagebutton does not have a %s image. (auto=%r)." % (name, auto))
Exception: Imagebutton does not have a idle image. (auto=u'places/to_living room_%s.png').


Re: location system with list and imagebutton

Posted: Mon Apr 26, 2021 10:02 am
by Ocelot
It says that it cannot find image places/to_living room_idle.png. Note the space and underscores. Do you have image named exactly that?

Re: location system with list and imagebutton

Posted: Mon Apr 26, 2021 11:04 am
by Psion
Yep, wrong name. Fixed it with this:

Code: Select all

screen BGIMAGE():
    $ BGDeclare()
    add BGimage
    for q in Rooms:
        if q.name.lower() == location:
            for w in q.locs:
                $ w = w.lower()
                $ w = w.replace(" ", "_")
                $ TempName = "places/to_" + str(w) + "_%s.png"
                imagebutton auto TempName focus_mask True action SetVariable("clickType", "move"), Return(q.locs)
But next problem is that in
Rooms.append(place("Hallway", ["Porch", "Living room"], False, True))
only to_porch working

Re: location system with list and imagebutton

Posted: Mon Apr 26, 2021 11:48 am
by Ocelot
What do you mean by working/not working? Does screen not return? Does return value contains something different from what you set it to (it should be a list ["Porch", "Living room"] judging from your code)?

Re: location system with list and imagebutton

Posted: Mon Apr 26, 2021 12:01 pm
by Psion
I mean when in hallway i hover my mouse in place where is door to porch it lights up, but when i hover over door to living room nothing happening.

Re: location system with list and imagebutton

Posted: Mon Apr 26, 2021 12:53 pm
by Ocelot
Is there image named places/to_living_room_hover.png? Does clicking it, even if it is not lit up, works?

Re: location system with list and imagebutton

Posted: Mon Apr 26, 2021 2:03 pm
by Psion
oh... my bad, to_living_room_hover.png image was made incorrectly... It work now, thank you.

my code now:

Code: Select all

label start:


    $ Playing = True
    while Playing:
        window hide
        $ clickType = ""
        $ UIreturn = renpy.call_screen("MainUI")
        if clickType == "move":
            $ location = UIreturn

init python:
    class place(object):
        def __init__ (self, name, locs, mapped, unlocked):
            self.name = name
            self.locs = locs
            self.mapped = mapped
            self.unlocked = unlocked

    Rooms = []
    #home
    Rooms.append(place("Porch", ["Hallway"], True, True))
    Rooms.append(place("Hallway", ["Porch", "Living room"], False, True))

    location = Rooms[1].name.lower()


    def BGDeclare():
        global locations
        global BGimage
        BGimage = location.lower()
        BGimage = BGimage.replace(" ", "_")
        BGimage = "places/" + BGimage + ".png"

screen MainUI():
    use BGIMAGE
    
screen BGIMAGE():
    $ BGDeclare()
    add BGimage
    for q in Rooms:
        if q.name.lower() == location:
            for w in q.locs:
                $ w = w.lower()
                $ w = w.replace(" ", "_")
                $ TempName = "places/to_" + str(w) + "_%s.png"
                imagebutton auto TempName focus_mask True action SetVariable("clickType", "move"), Return(q.locs)
Sadly now moving to places not working. It worked fine when i added places one by one, but with list it give me an error.

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 18, in script
    $ UIreturn = renpy.call_screen("MainUI")
  File "game/script.rpy", line 18, in <module>
    $ UIreturn = renpy.call_screen("MainUI")
  File "game/screens/MainUI.rpy", line 1, in execute
    screen MainUI():
  File "game/screens/MainUI.rpy", line 1, in execute
    screen MainUI():
  File "game/screens/MainUI.rpy", line 2, in execute
    use BGIMAGE
  File "game/screens/BGimage.rpy", line 1, in execute
    screen BGIMAGE():
  File "game/screens/BGimage.rpy", line 1, in execute
    screen BGIMAGE():
  File "game/screens/BGimage.rpy", line 2, in execute
    $ BGDeclare()
  File "game/screens/BGimage.rpy", line 2, in <module>
    $ BGDeclare()
  File "game/functions.rpy", line 5, in BGDeclare
    BGimage = location.lower()
AttributeError: 'RevertableList' object has no attribute 'lower'

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

Full traceback:
  File "game/script.rpy", line 18, in script
    $ UIreturn = renpy.call_screen("MainUI")
  File "renpy/ast.py", line 913, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "renpy/python.py", line 2111, in py_exec_bytecode
    exec(bytecode, globals, locals)
  File "game/script.rpy", line 18, in <module>
    $ UIreturn = renpy.call_screen("MainUI")
  File "renpy/exports.py", line 2969, in call_screen
    rv = renpy.ui.interact(mouse="screen", type="screen", roll_forward=roll_forward)
  File "renpy/ui.py", line 298, in interact
    rv = renpy.game.interface.interact(roll_forward=roll_forward, **kwargs)
  File "renpy/display/core.py", line 2978, in interact
    repeat, rv = self.interact_core(preloads=preloads, trans_pause=trans_pause, **kwargs)
  File "renpy/display/core.py", line 3378, in interact_core
    root_widget.visit_all(lambda i : i.per_interact())
  File "renpy/display/core.py", line 566, in visit_all
    d.visit_all(callback, seen)
  File "renpy/display/core.py", line 566, in visit_all
    d.visit_all(callback, seen)
  File "renpy/display/core.py", line 566, in visit_all
    d.visit_all(callback, seen)
  File "renpy/display/screen.py", line 432, in visit_all
    callback(self)
  File "renpy/display/core.py", line 3378, in <lambda>
    root_widget.visit_all(lambda i : i.per_interact())
  File "renpy/display/screen.py", line 443, in per_interact
    self.update()
  File "renpy/display/screen.py", line 631, in update
    self.screen.function(**self.scope)
  File "game/screens/MainUI.rpy", line 1, in execute
    screen MainUI():
  File "game/screens/MainUI.rpy", line 1, in execute
    screen MainUI():
  File "game/screens/MainUI.rpy", line 2, in execute
    use BGIMAGE
  File "game/screens/BGimage.rpy", line 1, in execute
    screen BGIMAGE():
  File "game/screens/BGimage.rpy", line 1, in execute
    screen BGIMAGE():
  File "game/screens/BGimage.rpy", line 2, in execute
    $ BGDeclare()
  File "game/screens/BGimage.rpy", line 2, in <module>
    $ BGDeclare()
  File "game/functions.rpy", line 5, in BGDeclare
    BGimage = location.lower()
AttributeError: 'RevertableList' object has no attribute 'lower'