Page 1 of 1

[Solved] Variable scope between platforms

Posted: Thu Jan 23, 2020 10:05 am
by Jezzy
Hi,

I have a Renpy project working perfectly fine on Windows and MacOS but raising an exception when running under Linux.

I get a NameError not defined on a [None] * len(i). Linux pretend that i isnt defined and I can't figure why.

Here a simplified sniplet of my script code :

Code: Select all

init:
    $ renpy.music.register_channel("music","sfx",False)
    
$ Calendar()
$ Clock()

$ i = 0

define tracking_var = [None] * len(i)

This code will crash and raise this exception :

NameError: name 'i' is not defined

Anyone have an idea?

Re: Variable scope between platforms

Posted: Fri Jan 24, 2020 6:15 pm
by Angelo Seraphim
Consider using "default" when declaring variables and not "$" .

Code: Select all

init python:
	renpy.music.register_channel("music","sfx",False)
	
	## I'm assuming these were supposed to go here (?)
	Calendar()
	Clock()

default i = 0
This should solve your problem.

Also, if the variable is dynamic consider using "default" . If it's a constant use "define" .

Code: Select all

default tracking_var = [None] * len(i)
Also, also, "... = [None] * len(i)" doesn't really make much sense since "i" is defaulted as an "int" type variable. No real need for "len()" .

Re: Variable scope between platforms

Posted: Thu Jan 30, 2020 2:47 pm
by Jezzy
Thank you Angelo Seraphim for your answer. Your post made me realized that my mindset wasnt Pythonic enough. I was trying to store object in a Python list like an array in other languages. That is why I tried to pre-create a list with its elements set to a null value * by the len but it wasnt making sense at all as you wrote.

So in order to fix my issue, I had to rethink my script in a more pythonic way. So instead I am using .append to add stuff to my tracking variable and I'm using -1 to get the lastest iteration of my list when I need it later in my code.

Thank you for your time! It was really appreciated.

PS. I still can't understand why It was working just fine un Windows and MacOS but not in Linux though but it doesnt matter much anymore