[solved] finding transforms based on name

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
sam1993
Newbie
Posts: 7
Joined: Tue May 19, 2020 2:23 pm
Contact:

[solved] finding transforms based on name

#1 Post by sam1993 »

Hello,

In my point & click game the player walks into different rooms. Depending on multiple variables different things will be displayed in these rooms.

Between 0 and 3 characters will be shown in a room. I would like these characters to stand neatly next to eachother in the bottomleft of the screen. Which I can do with the following transforms:

Code: Select all

init:
    transform slot0:
        zoom 0.40
        ypos 0.38
    
    transform slot1:
        zoom 0.40
        ypos 0.38
        xpos 0.15

    transform slot2:
        zoom 0.40
        ypos 0.38
        xpos 0.30
I get an npc and add the 'slot0' transform to it in a tuple, later on when I build the screens I use this transform to position the npc at the right place.

the npc object:

Code: Select all

npcWithSlot = (npc, slot0)
and the screen:

Code: Select all

screen BodyScreen(npcWithSlot):
    imagebutton:
        idle npcWithSlot[0].GetUIName()
        focus_mask True
        action Call("interact", "npc", npcWithSlot[0])
        at npcWithSlot[1]
I would like to implement the following code:

Code: Select all

self.npcWithSlotArray = []
teller = 0
    for npc in world.npcArray:
    	 slotToUse = # something like slot + teller?
         npcWithSlot = (npc, slotToUse)
         self.npcWithSlotArray.append(npcWithSlot)
         teller = teller + 1
As you see in the code it's not clear to me how I can get a transform object when I still need to 'create' it's name with the teller. Can anyone send me in the right direction?

Thank you in advance
Last edited by sam1993 on Mon Dec 06, 2021 5:58 pm, edited 1 time in total.

enaielei
Veteran
Posts: 293
Joined: Fri Sep 17, 2021 2:09 am
Organization: enaielei
Tumblr: enaielei
Deviantart: enaielei
Github: enaielei
Skype: enaielei
Soundcloud: enaielei
itch: enaielei
Discord: enaielei#7487
Contact:

Re: finding transforms based on name

#2 Post by enaielei »

it's all just stored in the `store` globally.
e.g.

Code: Select all

trans = getattr(store, "slot{}".format(number))

sam1993
Newbie
Posts: 7
Joined: Tue May 19, 2020 2:23 pm
Contact:

Re: finding transforms based on name

#3 Post by sam1993 »

enaielei wrote: Mon Dec 06, 2021 4:39 pm it's all just stored in the `store` globally.
e.g.

Code: Select all

trans = getattr(store, "slot{}".format(number))
Thank you for your help. This was just what I needed.

User avatar
zmook
Veteran
Posts: 421
Joined: Wed Aug 26, 2020 6:44 pm
Contact:

Re: [solved] finding transforms based on name

#4 Post by zmook »

sam1993 wrote: Mon Dec 06, 2021 2:42 pm Between 0 and 3 characters will be shown in a room. I would like these characters to stand neatly next to eachother in the bottomleft of the screen.
If I understand correctly, you want to keep track of a list of characters, and draw them on the BodyScreen in the right places. It sounds like you're overcomplicating things.

I'm guessing the logic is probably that you want everyone to stand next to each other, no empty slots, filling the ones furthest in the corner first. For that all you need is a list of characters:

Code: Select all

$ npcsPresent = [npc1, npc2, npc3]

screen BodyScreen(npcsPresent):

    for (i,npc) in enumerate(npcsPresent):
        imagebutton:
            zoom 0.40
            xpos i * 0.15
            ypos 0.38

            idle npc.GetUIName()
            focus_mask True
            action Call("interact", "npc", npc)
[`enumerate` is a standard Python function that basically creates a loop counter for you.]

If you need the `slot#` transforms for reasons you haven't included in this question, that's easy enough, too:

Code: Select all

$ npcsPresent = [npc1, npc2, npc3]
define slotTransforms = [slotTransform1, slotTransform2, slotTransform3, slotTransform4]

screen BodyScreen(npcsPresent):

    for (i,npc) in enumerate(npcsPresent):
        imagebutton:
            idle npc.GetUIName()
            focus_mask True
            action Call("interact", "npc", npc)
            at slotTransforms[i]
The above just tracks which npc appears in which slot by their order in the list. If you want more control than that, such as because you might leave empty slots, you could do this:

Code: Select all

$ npcsPresent = [(npc1,3), (npc2,1)), ]

screen BodyScreen(npcsPresent):

    for (npc, slot) in npcsPresent:
        imagebutton:
            idle npc.GetUIName()
            focus_mask True
            action Call("interact", "npc", npc)
            at getattr(store, "slotTransform%d" % slot)
            # (I like old-style %f substitutions in renpy because they cause less trouble 
            # with Ren'Py {} formatting)
I wouldn't actually recommend that, though, because now it's become possible to screw up by putting two characters in the same slot. It's always better to make errors impossible than to have to debug them. A less error-prone way to do the same thing would be to allow Nones as members of the npcsPresent list, and check for them before displaying:

Code: Select all

$ npcsPresent = [npc1, npc2, None, npc3]

screen BodyScreen(npcsPresent):

    for (i,npc) in enumerate(npcsPresent):
        if npc is not None:
            imagebutton:
                zoom 0.40
                xpos i * 0.15
                ypos 0.38

                idle npc.GetUIName()
                focus_mask True
                action Call("interact", "npc", npc)
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

Post Reply

Who is online

Users browsing this forum: No registered users