[Solved] Proper use of variable as class name

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
nananame
Regular
Posts: 72
Joined: Fri Oct 13, 2017 1:40 pm
Contact:

[Solved] Proper use of variable as class name

#1 Post by nananame »

I can't seem to figure out the correct syntax to call a class through a variable.

Code: Select all

default Plaza = Place("something","something","THIS")

label makeCheck(where):
    "[where]" ####The 'where' variable holds "Plaza" Here I just check that it is so and indeed it is
    if where.when == "THIS":
    	#do stuff
   	
Now I know this isn't the correct way to go about it because it will look for object Place called 'where'.
The error I get is 'unicode' object has no attribute 'when'.

Of course, if I write:

Code: Select all

if Plaza.when == "THIS":
it works.

So basically what is the right syntax to get Plaza.when from where.when?

I tried:
if (where).when
if [where].when
if "[where]".when
if ("[where]".when)
and anything else I can think of.
Last edited by nananame on Sun May 19, 2019 2:07 pm, edited 1 time in total.

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: Proper use of variable as class name

#2 Post by trooper6 »

What is the rest of your code?
Where/how do you define the class Place.
Where/how do you define the variable where?
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

nananame
Regular
Posts: 72
Joined: Fri Oct 13, 2017 1:40 pm
Contact:

Re: Proper use of variable as class name

#3 Post by nananame »

Just got it working a minute ago using eval()
The rest of the code isn't relevant (Place is a class defined in init python and variable 'where' is passed through the call label)

Code: Select all

label makeCheck(where):
    python:
	    if eval(where).when == "THIS":
    		#do stuff
So this works. Basically the question should of been: how to use a string (because variable 'where' is a string) to reference class object.

Is there perhaps another way without resorting to python here that I missed? This is of course short and works fine but just wondering at this point.

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Proper use of variable as class name

#4 Post by Remix »

getattr( store, where ).when
is probably the most Ren'Py centric

Why is where a string though? The problem would seem to be in what you are passing into the screen (a string rather than an object)
Frameworks & Scriptlets:

nananame
Regular
Posts: 72
Joined: Fri Oct 13, 2017 1:40 pm
Contact:

Re: Proper use of variable as class name

#5 Post by nananame »

Remix wrote: Sun May 19, 2019 12:34 pm getattr( store, where ).when
is probably the most Ren'Py centric

Why is where a string though? The problem would seem to be in what you are passing into the screen (a string rather than an object)
Thanks for the info!
Why a string? Quite complicated I'm afraid, it's a complex logically textual game and the actual string is a part of a longer sentence. Main thing is I now got two ways to go about it.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Majestic-12 [Bot]