I wanted to know if there were a way to use strings to determine which proc to call. Not in the context of an if statement, but in this way. I know MOO, so I know how to do it there, but I don't know if BYOND has the same capability.
number = rand(1, 5)
numstring = "[number]"
vstring = "verb_" + numstring
vstring()
Is there code to then call the resulting verb, other than using switch() or a string of if statements?
If you know both that the target object has the variable and that the variable's name has been resolved at compile-time:
datum.var_name //compile-time access
If you don't know whether the target object has the variable, and the variable's name hasn't been resolved at compile-time: datum.vars["var_name"] //runtime instance access If you don't know whether the target object has the variable, but the variable's name has been resolved at compile-time:
datum:var_name //runtime access
|
call()() is unsafe because it bypasses compile-time checks for whether an object actually has the proc you want to call (which is what hascall() is for, I guess).
Fortunately, there are better, safer alternatives that are more object-oriented, such as the "Command" pattern:
In an action RPG, active skills can be like these "action" datums. You could implement a hotbar as a simple array of action datums, and when a hotkey is pressed, you perform the action bound to that hotkey.
More info here: http://gameprogrammingpatterns.com/command.html