Ren'Py Noob python question

Playstorepers

Member
May 24, 2020
160
79
Hey everyone,

I'm struggling with Python and the variables.

I don't know, how to call variables correctly.

My explicit example would be this:

Python:
$test1 = 10
$test2 = 100
$test3 = 500
$test4 = 6

for i in range(0, 4):
    $numbername = "test" + str(i)
    for j in range(0, "numbername"):
        goal = (i, j)
with goal being an array, that has to be filled to keep working with it (Beneath goal, there's shit, that's happening with goal)

In this particular example i want to have a lot of "goals"
but for this to work the for j in range has to be read, but i have to put in an integer, right?
So how do i call the test1-4 integers correctly? (numbername shall not be the string, but the integer behind test1, test2, test3, test4)

It's quite a fundamental knowledge-check, that I have yet to pass...

Thanks in advance
 
  • Like
Reactions: Randeep44

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,363
15,280
Python:
$test1 = 10
$test2 = 100
$test3 = 500
$test4 = 6

for i in range(0, 4):
    $numbername = "test" + str(i)
    for j in range(0, "numbername"):
        goal = (i, j)
There's many problems.

Where are you writing this ? In an init python block, in a label, in a python block ? The use of $ as prefix lets think that it's a label, but since for isn't part of Ren'py statements, it can't be that.
So the first problem is the said $ prefix. It have no meaning in Python, Ren'py use it only to tell that "this line" is supposed to be an inline Python code. In both Python and Ren'py, variables are just a name without prefix.

Secondly, "whatever" mean that it's a string. In Ren'py language, some statements are designed to works with "variable name" as parameters, but it's not a Python thing ; this have no meaning for Python.

What lead to the third point, "variable name" isn't how you indirectly address a variable in Python. You need to use the instruction for that.

And finally, need to be knew that Ren'py store the variable in a "store" named store (yeah, as uneasy to explain than to understand, sorry). It's transparent when you're using Ren'py language, but need to be said when you use Python. What imply that what is varName in Ren'py language, have to be store.varName in Python.


In the end, this should works (but I'm at works, so I can't test it, and I have a small doubt) :
Python:
init python:
    test1 = 10
    test2 = 100
    test3 = 500
    test4 = 6

    for i in range(0, 4):
        for j in range(0, getattr( store, "test{}".format( 4 ) ) ):
            store.goal = (i, j)
 

79flavors

Well-Known Member
Respected User
Jun 14, 2018
1,581
2,219
If I'm reading this right, you probably want .

getattr(store, numbername) will return the integer stored in the variable name contained within "numbername". Though I'm not sure you need that intermediate variable.

Also, RenPy at least keeps all it's variables within an object called store. It's effectively dumped wholesale into the save files. Actually, I think it's called renpy.store, but "store" alone has always worked for me.

So maybe something like..
You don't have permission to view the spoiler content. Log in or register now.

I am FAR from a python expert. So this might need some more thinking. And if I'm honest, I'm not following the logic of what you are trying to achieve - but hopefully this is enough to get you there.

Also check out and .

Edit: I just realized my first pass at this won't work.
I tend to advocate default for creating all changeable data variables. But if I recall correctly, default lines are processed AFTER init python:. So the variables wouldn't exist when the code needs them.
(Plus I've now read Anne's reply too).

So yeah. Something like:

Python:
init python:
    test1 = 10
    test2 = 100
    test3 = 500
    test4 = 6

    for i in range(0, 4):
        for j in range(0, getattr(store, "test" + str(i)):
            store.goal = (i, j)
 
Last edited:
  • Like
Reactions: Playstorepers

Playstorepers

Member
May 24, 2020
160
79
getattr is what I was looking for

and yes it was init python, my bad.

I just wanted to describe the problem, but you're 100% right, that my description was very confusing.

Thanks, you two
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,363
15,280
Firstly, just a thing that we both missed at first, too focused on the asked problem :
Code:
default test1 = 10
[...]
    for i in range(0, 4):
        for j in range(0, getattr(store, "test" + str(i)):
The variable goes from 1 to 4, while the range goes from 0 to 3. So it should be str( i + 1 ) or range( 1, 5 ).
create a list that goes from the first value to the value right before the last one.



And if I'm honest, I'm not following the logic of what you are trying to achieve [...]
Honestly I didn't as well. It's the fifth point, the one that I haven't addressed because failing to understand the logic behind the code:

The line goal = (i, j) imply a value assignation to goal ; and, being done at every step of the loops, this assignation reset the value of goal every single time. What mean that in the end goal will be equal to the tupple ( 4, 6), what don't need all those loops to be achieved.


So there's also a problem in the logic of the code.

If the intent is to create four lists, with each one have a variable number of entries, with each entries being it's own rank. In this case, it's already what range is doing and the code can be limited to :
Code:
    for i in range(0, 4):
        goal[i] = range(0, getattr(store, "test" + str(i+1))
goal will be a two dimensional list (array) with goal[0] being equal to [0,1,2,3,4,5,6,7,8,9] (see above if it should start at 1 and end at 10).

But if the intent is just to have four list of X entries, whatever their value, then the simplified loop approach is easier:
Code:
default goal = [ [0 for j in range( 0, 10 )], [0 for j in range( 0, 100 ) ], [0 for j in range( 0, 500 ) ], [0 for j in range( 0, 6 ) ] ]
Here, goal will be a two dimensional list with each list in its first dimension being filled by the asked number of '0'. What mean that goal[0] will be [0,0,0,0,0,0,0,0,0,0].


Edit:
Be noted that the second code imply that the first one can be just :
Code:
default goal = [ range( 0, 10 ), range( 0, 100 ), range( 0, 500 ), range( 0, 6 ) ]
 

Playstorepers

Member
May 24, 2020
160
79
Thanks for your answers and yes, I noticed the range problem soon after as well.

And as for the purpose of the code:
In the for-loop, a lot of shit happens with the "goal" array, which gets appended to another stack.

And that happens over 100 times (the actual range is 1, 150) , which is why I'm happy, that you two showed me getattr.

Thanks again.