Variable Layered Image/Composite Within an Item Definition

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
thexerox123
Regular
Posts: 131
Joined: Fri Jan 20, 2023 3:21 pm
itch: thexerox123
Contact:

Variable Layered Image/Composite Within an Item Definition

#1 Post by thexerox123 »

I have an inventory system in my game that's based on this one.

Here's an example of how I define a food Item:

Code: Select all

define teacup = Item(name="Tea", desc="A steaming cup of tea.", category="Food", icon="images/items/Tea.png", act=Show("edible_popup", food="tea", message="A steaming cup of tea."))
And the code for the inventory view for food:

Code: Select all

screen food_view(inventory, second_inventory=False, trade_mode=False, single_inventory=True):     
    side "c r":
        style_group "invstyle"
        area (0, 0, 1400, 700) 
        vpgrid id ("vp"+inventory.name):
            draggable True   
            mousewheel True
            allow_underfull True
            xsize 1400 ysize 700
            # if inventory.grid_view:
            #     cols 3 spacing 50

            cols 3 spacing 50

            $ food_items = [item for item in inventory.inv if item[0].category == "Food"]

            if not food_items:
                add Null(height=100,width=100)
            else:
                for item in food_items:
                    $ name = item[0].name
                    $ desc = item[0].desc
                    $ value = item[0].value
                    $ category = item[0].category
                    $ qty = str(item[1])
                    if item[0].category == "Food":
                        vbox:
                            if item[0].icon:
                                $ icon = item[0].icon
                                $ hover_icon = im.Sepia(icon)                          
                                imagebutton:
                                    idle LiveComposite((370,320), (0,0), icon, (0,0), Text(qty))
                                    hover LiveComposite((370,320), (0,0), hover_icon, (0,0), Text(qty))
                                    action (If(not second_inventory, item[0].act, (If(trade_mode, Function(trade,inventory, second_inventory, item), Function(transaction,inventory, second_inventory, item)))))
                                hbox:
                                    xalign 0.5
                                    text name size 25
                                if not inventory.grid_view:
                                    vbox:
                                        text name
                                        if not trade_mode:
                                            text "List Value: [value]"                                        
                                            if second_inventory:                                            
                                                text ("Sell Value: " + str(calculate_price(item, second_inventory)) + ")")
                            
                            else:                               
                                textbutton "[name] ([qty])" action (If(not second_inventory, item[0].act,(If(trade_mode, Function(trade,inventory, second_inventory, item), Function(transaction,inventory, second_inventory, item))))) hovered Show("tooltip",item=item,seller=second_inventory) unhovered Hide("tooltip")
                                if not inventory.grid_view:
                                    vbox:                        
                                        text "List Value: [value]"
                                        if not trade_mode and second_inventory:
                                            text "Sell Value: " + str(calculate_price(item, second_inventory)) + ")"

            ## maintains spacing in empty inventories.
            if len(inventory.inv) == 0:
                add Null(height=100,width=100)
                                    
        vbar value YScrollValue("vp"+inventory.name)
Here's my problem... I have one store in particular that will be selling Fro Yo.

Image

I'm hoping that the player can buy the Fro Yo, then choose whether they want toppings... but that means that for each Fro Yo item, the icon would need to be its own composite determined by what the player chooses topping-wise.

The problem is, the icon is determined in the wider Item definition, so I'm not quite sure how to square that with my need for a for loop to determine it on an instance level.

If anybody has any thoughts for how I could get such a thing working, it would be greatly appreciated!
Last edited by thexerox123 on Fri Apr 12, 2024 8:34 pm, edited 1 time in total.

philat
Eileen-Class Veteran
Posts: 1920
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Variable Layered Image/Composite Within an Item Definition

#2 Post by philat »

Probably simplest route would be to use a ConditionSwitch or LayeredImage with conditions for the toppings in place of the LiveComposite.

thexerox123
Regular
Posts: 131
Joined: Fri Jan 20, 2023 3:21 pm
itch: thexerox123
Contact:

Re: Variable Layered Image/Composite Within an Item Definition

#3 Post by thexerox123 »

That does make sense... so I'd still define the 12 different Fro Yo images, but when putting the image together in the inventory, would determine what to layer on top with ConditionSwitch or LayeredImage?

Thank you, that's helpful in framing how I could approach it! :)

thexerox123
Regular
Posts: 131
Joined: Fri Jan 20, 2023 3:21 pm
itch: thexerox123
Contact:

Re: Variable Layered Image/Composite Within an Item Definition

#4 Post by thexerox123 »

Though I guess the thing I'm still not sure about is how I'd make a for loop to check each individual instance of a Fro Yo item and keep track of them.

Perhaps a list of lists? But then I still need to figure out how to integrate that with my existing inventory system.

Purchasing items is just done via a screen that points to a label for each item... so the label would have

Code: Select all

$ foster_inv.take(whateveritem)
$ wallet -= 1
for instance. So I could easily do things differently just for buying the Fro Yo, but I'm still not quite sure how to give each Fro Yo instance its own specific extra variable(s) for the toppings.

philat
Eileen-Class Veteran
Posts: 1920
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Variable Layered Image/Composite Within an Item Definition

#5 Post by philat »

Eh... I mean, that's a data structure question and basically only you can decide, but if you don't want to change the static item definitions, you could just have a list/dictionary in the screen to keep track (i.e., toppings = {0:[True, False], 1:[False, False]} etc. with True/False switches for each relevant topping). Otherwise, you would need to change the item structure itself to integrate the toppings into it - and also then you would need to save it so you would have to default/deep copy the instances.

thexerox123
Regular
Posts: 131
Joined: Fri Jan 20, 2023 3:21 pm
itch: thexerox123
Contact:

Re: Variable Layered Image/Composite Within an Item Definition

#6 Post by thexerox123 »

Hmm, right. I guess the data structure is what I'm having trouble wrapping my mind around, though.

I'm not averse to changing the structure; I've already added the category attribute to be able to split them into different tabs.
I guess for the potential list/dict method, I still can't quite wrap my head around how I'd integrate it back into the broader inventory list.

Since the category attribute is just used for filtering, I could maybe put the 12 base items into a "FroYo" category rather than the broader "Food" category, filter them alongside the Food items, but use the fact that they are a separate category to give them distinct logic for creating instances and storing the topping variables and layered icons?

Or should I make the instantiation a separate function that's called from the label when the toppings are selected?

Maybe I could figure out a way to give each FroYo item a hash for its toppings? First two digits would range from 00 to 11 for the 12 base flavours, then have 5 more binary digits to represent the toppings? So like 1001100 could be base flavour 11, no strawberries, yes chocolate chips, yes sprinkles, no nuts, no gummy bears.

I may be overthinking this.

(Apologies for my rambling, it's helpful for me to think through it out loud like this, I appreciated your response in helping me to get my head around things!)

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Majestic-12 [Bot]