Home > front end >  is it good to use delegate to implement special items in unity game?
is it good to use delegate to implement special items in unity game?

Time:06-16

i had trouble implementing special items in my game

for example when i make sword that shoot laser every 3 attack instead of swinging it

the script must know how much time player triggered attack. this is where i got problem

i cant just add count() method or something like that

if so, i have to check if player actually equipped the item

and i have to check for every item i add to this game. this is clearly not good

so i come up with a idea but not sure if it's recommendable way to do it

my idea is like this (not real code)

playerScript

//some properties like hp, speed
//...
weapon equippedWeapon;
delegate void del_attack();
private del_attack whenAttack = new del_attack();

public void Attack() 
{
    whenAttack();
}

public void equipWeapon(weapon weapon)
{
    this.weapon = weapon;
    whenAttack  = weapon.fire;
}

weaponScript

class laserBlade : weapon
{
    //some property like damage
    //...
    playerScript playerScript = GameObject.Find("player").getComponent<playerScript>();
    int chargeStack = 0;
    void override fire()
    {
        this.chargeStack  = 1;
         //some other code
    }
    void whenPicked()
    {
        playerScript.equipWeapon(this);
    }
}

but even if it works. there's still a problem

let's imagine that i write code to implement spike armor. spike armor reflect certain percent of damage that player take.

so script must know who hit the player.

And something similar can happen over over again whenever i want any effect at specific event

i'm worrying about my code looking like this.

del_attack whenAttack = new del_attack();
del_onHit whenHit = new del_onHit(enemy enemy);
del_getDamage whenDamaged = new del_getHit();
del_getHit whenGetHit = new del_getHit(enemy enemy);
del_dead whenDied = new del_dead();
// and a lot of other delegate

i think this can overload the entire program

is this okay to do? or there's better way to do same thing?

plz help

CodePudding user response:

The code you have here does a lot of referencing back and forth between the two scripts which is kind of what delegates and actions are supposed to help you avoid. I think you have the right idea with using delegates. You should remember to also unregister the listeners to the delegates though with -= somewhere probably when unequipping and definitely in OnDestroy to keep it from memory leaking.

Only thing I would probably do differently is use static actions instead of delegates since there's a little bit less boilerplate needed but it's mostly a subjective style thing. Means your declarations would be one line each rather than two so the bit at the top you're worried about being messy shouldn't be any more so than any other class variables.

I'm not sure if this is the sort of answer you're looking for. It's a well written question with a fair premise though so I thought it deserved some thought and responses. I think you have the right idea of using events like this to allow wide ranging effects to be triggered by the same actions without them having to know about each other. The alternative would be closer coupling with object references for each individual case.

In any case, this has been my favourite resource on ways of achieving decoupling using events and delegates. It's a bit more on the mechanical side of the how rather than the why but it's pretty well put together and clear so maybe you can see how some approaches benefit you or don't in your situation. https://gamedevbeginner.com/events-and-delegates-in-unity/

  • Related