Can't assign to function call

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
city
Newbie
Posts: 23
Joined: Mon Jul 04, 2016 6:30 pm
Contact:

Can't assign to function call

#1 Post by city » Fri Mar 08, 2019 8:19 pm

I'm creating a card mini-game and I'm trying to make the code as efficient as possible to avoid redundancies where I can (and to hopefully better understand the software in the process). I've created a large class, and there are a ton of variables that are constantly changing by player choices, and I'm defining most of the variables by other variables, so the class variables stay consistent as card position changes on the field/hand/deck/discard pile. There are over a hundred cards, and each one can do multiple things, and the player will find them throughout the real game in an unpredictable way, and will build their own unique deck, so coding for every single possibility is not a good idea.

Here's the class (condensed for ease) and some example variables:

Code: Select all

init python:
    class Card:
        def __init__(self, cardnr, cname, card_bg, location):
            self.cardnr = cardnr
            self.power = cname
            self.card_bg = card_bg
            self.location = location
            
init:
	$ card1 = Card(1,"fireball","fire.png",1)
	$ card2 = Card(2,"ice spike","ice.png",1)
	$ p1 = "none"
	$ p6 = "none"
	$ pcard1_xpos = 100 
	$ pcard1_ypos = 200
	$ pcard6_xpos = 400
	$ pcard6_ypos = 500
Now, here's the rest of the background functioning code:

Code: Select all

screen hand:
	imagemap:
		ground "trans_ground.png" #transparent
		hover "blue_hover.png"
                         
        	if p1 != "none":     ### First slot in hand
			hotspot (pcard1_xpos, pcard1_ypos, 93, 133)  action [SetVariable(hand_select,1), Return()]

screen field:
	imagemap:
		ground "trans_ground.png" #transparent
		hover "blue_hover.png"
                         
        	if p6 == "none":     ### First slot on field
			hotspot (pcard6_xpos, pcard6_ypos, 93, 133)  action [SetVariable(field_select,6), Return()]

label play_cards:
	$ cardlist = [card1,card2]
	$ renpy.random.shuffle(cardlist)
	$ p1 = cardlist.pop() ### first card drawn, randomized
	jump card_background
	
label card_background:
	if p1 != "none:
		show image p1.card_bg:
			xpos pcard1_xpos ypos pcard1_ypos 
		
	call screen hand
	call screen field
	jump card_equalize
Now's when it gets weird. If I do this:

Code: Select all

label card_equalize:
	$ p6 = p1
	$ p1 = "none"
	$ p6.location = 2
Everything's great! But I'd have to do this for literally thousands of possibilities, so a string is helpful, but when I streamline it:

Code: Select all

$ (eval("p"+str(field_select))) = (eval("p"+str(hand_select)))  ### $p6 = p1
$ (eval("p"+str(hand_select))) = "none"  ### $p1 = "none"
$ (eval("p"+str(field_select))).location = 2  ### $p6.location = 2
I get a "Can't assign to function call" error. If I try to use another variable as the go-between:

Code: Select all

$ temp_var = (eval("p"+str(hand_select)))
$ (eval("p"+str(field_select))) = temp_var
I still get the error. How would I go about having the result of one function equal another function?

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: Can't assign to function call

#2 Post by Remix » Fri Mar 08, 2019 8:49 pm

Firstly, SetVariable uses a string as the variable name ... SetVariable( "variable_name", value )

Second, try to avoid using eval where possible. Even when only eval'ing your own code rather than user input it is not a coding practice to become complacent with.
For most, if not all of your use-cases, try perhaps setattr and getattr:

setattr( globals(), "p"+str(field_select), getattr( globals(), "p"+str(hand_select) ) )
...
setattr( getattr( globals(), "p"+str(field_select) ), "location", 2 )
Frameworks & Scriptlets:

city
Newbie
Posts: 23
Joined: Mon Jul 04, 2016 6:30 pm
Contact:

Re: Can't assign to function call

#3 Post by city » Fri Mar 08, 2019 9:16 pm

Thanks for replying, Remix! When I put it in, I get this error:

AttributeError: 'StoreDict' object has no attribute 'p1'

Any ideas? And do you have any suggestions for replacing:

Code: Select all

$ (eval("p"+str(hand_select))) = "none" 
with something that works?

Post Reply

Who is online

Users browsing this forum: Google [Bot]