Topic category: Help with MCreator software
How would i go about finding and calling a procedure dynamically by its name? Looking through the generated code, i can see that this block:
generates the code SpellFireballProcedure.execute(spellLevel);
(assuming the procedure needs the dependency). This doesn't let me change what procedure it calls at runtime. My question is, what code would a block that looks like this:
generate, assuming the given procedure will always require, in this case, level as a number? Making an omega-if-tower is certainly an option but not one i'm willing to resort to just yet.
The closest i think i've gotten is with some variations of the code
Class.forName("net.mcreator.orchidspells.procedures.
SpellFireballProcedure").execute(spellLevel);
but that generates an error i don't fully understand:
> Task :compileJava FAILED
---/MCreatorWorkspaces/orchidspells/src/main/java/net/mcreator/orchidspells/procedures/StaffRightclickedProcedure.java:47: error: cannot find symbol
Class.forName("net.mcreator.orchidspells.procedures.SpellFireballProcedure").execute(spellLevel);
^
symbol: method execute(double)
location: class Class<CAP#1>
where CAP#1 is a fresh type-variable:
CAP#1 extends Object from capture of ?
1 error
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler output below.
---/MCreatorWorkspaces/orchidspells/src/main/java/net/mcreator/orchidspells/procedures/StaffRightclickedProcedure.java:47: error: cannot find symbol
Class.forName("net.mcreator.orchidspells.procedures.SpellFireballProcedure").execute(spellLevel);
^
symbol: method execute(double)
location: class Class<CAP#1>
where CAP#1 is a fresh type-variable:
CAP#1 extends Object from capture of ?
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error
* Try:
> Check your code and dependencies to fix the compilation error(s)
> Run with --scan to get full insights.
BUILD FAILED in 217ms
(Might potentially have something to do with the fact execute() is a static function? Not sure what to do with that though)
I'm convinced that this is possible but i have barely any understanding of java and i think my brute force strategy hit its limit.
Figured it out, what worked for me is
Class.forName("net.mcreator.orchidspells.procedures.SpellFireballProcedure").getMethod("execute", double.class).invoke(null, spellLevel);
To use it in your own project, replace "orchidspells" with your mod's id, "SpellFireballProcedure" with your procedure's name + "Procedure", "double.class" with a list of your target procedure's dependencies' classes in order starting with world, then xyz, then the rest from the top (so a dependency list of "entity, itemstack, x, y, z, world" would be "LevelAccessor.class, double.class x3, Entity.class, ItemStack.class"), and finally replace spellLevel with a list the variables you want to pass to the procedure in the same order as before. Hopefully this eventually helps someone.