Randomly selected combinations of objects/characters
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.
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.
-
Elseifthennoidea
- Newbie
- Posts: 4
- Joined: Mon Jun 01, 2015 12:16 am
- Contact:
Randomly selected combinations of objects/characters
Hi all,
I'm wondering if it's possible to create a game using Ren'py which allows for random selection of objects and/or characters to be present in the game from an existing set - for example, I want only 5 items to be present in a game per playthrough, and these would be selected at random from a set of 7, allowing for a total of 21 different combinations of items. Would there be limitations on this? If I had 20 possible items with 12 selected the combinations would be 125,970, for example. (Sorry if the subject wasn't clear enough.)
Additionally, I'm wondering if I could set certain properties to each item - or alternatively characters (applying the same principle). For (a silly) example, if a lighter and cigarettes are present, a lit cigarette might be found later.
Sorry if this is a silly question, thanks for any help given!
I'm wondering if it's possible to create a game using Ren'py which allows for random selection of objects and/or characters to be present in the game from an existing set - for example, I want only 5 items to be present in a game per playthrough, and these would be selected at random from a set of 7, allowing for a total of 21 different combinations of items. Would there be limitations on this? If I had 20 possible items with 12 selected the combinations would be 125,970, for example. (Sorry if the subject wasn't clear enough.)
Additionally, I'm wondering if I could set certain properties to each item - or alternatively characters (applying the same principle). For (a silly) example, if a lighter and cigarettes are present, a lit cigarette might be found later.
Sorry if this is a silly question, thanks for any help given!
Re: Randomly selected combinations of objects/characters
Sure. You can just set up a number of items, run a random number up to the max total number of items (21 in your first example), and then maybe use a list to store them. You'd also want to have a check that if it re-rolls the same item, it would run through the list again to find something new, until the list has seven unique items in it.
As for the second bit, you'd have to write in those story links yourself, but if you have a "key item" list, you could check it at some point and if the right conditions are present, something happens. I would try to keep the list small for that sort of thing though. Here's how you could write the cigarette check part (place this line at the location in the story where one would find it):
In that example, if both items don't make the final list then nothing would happen. You'd have to do up one of these for each combination that you wanted to have do something though, so don't get too elaborate.
As for the second bit, you'd have to write in those story links yourself, but if you have a "key item" list, you could check it at some point and if the right conditions are present, something happens. I would try to keep the list small for that sort of thing though. Here's how you could write the cigarette check part (place this line at the location in the story where one would find it):
Code: Select all
if "cigarettes" in Key_Items and "lighter" in Key_Items: # I think there's a more elegant way of writing this out, but I don't know it myself, this way would work though
"You see a lit cigaratte in the ashtray." #or whatever things you want to have happen if the cigarette is there
- xavimat
- Eileen-Class Veteran
- Posts: 1458
- Joined: Sat Feb 25, 2012 8:45 pm
- Completed: Yeshua, Jesus Life, Cops&Robbers
- Projects: Fear&Love, unknown
- Organization: Pilgrim Creations
- Github: xavi-mat
- itch: pilgrimcreations
- Location: Spain
- Contact:
Re: Randomly selected combinations of objects/characters
A short way I use to select randomly some items in a list:
Code: Select all
label initialize_variables:
$ object_list = ['object1', 'object2', 'object3', 'object4', 'object5'] # The entire list of objects...
$ renpy.random.shuffle(object_list) # ...shuffled randomly...
$ object_list = object_list[:3] # ...and only the three first objects remain in the list.
returnComunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)
-
Elseifthennoidea
- Newbie
- Posts: 4
- Joined: Mon Jun 01, 2015 12:16 am
- Contact:
Re: Randomly selected combinations of objects/characters
Sorry for the extremely late response - thanks very much to both of you. May have more questions later but that's really helped me a lot. There's one other thing I was interested in - if I add in additional random objects later is there a piece of code I can add in that will prevent already present objects being presented?
I know I could individually code everything but I was wondering if anyone was aware of a "shortcut" for preventing duplicates? Or is it just a case of something like if "cigarettes" = True reshuffle and choose another object?
Figured I shouldn't start another thread because this kind of relates to it...
I know I could individually code everything but I was wondering if anyone was aware of a "shortcut" for preventing duplicates? Or is it just a case of something like if "cigarettes" = True reshuffle and choose another object?
Figured I shouldn't start another thread because this kind of relates to it...
- SinnyROM
- Regular
- Posts: 166
- Joined: Mon Jul 08, 2013 12:25 am
- Projects: Blue Birth
- Organization: Cosmic Static Games
- Contact:
Re: Randomly selected combinations of objects/characters
Using a set structure in Python is a way of preventing duplicates: https://docs.python.org/2/tutorial/data ... .html#sets
I haven't tried it with the random function though. Later on I'll give it a shot.
EDIT: It works! Sets turn out to be non-indexable, which makes sense due to their nature, so I imported random instead of using renpy.random.choice. Here's a quick Python function:
I haven't tried it with the random function though. Later on I'll give it a shot.
EDIT: It works! Sets turn out to be non-indexable, which makes sense due to their nature, so I imported random instead of using renpy.random.choice. Here's a quick Python function:
Code: Select all
init python:
# Start with empty set
all_objects = set()
# Making 10 objects and adding to set of all_objects
for i in range(0,10):
all_objects.add('obj'+str(i))
# Current objects
curr_objects = set()
# Random object function
# Takes in the number of objects wanted in curr_objects
import random
def random_objects(num=1):
global all_objects
global curr_objects
curr_objects = random.sample(all_objects, num)
# The game starts here.
label start:
show screen debug
"Start with 10 objects in total"
$ random_objects(3)
"3 random objects"
$ random_objects(5)
"5 random objects"
# Check if obj1 and obj2 are in curr_objects
if all(x in curr_objects for x in ["obj1", "obj2"]):
"You have obj1 and obj2"
elif "obj1" in curr_objects:
"You have obj1"
else:
"You don't have obj1 at all"
return
screen debug():
hbox:
frame:
has vbox
text "all_objects:"
for obj in sorted(all_objects):
text obj
frame:
has vbox
text "curr_objects:"
for obj in sorted(curr_objects):
text obj
Re: Randomly selected combinations of objects/characters
I guess my question is, can you be more specific about what you want? I haven't looked closely at SinnyROM's code, but that's because I wanted to be sure of what you're asking.Elseifthennoidea wrote:Sorry for the extremely late response - thanks very much to both of you. May have more questions later but that's really helped me a lot. There's one other thing I was interested in - if I add in additional random objects later is there a piece of code I can add in that will prevent already present objects being presented?
I know I could individually code everything but I was wondering if anyone was aware of a "shortcut" for preventing duplicates? Or is it just a case of something like if "cigarettes" = True reshuffle and choose another object?
Figured I shouldn't start another thread because this kind of relates to it...
There's a list of 10 different objects.
At the start of the game, you choose a random set of 3.
At some point during the game, there's an opportunity to add to that set -- you want to choose one from the other 7?
Is the above scenario what you're thinking? Assuming all the objects are unique and you use xavimat's code, all you have to do is add object_list[3] -- the list of objects has been randomly shuffled and you took the first three, so you can add the fourth and it will be random.
- trooper6
- Lemma-Class Veteran
- Posts: 3712
- Joined: Sat Jul 09, 2011 10:33 pm
- Projects: A Close Shave
- Location: Medford, MA
- Contact:
Re: Randomly selected combinations of objects/characters
I agree with philat that I'm not sure exactly what you are asking.
I think you are asking not that you don't want duplicates in your set...but that when you pull a random object out of your bag, you don't want to pull out the same object twice. Is that right?
I think you are asking not that you don't want duplicates in your set...but that when you pull a random object out of your bag, you don't want to pull out the same object twice. Is that right?
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?) Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?) Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978
-
Elseifthennoidea
- Newbie
- Posts: 4
- Joined: Mon Jun 01, 2015 12:16 am
- Contact:
Re: Randomly selected combinations of objects/characters
Thanks, I'm sorry I didn't make it clear, but really appreciate the example SinnyROM posted up.
Honestly, I think I just better learn more python before I mess around with this more.
My main goal at the moment is to randomly assign objects to characters, and then have set events/paths play out depending on which character gets what, and have no two characters being assigned the same objects.
Could I use xavimat's in this way?
Like...
and adding some flag for which object it is that person a gets (triggering certain events if person a has batteries, for example)?
I'm thinking that sets won't allow me to define actions for each individual object being present - at least, not at my very basic level (I'll head over to Code Academy and come back with more questions when I'm feeling a little more with it, I'm sure).
Again, thanks for the help!
Honestly, I think I just better learn more python before I mess around with this more.
My main goal at the moment is to randomly assign objects to characters, and then have set events/paths play out depending on which character gets what, and have no two characters being assigned the same objects.
Could I use xavimat's in this way?
Like...
Code: Select all
label initialize_variables:
$ object_list = ['cigarettes', 'matches', 'batteries', 'torch']
$ renpy.random.shuffle(object_list)
$ persona_object = object_list[:1]
return
I'm thinking that sets won't allow me to define actions for each individual object being present - at least, not at my very basic level (I'll head over to Code Academy and come back with more questions when I'm feeling a little more with it, I'm sure).
Again, thanks for the help!
Re: Randomly selected combinations of objects/characters
Well, one thing that you can do to pull each item out exactly once, depending on how you write it in, is to get the "take an object list and shuffle it into a random order" code above, and then when you're pulling from the list in the body of the game, use something like this:
That should work, although I think you'd need to make sure to never ask for more objects than exist in the list, if there are five items in the total list and you ask for the sixth item I think it would crash.
Code: Select all
$ Object_Order = 0 #sets a counting variable to 0, the first slot
. . . #game happens
$ Object_Current = object_list[Object_Order] #sets a string variable to the name of the object at that location in the list.
"You found a [Object_Current]."
$ Inventory.append(Object_Current) #adds the item to the player's inventory. You can put this line inside a menu, to ask if they want to pick up the item.
$ Object_Order += 1 #increments the counter to 1
. . . #game happens, you find the next object
$ Object_Current = object_list[Object_Order]
"You found a [Object_Current]." # now you've found the second object, the counter means that you'll only ever find the next item on the list
$ Inventory.append(Object_Current)
$ Object_Order += 1 #increments the counter to 1
if "matches" in Inventory: #if you found matches by this point, something happens, otherwise, it does not
jump do_that_thing
- trooper6
- Lemma-Class Veteran
- Posts: 3712
- Joined: Sat Jul 09, 2011 10:33 pm
- Projects: A Close Shave
- Location: Medford, MA
- Contact:
Re: Randomly selected combinations of objects/characters
So, if what you want to have 5 items and then randomly give those 5 items to 5 people but make sure you don't give the same item out twice, I'd use the pop method. Pop() removes the item from the list and gives it to you. And since the object is no longer in the list, it can't be given twice. Now for this example, I'm going to assume that you have created a character class and an inventory class and that each character has an inventory object. (see my thread here on inventories: http://lemmasoft.renai.us/forums/viewto ... =inventory)
So:
Now you said "I'm thinking that sets won't allow me to define actions for each individual object being present - at least, not at my very basic level (I'll head over to Code Academy and come back with more questions when I'm feeling a little more with it, I'm sure)."
You are being unclear again. What action do you want to happen? Who (or what object) will do the action? When? Under what circumstances?
So:
Code: Select all
#initialize your list
default object_list = ["cigarettes", "soap", "flashlight", "cassette tape"]
#game starts
label start:
#shuffle your list
$ renpy.random.shuffle(object_list)
#give an item to a character and and remove it from your list
$ tim.inventory.append(object_list.pop())
$ sue.inventory.append(opject_list.pop())You are being unclear again. What action do you want to happen? Who (or what object) will do the action? When? Under what circumstances?
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?) Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?) Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978
-
Elseifthennoidea
- Newbie
- Posts: 4
- Joined: Mon Jun 01, 2015 12:16 am
- Contact:
Re: Randomly selected combinations of objects/characters
Thank you for that link. I see now that I'll definitely have to look into inventories more.
Well my current goal would be to code a card game - 4 cards have different attributes, I want to give a random card to each of 4 people. Fire, Water, Earth, and Air. For example, water beats fire, which beats air, which beats earth, which beats water, and the letters work similarly. The choice I give to the player determines which people play against each other. This would be a repeatable event - the game would have a couple of rounds.
Depending on:
a). who wins in each round
b). what card each person plays
c). the results of particular cards, and
d). who played against each other
different events occur.
E.g. Person 1 plays against Person 3, in this case Person 1 loses by using fire against water. An event is then triggered (based on Person 1 losing against person 3). Additional events are triggered later on because Person 1 lost when they had fire - e.g. Person 1 now refuses to pick the card that seems to be the fire card (maybe they've marked it in a previous event).
Onishion, sorry, I haven't been clear enough - would that work for what I want, if I just change it so there's more than one person's inventory defined, as in the codes shown by trooper6?
trooper6, I hope that's clear enough...? Sorry, I'm not the most articulate with these ideas...
Well my current goal would be to code a card game - 4 cards have different attributes, I want to give a random card to each of 4 people. Fire, Water, Earth, and Air. For example, water beats fire, which beats air, which beats earth, which beats water, and the letters work similarly. The choice I give to the player determines which people play against each other. This would be a repeatable event - the game would have a couple of rounds.
Depending on:
a). who wins in each round
b). what card each person plays
c). the results of particular cards, and
d). who played against each other
different events occur.
E.g. Person 1 plays against Person 3, in this case Person 1 loses by using fire against water. An event is then triggered (based on Person 1 losing against person 3). Additional events are triggered later on because Person 1 lost when they had fire - e.g. Person 1 now refuses to pick the card that seems to be the fire card (maybe they've marked it in a previous event).
Onishion, sorry, I haven't been clear enough - would that work for what I want, if I just change it so there's more than one person's inventory defined, as in the codes shown by trooper6?
trooper6, I hope that's clear enough...? Sorry, I'm not the most articulate with these ideas...
Re: Randomly selected combinations of objects/characters
Yeah, so if I'm understanding it right, you start with four options? So have a list that is initialized to contain Cards = ["earth", "air", "wind", "water"], and then shuffle it using "$ renpy.random.shuffle(Cards)". Then you just give "Cards[0]" to first player by using something similar to the code I wrote above, only since you're dealing each character a card, you would repeat the sequence four times, so:
Now, the sort of event checking you're talking about is possible, but it could get complicated, because you seem to want to track a lot of different variables at once. How you do this kind of depends on exactly how you want to do it, there are more or less efficient methods each way. You could do it like this:
So now you have these lists, later in the game, you'd have something along these lines:
"Round_Winner" would look like this: [3, 2, 1, 2, 3, 4]
"Round_Loser" would look like this: [1, 3, 4, 1, 2, 3]
"Player3Played" would look like this: ["water", "fire", "null", "null", "earth", "air"]
So then you could do an event check at some point like:
That means that if in round 5 (which would be denoted by [4] since lists start at 0), Player 3 was in it, and won, and used the earth card to do it, then something happens, whatever you want, but otherwise, nothing happens. You could also do "if (Round_Winner[4] == 3 and Round_Loser[4] == 1) or (Round_Winner[4] == 1 and Round_Loser[4] == 3):", which would clear only if players 1 and 3 fought in round 4, regardless of which won. You could check against any combination of those factors, as few or as many as you need, but it can get very complicated to manage.
Code: Select all
$ Card_Order = 0
$ Card_Current = Cards[Card_Order]
"Player 1 found a [Card_Current]."
$ Player1Cards.append(Card_Current) #adds the item to the player's inventory.
$ Card_Order += 1 #increments the counter to 1
"Player 2 found a [Card_Current]."
$ Player2Cards.append(Card_Current) #adds the item to the player's inventory.
$ Card_Order += 1 #increments the counter to 1
"Player 3 found a [Card_Current]."
$ Player3Cards.append(Card_Current) #adds the item to the player's inventory.
$ Card_Order += 1 #increments the counter to 1
"Player 4 found a [Card_Current]."
$ Player4Cards.append(Card_Current) #adds the item to the player's inventory.
# now each player has their card, randomly picked from the available options.
Code: Select all
#to track condition "a" as you listed it,
$ Round_Winner = [] #initializes a round tracker list at the beginning of the game. Make sure to do this with all the list variables I'm talking about below
#here you put the actual combat, let's say Player3 won.
$ Round_Winner.append(3) #this adds "3" to the list, with each player receiving a number 1-4
#after several rounds of this, the list "Round_Winner" would look like this: [3, 2, 1, 2, 3, 4], indicating that player three won the first round, two the second, one the third, and so on.
# You could then use another variable to track which card they played, so if player 3 played water in round one, then right after he plays it, you can add:
$ Player3Played.append("water") #now a tricky thing here is that if only two players compete in each round, you still need to add one of these for the players that didn't play, to keep them synched up, so:
$ Player4Played.append("null") #or something like that
#then for the d, you could just go with:
$ Round_Loser.append(3) #this makes a second list, which would track the losers, by comparing this to the winner list, you would have both combatants, in this case, players 2 and 3 fought in round 1, and 2 won.
"Round_Winner" would look like this: [3, 2, 1, 2, 3, 4]
"Round_Loser" would look like this: [1, 3, 4, 1, 2, 3]
"Player3Played" would look like this: ["water", "fire", "null", "null", "earth", "air"]
So then you could do an event check at some point like:
Code: Select all
if Round_Winner[4] == 3 and Player2Played[4] == "earth":
jump Do_That_Thing
Last edited by Onishion on Thu Jun 25, 2015 3:12 pm, edited 1 time in total.
Re: Randomly selected combinations of objects/characters
Onishion's general idea works, but the specific implementation doesn't work because of how assignment works in python -- basically, merely increasing the counter is not enough, you have to assign Card_Current again each time. Trooper's suggestion of using pop is probably better if there is a finite number of cards that shouldn't be duplicated.
Anyway, besides that, this is really more a question to the OP about what exactly needs to happen. Every time someone asks one of the questions about designing a system generally, I'm like, well, I can't help you, because this is an intricate enough system that you need to figure out way more stuff in DETAIL.
For instance, what happens when fire plays earth? How are cards dealt? You implied that it would be random at first but not later? Are there only four cards? Does each player only hold one card at a time? If not, can you have multiples of each card? Do players only play two at a time? How is that decided? If one player can hold multiple cards, how does the opponent decide what card to play? (i.e., do you have to program AI?) Does one card have multiple attributes (like playing cards have a suit and a number/letter)? Does that affect win/loss? Obviously every answer to these questions would change how you want to optimally structure your card class.
At what point are games being played and and at what point are "events" happening? Is this a mostly linear story-based game punctuated by matches which act as branching points, or a mostly match-based game that loops the matches and calls events in-between based on variables? This would change how you design the whole 'engine', so to speak, of the match process and whether you jump to or call events, etc.
It's fine if you don't have the details ironed out (that's what the development process is for!), but the vagueness makes it almost impossible for a third party to suggest how you should structure the code.
Anyway, besides that, this is really more a question to the OP about what exactly needs to happen. Every time someone asks one of the questions about designing a system generally, I'm like, well, I can't help you, because this is an intricate enough system that you need to figure out way more stuff in DETAIL.
For instance, what happens when fire plays earth? How are cards dealt? You implied that it would be random at first but not later? Are there only four cards? Does each player only hold one card at a time? If not, can you have multiples of each card? Do players only play two at a time? How is that decided? If one player can hold multiple cards, how does the opponent decide what card to play? (i.e., do you have to program AI?) Does one card have multiple attributes (like playing cards have a suit and a number/letter)? Does that affect win/loss? Obviously every answer to these questions would change how you want to optimally structure your card class.
At what point are games being played and and at what point are "events" happening? Is this a mostly linear story-based game punctuated by matches which act as branching points, or a mostly match-based game that loops the matches and calls events in-between based on variables? This would change how you design the whole 'engine', so to speak, of the match process and whether you jump to or call events, etc.
It's fine if you don't have the details ironed out (that's what the development process is for!), but the vagueness makes it almost impossible for a third party to suggest how you should structure the code.
Re: Randomly selected combinations of objects/characters
Ah, yes, sorry about that. So:Onishion's general idea works, but the specific implementation doesn't work because of how assignment works in python -- basically, merely increasing the counter is not enough, you have to assign Card_Current again each time.
Code: Select all
$ Player1Cards.append(Card_Current) #adds the item to the player's inventory.
$ Card_Order += 1 #increments the counter by 1
$ Card_Current = Cards[Card_Order] #Sets Card_Current to the option sitting at the "Card_Order" position in the "Cards" list
And I agree that while I offered a suggestion that might work for him, he'll have to tweak it (or use something else entirely) depending on what he actually wants to accomplish with it.
Who is online
Users browsing this forum: Ocelot

