How do I make a shelf, with multiple random items, each an image button?

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.
Message
Author
Hadriex
Newbie
Posts: 11
Joined: Mon Feb 05, 2024 12:11 am
Contact:

Re: How do I make a shelf, with multiple random items, each an image button?

#16 Post by Hadriex »

I'm still tweaking the code, making sure everything is working and ran into an unexpected problem with the random generation.

The results are random, but only once per launch of the game. I had hoped simply calling the label would reroll everything, but it gives the same results each time.

I even tried replacing renpy.random with just random, using

init python:
import random

but no same result.

So here is the label with all the code for randomizing the lists:


label rollforitems:

# Random number generators.
default slot1_idx = random.randint(0,1) # adjust for list size -- you can also use len(potion_list)
default slot2_idx = random.randint(0,1)
default slot3_idx = random.randint(0,1)
default slot4_idx = random.randint(0,1)
default slot5_idx = random.randint(0,1)

# Taking the random number and matching it to the list.
default random_slot1 = slot1_list[slot1_idx]
default random_slot2 = slot2_list[slot2_idx]
default random_slot3 = slot3_list[slot3_idx]
default random_slot4 = slot4_list[slot4_idx]
default random_slot5 = slot5_list[slot5_idx]

# Resetting all the buttons # I haven't switched all my variable to boolean yet, I was toying with an idea to do something with buttons incrementing to reveal new items behind them and kept them an integers but I'll probably drop the idea and just turn them boolean.
$ button1 = 0
$ button2 = 0
$ button3 = 0
$ button4 = 0
$ button5 = 0
$ leaveshop = 0

return




Unfortunately calling this label only sets the buttons and leaveshop to 0, it does not reroll any of the random numbers.

User avatar
m_from_space
Miko-Class Veteran
Posts: 975
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: How do I make a shelf, with multiple random items, each an image button?

#17 Post by m_from_space »

Hadriex wrote: Thu Feb 15, 2024 4:35 am Unfortunately calling this label only sets the buttons and leaveshop to 0, it does not reroll any of the random numbers.
Please put your code into [ code ] ... [ /code ] tags next time!

Statements using "default" are only executed when you start a new game. They have no place inside a label, but outside of it.

Code: Select all

default slot1_idx = 0

label rollforitems:
    $ slot1_idx = renpy.random.randint(0, 1)
    ...

Hadriex
Newbie
Posts: 11
Joined: Mon Feb 05, 2024 12:11 am
Contact:

Re: How do I make a shelf, with multiple random items, each an image button?

#18 Post by Hadriex »

Just wanted to say, thank you very much for taking the time to help me out. I really do appreciate it m_from_space.

Post Reply

Who is online

Users browsing this forum: No registered users