Help on random items [SOLVED]

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
User avatar
LittleRainySeasons
Regular
Posts: 46
Joined: Fri Mar 24, 2017 9:37 am
Projects: Patsy n' Crinkles, Galaxy Party, Time|Reversed
Deviantart: LittleRainySeasons
Soundcloud: RoboticDreamz
itch: LittleRainySeasons
Contact:

Help on random items [SOLVED]

#1 Post by LittleRainySeasons »

I'm currently trying to make a feature where player can randomly get 3 items when they wander around a certain location I'm making.

I tried to test it out but it keeps saying it's invalid syntax. This is the code I made:

Code: Select all

default orange = 0
default apple = 0
default grape = 0
default pear = 0
default banana = 0
default strawberry = 0

label wander:
    $ wander = renpy.random.randint(1, 3)
    $ objqty1 = renpy.random.randint(1, 10)
    $ objqty2 = renpy.random.randint(1, 10)
    $ objqty3 = renpy.random.randint(1, 10)
    $ item1 = renpy.random.choice(['orange', 'apple', 'grape', 'pear', 'banana', 'strawberry'])
    $ item2 = renpy.random.choice(['orange', 'apple', 'grape', 'pear', 'banana', 'strawberry'])
    $ item3 = renpy.random.choice(['orange', 'apple', 'grape', 'pear', 'banana', 'strawberry'])
    
    if wander == 1:
        "You got [objqty1]x [item1], [objqty2]x [item2], and [objqty3]x [item3]."
        if item1 == 'orange':
            $ orange += objqty1
        elif item2 == 'orange':
            $ orange += objqty2
        elif item3 'orange':
            $ orange += objqty3
            
        elif item1 == 'apple':
            $ apple += objqty1
        elif item2 == 'apple':
            $ apple += objqty2
        elif item3 'apple':
            $ apple += objqty3
            
        elif item1 == 'grape':
            $ grape += objqty1
        elif item2 == 'grape':
            $ grape += objqty2
        elif item3 'grape':
            $ grape += objqty3
            
        elif item1 == 'pear':
            $ pear += objqty1
        elif item2 == 'pear':
            $ pear += objqty2
        elif item3 'pear':
            $ pear += objqty3
            
        elif item1 == 'banana':
            $ banana += objqty1
        elif item2 == 'banana':
            $ banana += objqty2
        elif item3 'banana':
            $ banana += objqty3
            
        elif item1 == 'strawberry':
            $ strawberry += objqty1
        elif item2 == 'strawberry':
            $ strawberry += objqty2
        elif item3 'strawberry':
            $ strawberry += objqty3
            
        jump wander
            
    else:
        "You got nothing."
        jump wander
Last edited by LittleRainySeasons on Wed Jul 10, 2024 3:54 am, edited 1 time in total.

giorgi1111
Regular
Posts: 163
Joined: Sat May 04, 2024 10:40 pm
Contact:

Re: Help on random items

#2 Post by giorgi1111 »

You need == here elif item3 'apple' line 23 and on every item3
And i dont know you know or not there is possibility that you ll get same fruits on item1 item2 item3. And how are you using this.in this case label wander must be called every time if you want to get different items diffrent fruits. I just call it in start label call wander. "Orange:[orange],apple:[apple] ....." and it changes results and works good but sometimes gets same fruits on item1 item2 item3 as i saiid

User avatar
LittleRainySeasons
Regular
Posts: 46
Joined: Fri Mar 24, 2017 9:37 am
Projects: Patsy n' Crinkles, Galaxy Party, Time|Reversed
Deviantart: LittleRainySeasons
Soundcloud: RoboticDreamz
itch: LittleRainySeasons
Contact:

Re: Help on random items

#3 Post by LittleRainySeasons »

giorgi1111 wrote: Wed Jul 10, 2024 3:09 am You need == here elif item3 'apple' line 23 and on every item3
And i dont know you know or not there is possibility that you ll get same fruits on item1 item2 item3. And how are you using this.in this case label wander must be called every time if you want to get different items diffrent fruits. I just call it in start label call wander. "Orange:[orange],apple:[apple] ....." and it changes results and works good but sometimes gets same fruits on item1 item2 item3 as i saiid
I actually manage to fix the problem and it turns out I just have to re-arrange the positions of the if and elif.
So if it gives me 2x Banana, 3x Strawberry, and 5x Orange
It will store the said amount in the variables.

Code: Select all

if wander == 1:
	"You got [objqty1]x [item1], [objqty2]x [item2], and [objqty3]x [item3]."
	if item1 == 'orange':
		$ orange += objqty1
        elif item1 == 'apple':
		$ apple += objqty1
        elif item1 == 'grape':
		$ grape += objqty1
        elif item1 == 'pear':
		$ pear += objqty1
        elif item1 == 'banana':
		$ banana += objqty1
        elif item1 == 'strawberry':
		$ strawberry += objqty1
            
        if item2 == 'orange':
		$ orange += objqty2
        elif item2 == 'apple':
		$ apple += objqty2
        elif item2 == 'grape':
		$ grape += objqty2
        elif item2 == 'pear':
		$ pear += objqty2
        elif item2 == 'banana':
		$ banana += objqty2
        elif item2 == 'strawberry':
		$ strawberry += objqty2
        
        if item3 == 'orange':
		$ orange += objqty3
        elif item3 == 'apple':
		$ apple += objqty3
        elif item3 == 'grape':
		$ grape += objqty3
        elif item3 == 'pear':
		$ pear += objqty3
        elif item3 == 'banana':
		$ banana += objqty3
        elif item3 == 'strawberry':
		$ strawberry += objqty3       
        jump wander

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

Re: Help on random items

#4 Post by m_from_space »

LittleRainySeasons wrote: Wed Jul 10, 2024 3:54 am I actually manage to fix the problem and it turns out I just have to re-arrange the positions of the if and elif.
So if it gives me 2x Banana, 3x Strawberry, and 5x Orange
It will store the said amount in the variables.

Code: Select all

if wander == 1:
	"You got [objqty1]x [item1], [objqty2]x [item2], and [objqty3]x [item3]."
	if item1 == 'orange':
		$ orange += objqty1
        elif item1 == 'apple':
		$ apple += objqty1
        elif item1 == 'grape':
		$ grape += objqty1
        elif item1 == 'pear':
		$ pear += objqty1
        elif item1 == 'banana':
		$ banana += objqty1
        elif item1 == 'strawberry':
		$ strawberry += objqty1
            
        if item2 == 'orange':
		$ orange += objqty2
        elif item2 == 'apple':
		$ apple += objqty2
        elif item2 == 'grape':
		$ grape += objqty2
        elif item2 == 'pear':
		$ pear += objqty2
        elif item2 == 'banana':
		$ banana += objqty2
        elif item2 == 'strawberry':
		$ strawberry += objqty2
        
        if item3 == 'orange':
		$ orange += objqty3
        elif item3 == 'apple':
		$ apple += objqty3
        elif item3 == 'grape':
		$ grape += objqty3
        elif item3 == 'pear':
		$ pear += objqty3
        elif item3 == 'banana':
		$ banana += objqty3
        elif item3 == 'strawberry':
		$ strawberry += objqty3       
        jump wander
If your variable names are the same as the strings you randomly selected, you can use a shortcut for changing those variables. Make sure to understand what this is doing though, since your code is of course way more readable.

Code: Select all

if wander == 1:
    "You got [objqty1]x [item1], [objqty2]x [item2], and [objqty3]x [item3]."
    $ setattr(store, item1, getattr(store, item1) + objqty1)
    $ setattr(store, item2, getattr(store, item2) + objqty2)
    $ setattr(store, item3, getattr(store, item3) + objqty3)
    jump wander

JohnChristopher
Newbie
Posts: 1
Joined: Fri Jun 28, 2024 8:10 am
Contact:

Re: Help on random items [SOLVED]

#5 Post by JohnChristopher »

Thanks for answering.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]