Is there any way to add words to a string to call a different variable?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Chevi

I have an array of strings that are randomly selected,. and I want it to add “():” to it for example:

if it chooses ‘Run’ from the array, I want it to run the Run(): function. How I’m doing it right now is

	if state == 'Run':
	Run()
if state == 'Attack':
	Attack()

But I want a way to not fill my code with a lot of this ‘if’ statements.

:bust_in_silhouette: Reply From: njamster

You can append to a string like this:

var string = "Run"
string = string + "()"  # or shorter: string += "()"

However, for your specific problem, use call:

var methods = ["Run", "Attack"]
var random_method = methods[randi() % methods.size]
call(random_method)

If the method takes arguments, use callv instead.