Modify tower stats at runtime (attack speed)



  • Thank you so much.

    Now my question wont really fit the title anymore, but;

    If i want to modify the tower attack speed in real time, how should i approach it?

    I've added a variable of workers to towers, which is supposed to increase tower attack speed, based on the amount of workers on said tower.
    But modifying the "shoot delay" variable, does not seem to change anything, when done in real time.

    Please help!

    (Thanks for moving it ^^ )



  • Towers attack in intervals, based on the shootDelay variable. If an enemy enters the range of the tower, the interval starts.

    Now if you modify the shootDelay variable and the tower has already started its attack interval, your change will not have any effect. In this case you have to restart the interval. You could do that by adding a new method to the TowerBase script, and calling it after you've modified the shootDelay value.

    public void RestartAttackInterval() {   CancelInvoke("CheckRange");   StartInvoke(0); }

    Note that if you restart the interval too fast, your tower will not attack at all - because the delay has to run through.



  • If you have access to the tower's upgrade script, you should have access to its base script too?



  • Yes, sorry, i figured it out. :)



  • The tower now succesfully changes its attack speed according the the worker count, but whenever i add/remove a worker in realtime, the restart method will be called.

    This results in a spam-shoot function. Any ideas on how to get around it?



  • This results in a spam-shoot function. Any ideas on how to get around it?

    The value you're passing in StartInvoke() is an additional start delay of the interval. If you're using StartInvoke(2) for example, your tower will start shooting after 2 seconds and then use the shootDelay value.


Log in to reply