philat wrote:...why are you back to using variables rather than classes again? Randomizing the names is just a matter of building the choices into the initialization of the object.
Philat - you're awesome! This code was very clean and easy to read, I really appreciate it. Just a few last questions and I think I can mark this one Solved! For one, when you run through this and it kicks you back to the menu (or go back to the menu manually)… and start again it errors out:
Code: Select all
I'm sorry, but an uncaught exception occurred.
While running game code:
File "game/script.rpy", line 56, in script
"[char_list[0].full]"
IndexError: list index out of range
-- Full Traceback ------------------------------------------------------------
Full traceback:
File "game/script.rpy", line 56, in script
"[char_list[0].full]"
File "/Applications/renpy-6.99.6-sdk/renpy/ast.py", line 594, in execute
renpy.exports.say(who, what, interact=self.interact)
File "/Applications/renpy-6.99.6-sdk/renpy/exports.py", line 1032, in say
who(what, interact=interact)
File "/Applications/renpy-6.99.6-sdk/renpy/character.py", line 817, in __call__
what = what_pattern.replace("[what]", sub(what, translate=translate))
File "/Applications/renpy-6.99.6-sdk/renpy/character.py", line 799, in sub
return renpy.substitutions.substitute(s, scope=scope, force=force, translate=translate)[0]
File "/Applications/renpy-6.99.6-sdk/renpy/substitutions.py", line 229, in substitute
s = formatter.vformat(s, (), kwargs)
File "/home/tom/ab/x64lucid-deps/install/lib/python2.7/string.py", line 543, in vformat
File "/home/tom/ab/x64lucid-deps/install/lib/python2.7/string.py", line 565, in _vformat
File "/home/tom/ab/x64lucid-deps/install/lib/python2.7/string.py", line 634, in get_field
IndexError: list index out of range
Darwin-14.5.0-x86_64-i386-64bit
Ren'Py 6.99.6.739
demo 0.0
I'm not sure what is being left over that could cause that error - the only thing I did was remove the "long way" bit as it seemed redundant (I also added my code for non-random variables, but it's not being used here):
Code: Select all
init python:
class NonUniformRandom(object):
def __init__(self, list_of_values_and_probabilities):
"""
expects a list of [ (value, probability), (value, probability),...]
"""
self.the_list = list_of_values_and_probabilities
self.the_sum = sum([ v[1] for v in list_of_values_and_probabilities])
def pick(self):
"""
return a random value taking into account the probabilities
"""
import random
r = random.uniform(0, self.the_sum)
s = 0.0
for k, w in self.the_list:
s += w
if r < s: return k
return k
class CharInfo():
def __init__(self, first, last, alive=True):
self.first = first
self.last = last
self.full = first + " " + last
self.alive = alive
first_names = ['Bob', 'James', 'John']
last_names = ['Jones', 'Smith', 'Young']
label start:
"start"
## short way of generating random characters
$ char_list = []
python:
for i in range(len(first_names)):
char_list.append(
CharInfo(
renpy.random.choice(first_names),
renpy.random.choice(last_names),
)
)
first_names.remove(char_list[i].first)
last_names.remove(char_list[i].last)
first_names = ['Bob', 'James', 'John'] ## restore lists as otherwise rollback gets screwed up
last_names = ['Jones', 'Smith', 'Young']
"[char_list[0].full]"
"[char_list[1].full]"
"[char_list[2].full]"
## long way
## testing for save data retention
$ char_list[0].alive = False
"save/load here"
"[char_list[0].alive]"
A few other quickies - what is the testing for save data retention bit mean? I see you flag what was true to false there, but other than that not sure what it's supposed to be doing?
And lastly, what would the syntax be for creating an if/elseif/else statement in the code for the generator? Like, if the random generator pulls the name "Bob" for instance, his age is a random variable between 20 and 30, but elif his name is james, his age is 30 and 40, and else his age is 40 to 50? Don't worry about the coding for the ages, I just am not sure the proper syntax for the if/elseif/else statements in that section of Python…