Best way to dynamically call function
For my project, I have callable
objects (arbitrary NBT dict that is theoretically capable of calling an mcfunction) stored on an NBT array (the Stack).
At an arbitrary time in execution I want to be able to pop the callable off the Stack and somehow use it to call a function.
My original thought was to just store a string like "function ns:fn_name"
and then data modify...
that string into a command block and run it.
Problem is, command blocks execute on the next tick and there seems to be no way around this.
My next thought would be to have an invoker
function that contains every possible function call, and a chain of execute if data stack[0]{name:<function_name>} run function ns:<function_name>
, where stack[0]
is the callable at the top of the stack, and the callable would store the name of its desired function in callable.name
.
My problem with this is efficiency. For the number of defined functions n, it's potentially adding n checks every function call and that sucks.
However, I can't think of a better way to call an arbitrary function like this.
Pictures about "Best way to dynamically call function"
How do you dynamically call a function?
function MyClass() { this. abc = function() { alert("abc"); } } var myObject = new MyClass(); myObject["abc"](); If what you want to do is not only dynamically call a function but also dynamically create a named function, it is also best done with the window object using either: window['name'] = function() { ... }What are the different ways to call a function?
But for functions with arguments, we can call a function in two different ways, based on how we specify the arguments, and these two ways are: Call by Value. Call by Reference.How do you create a dynamic function in Python?
Python Code can be dynamically imported and classes can be dynamically created at run-time. Classes can be dynamically created using the type() function in Python. The type() function is used to return the type of the object. The above syntax returns the type of object.What is dynamic function example?
Dynamic function is a way of dynamically invoking a function call. The compiler will have limited knowledge of what you are up to so you will get run time errors if you don't use correct inputs and outputs. One example that runs different functions depending on user input: DEFINE VARIABLE iFunc AS INTEGER NO-UNDO.Call function dynamically in Javascript - NodeJS
Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.
Images: Kamaji Ogino, Kamaji Ogino, Kamaji Ogino, Erik Mclean