How can randomization and adding_number work?

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
lets_try_it
Newbie
Posts: 15
Joined: Mon May 25, 2015 2:25 pm
Contact:

How can randomization and adding_number work?

#1 Post by lets_try_it »

Hello everybody on Lemmasoft. I need help with this code. You see, I am trying to make a life simulation game (think the sims or dwarf fortress on an extremely lower scale). So I am trying to figure out two things :


1. How to have the game add custom variables : In my game, after you click on the town-type population, the game picks out the number of important characters in the entire country. An example of my code that describes NPCs is npc_1_name. But the problem is that it would be very time consuming to have to do if_statements for each number of characters such as :

if npc_number >= 2, 3 ,4 , 5...

And so on. I need my coding in the game to duplicate the npc_1_name, but change the 1 each it duplicates that variable. Is there anyway to do this?


2. After the duplication, I need something that can randomized the attributes for each character without me having to do the code I just mentioned as being too time consuming and tedious. So if the I put the options for a character's hair color to be "black" , "brown" or "orange", I need something that also chooses which color the character has for each character. Like :

The computer chooses :

npc_1_haircolor = "black"

npc_2_haircolor = "brown"

User avatar
Milkymalk
Miko-Class Veteran
Posts: 756
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

Re: How can randomization and adding_number work?

#2 Post by Milkymalk »

You sooner or later WILL need to know python for what you are doing. At least, the basics. This time in particular, lists and objects.

You can make a class:

Code: Select all

init python:
    class Npc(object):
        def __init__(self):
            self.selecthair(['brown', 'blonde', 'ginger', 'black'])
            return
        def selecthair(self, colorlist):
            self.haircolor = colorlist[renpy.random.randint(0, len(colorlist)-1)]
            return
Now you can just create an object:

Code: Select all

python:
    mynpc = Npc()
The NPC will be assigned a random hair color from the list inside __init__().

Better, you can make as many NPCs as you like:

Code: Select all

python:
    mynpclist = []
    for i in range(100):
        mynpclist.append(Npc())
This would make 100 NPCs (numbered 0 to 99) as mynpclist[0] to mynpclist[99]. If you want to know the hair color of number 35, use mynpclist[35].haircolor.
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: How can randomization and adding_number work?

#3 Post by trooper6 »

To learn the basics of Python, I recommend the CodeAcademy course on Python. It is free and takes on average 13 hours: http://www.codecademy.com/en/tracks/python
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

lets_try_it
Newbie
Posts: 15
Joined: Mon May 25, 2015 2:25 pm
Contact:

Re: How can randomization and adding_number work?

#4 Post by lets_try_it »

Thank you! I just have a few more questions (sorry if I am really dense when it comes to this. I actually used to practice Python but that was a long time ago and even then , lists/classes always seemed to hard to me) :


I got an "expected statement" error at these points :



class Npc(object):

and

for i in range(100):


Did I do something wrong? Here is how I did it :

Code: Select all

label start:

   class Npc(object):
        def __init__(self):
            self.selecthair(['brown', 'blonde', 'ginger', 'black'])
            return
        def selecthair(self, colorlist):
            self.haircolor = colorlist[renpy.random.randint(0, len(colorlist)-1)]
            return
 
   $ mynpc = Npc()        
            
            
            
   $ mynpclist = []
   for i in range(100):
        mynpclist.append(Npc())      
        
        
   "(mynpclist[35].haircolor)"   
   
   return
            

User avatar
Milkymalk
Miko-Class Veteran
Posts: 756
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

Re: How can randomization and adding_number work?

#5 Post by Milkymalk »

The class declaration needs to be inside a python block as I did it. Preferably inside an "init python:" block. Likewise, the line containing "append" also is a python line and needs a $ sign. Heck, everything I posted was python code, so why not just use a python block?

You also don't need "$ mynpc = Npc()", that was just an example of how to use it.

Also, if you want to use variables inside renpy text lines, don't put them in () but in []. Maybe even [[]] (doubles), I'm not sure at the moment and might be mixing things up.

Code: Select all

init python:

   class Npc(object):
        def __init__(self):
            self.selecthair(['brown', 'blonde', 'ginger', 'black'])
            return
        def selecthair(self, colorlist):
            self.haircolor = colorlist[renpy.random.randint(0, len(colorlist)-1)]
            return
           
label start:       
    python:
        mynpclist = []
        for i in range(100):
            mynpclist.append(Npc())     
       
   "[[mynpclist[35].haircolor]]"   
   
   return
If you are having problems with python, are you sure you want to start such an ambitious project right off the bat?
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: How can randomization and adding_number work?

#6 Post by trooper6 »

My recommendation is, again, to work though the code academy to learn the basics of python, and also read through the QuickStart to learn the basics of renpy, and work incrementally.

Start with the basics:Showing images and text.
Move to menus and branching
Some basic use of variables
Then make a class
Then make a function
Some basic animations using ATL.

etc.
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

User avatar
Milkymalk
Miko-Class Veteran
Posts: 756
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

Re: How can randomization and adding_number work?

#7 Post by Milkymalk »

trooper6 wrote:My recommendation is, again, to work though the code academy to learn the basics of python, and also read through the QuickStart to learn the basics of renpy, and work incrementally.

Start with the basics:Showing images and text.
Move to menus and branching
Some basic use of variables
Then make a class
Then make a function
Some basic animations using ATL.

etc.
Quoted because it's true.

Don't try to run if you can't even walk yet. Don't try to walk if you can't stand.
In your case, you might need to get legs first ;-) Trust us and work yourself through this list. It will make your life much easier and save yourself a lot of question threads here.
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

lets_try_it
Newbie
Posts: 15
Joined: Mon May 25, 2015 2:25 pm
Contact:

Re: How can randomization and adding_number work?

#8 Post by lets_try_it »

OK! I really should check this stuff, so I will take a look at that site (I am trying to get out of my regular, everyday laziness). I used to only do very basic things , but as I get older, I am starting to realize that only knowing if statements, basic screen language and minor details about variables will not help me with my master plans.

Now my last problem is that I got a "turple out of range" error from this line :

"[[mynpclist[35].haircolor]]"

Thank you for all your help, MilkyMalk and trooper6!

lets_try_it
Newbie
Posts: 15
Joined: Mon May 25, 2015 2:25 pm
Contact:

Re: How can randomization and adding_number work?

#9 Post by lets_try_it »

Nevermind, I fixed that part! Thank you for all of your help!

Post Reply

Who is online

Users browsing this forum: Google [Bot]