[General] [Jass] creating templates with dynamic functions - best practice

Level 1
Joined
Sep 1, 2015
Messages
3
Hey guys, I could use your help:

I am currently working on an auto-battler map that lets you customize your army in many different ways. The map utilizes unit templates, which are used to create either base buildings or minions. (A base can be equipped with items/abilties, upgraded, .. and will spawn a corresponding minion during each round). Each minion/base has a bunch of unique function calls but to keep it simple, I'll only focus on basic attacks (which are only relevant for minions).

I started storing unit templates as following:

JASS:
function interface func_ba takes unit source, unit target returns nothing

struct unit_template extends array
    
    readonly string name
    readonly integer unit_id
    readonly integer race
    ..
    readonly func_ba basic_attack

    private static integer idx = -1
        
    static method load_instance takes nothing returns thistype
        set thistype.idx = thistype.idx + 1
        return thistype[thistype.idx]
    endmethod
    
endstruct

//! textmacro run_template takes name
    
        globals
            unit_template Template_$name$
        endglobals
        
        private function init takes nothing returns nothing
        
            set Template_$name$ = Template.load_instance()
            set Template_$name$.name = name
            ..
            set Template_$name$.func = basic_attack
        endfunction
    
//! endtextmacro

which are loaded through libraries/scopes (does it make a difference?) such as:

JASS:
library TEMPLATEFOOTMAN initializer init

    globals

        private constant string  name             = "Footman"
        private constant integer unit_id          = 'H000'
        private constant integer race             = RACE_TYPE_HUMAN
        ..
    endglobals

    public function basic_attack takes Minion source, Minion target returns nothing
        // deal splash damage, fire projectiles, etc..
    endfunction
 
    //! runtextmacro run_template("Footman")

endlibrary

that way I can spawn bases and minions via

JASS:
call Minion.create(Template_Footman, Player_Hostile, x, y, f)
call Base.create(Template_Footman, Player_Hostile, x, y, f)

But function.evaluate() calls for stuff like basic attacks are pretty slow from what I've gathered, so is there a better way to do this?
I thought about utilizing child structs such as

JASS:
interface Unit
    method basic_attack takes Unit target returns nothing defaults nothing
    ..
endinterface

struct Minion_Footman extends Unit

    readonly static constant name = "Footman"
    readonly static constant integer unit_id = 'H000'
    readonly static constant integer race = RACE_TYPE_HUMAN
    ..

    method basic_attack takes Minion target returns nothing
        //do stuff
    endmethod
endstruct

and storing each child creation in function interfaces through textmacros. That would make the basic_attack calls faster, but I would have to create a child for every single base and every single minion which is extremely cumbersome.

What is best practice for stuff like this? Do you have any suggestions for me?
Cheers
 
Back
Top