[SOLVED] Return a Function with a variable 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
User avatar
Discostar
Regular
Posts: 35
Joined: Mon Mar 10, 2014 10:29 am
Contact:

[SOLVED] Return a Function with a variable name?

#1 Post by Discostar » Mon Dec 02, 2019 12:08 pm

I'm not even sure how to search for the answer to this question.

I have a cool function that returns another function.

Code: Select all

def PushTheButton():
	Cool_Func()
	return
Works great.
BUT what I want to do is have multiple iterations of Cool_Func with different names:

Code: Select all

def Cool_Func():
	#whatever...
	return
	
def Awesome_Func():
	#whatever...
	return

def Hella_Func():
	#whatever...
	return
How can create a function that will return a function based on a variable name?

What I want to do is something like this:

Code: Select all

$ type = "Awesome"

**************************

def PushTheButton():
	"%s"%type+_Func()
	return
With the intent of running the Awesome_Func.

Any ideas?
Last edited by Discostar on Sun Dec 08, 2019 2:04 pm, edited 1 time in total.

User avatar
Per K Grok
Miko-Class Veteran
Posts: 674
Joined: Fri May 18, 2018 1:02 am
Completed: the Ghost Pilot, Sea of Lost Ships, Bubbles and the Pterodactyls, Defenders of Adacan Part 1
Projects: Defenders of Adacan Part 2
itch: per-k-grok
Location: Sverige
Contact:

Re: Return a Function with a variable name?

#2 Post by Per K Grok » Mon Dec 02, 2019 12:48 pm

Discostar wrote:
Mon Dec 02, 2019 12:08 pm
I'm not even sure how to search for the answer to this question.

I have a cool function that returns another function.

Code: Select all

def PushTheButton():
	Cool_Func()
	return
Works great.
BUT what I want to do is have multiple iterations of Cool_Func with different names:

Code: Select all

def Cool_Func():
	#whatever...
	return
	
def Awesome_Func():
	#whatever...
	return

def Hella_Func():
	#whatever...
	return
How can create a function that will return a function based on a variable name?

What I want to do is something like this:

Code: Select all

$ type = "Awesome"

**************************

def PushTheButton():
	"%s"%type+_Func()
	return
With the intent of running the Awesome_Func.

Any ideas?


I'm not sure if this is in line with what you want to do, but you could have a parameter to your base function that could determine what other function would be called.

Code: Select all


def PushTheButton(a):
	if a==0:
		Cool_Func()
	elif a==1:
		Awesome_Func()
	else:
		Hella_Func()
	return




philat
Eileen-Class Veteran
Posts: 1732
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Return a Function with a variable name?

#3 Post by philat » Mon Dec 02, 2019 12:53 pm

Leaving aside that PushTheButton isn't returning anything... rather unclear what you're trying to do but seems likely that a different method would be better, e.g., storing the function in the variable directly or organizing all "awesome" or "cool" data into an object which can be more easily referenced (Awesome_object.type = "awesome" and Awesome_object.method() rather than type = "awesome" and Awesome_func(), etc.).

User avatar
Discostar
Regular
Posts: 35
Joined: Mon Mar 10, 2014 10:29 am
Contact:

Re: Return a Function with a variable name?

#4 Post by Discostar » Mon Dec 02, 2019 1:22 pm

Per K Grok wrote:
Mon Dec 02, 2019 12:48 pm

I'm not sure if this is in line with what you want to do, but you could have a parameter to your base function that could determine what other function would be called.

Code: Select all


def PushTheButton(a):
	if a==0:
		Cool_Func()
	elif a==1:
		Awesome_Func()
	else:
		Hella_Func()
	return

I should have mentioned, I did this, and it works, but I don't know how many iterations I will need. It would be easier if I could get it to just find the function just based on the name given in a variable.

I will default to this method if I can't find another solution.

strayerror
Regular
Posts: 111
Joined: Fri Jan 04, 2019 3:44 pm
Contact:

Re: Return a Function with a variable name?

#5 Post by strayerror » Mon Dec 02, 2019 3:58 pm

You could just assign the functions directly, thus:

Code: Select all

python:
    def Cool_Func():
        #whatever...
        return
	
    def Awesome_Func():
        #whatever...
        return

    def Hella_Func():
        #whatever...
        return

$ PushTheButton = Awesome_Func

$ PushTheButton() # this will exec Awesome_Func

[...]

$ PushTheButton = Hella_Func

$ PushTheButton() # now this will exec Hella_Func
However if for some reason you're bound to using $ type = "Awesome", you could do this:

Code: Select all

$ type = "Awesome"

def PushTheButton():
    fn = '{}_Func'.format(type)
    if hasattr(store, fn):
        getattr(store, fn)()
Finally, as an aside, type isn't a great variable name, and will mask the python builtin type() function, so consider choosing another name for this variable.

User avatar
Discostar
Regular
Posts: 35
Joined: Mon Mar 10, 2014 10:29 am
Contact:

Re: [SOLVED] Return a Function with a variable name?

#6 Post by Discostar » Sun Dec 08, 2019 2:05 pm

@strayerror: That does the trick! Thank you.

Post Reply

Who is online

Users browsing this forum: hell_oh_world