Delayed attacks losing targets

Ive been making a script for a project im working on, and ATB system that allows skills to have a delay or recharge value added to them.

The recharge works fine, but im having problems getting the delayed skills to work properly.  Everything works just fine, the skill is saved and the battler can carry on fighting as normal, even queuing up more delayed skills, then once the timer runs down, combat pauses and the saved skill fires off, but if the original target of the skill has been killed since it was saved the screen just blinks, the mesage pops up and nothing happens.

I need to find a way of checking the saved targets variable at the ##### mark so that it can either randomize a new one or display a miss message, ive tried a few different things but none of them work.

Code:
alias non_delay_use_item use_item  def use_item(has_delay = false, i = 0)    if has_delay      @subject = DELAY.delay_a[i][1]      targets = DELAY.delay_a[i][2]      #######      item = DELAY.delay_a[i][3]      DELAY.delay_a.delete_at(i)      show_animation(targets, item.animation_id)      refresh_status      @log_window.add_text("Delayed attack hits")      targets.each {|target| item.repeats.times { invoke_item(target, item) } }    else      item = @subject.current_action.item      targets = @subject.current_action.make_targets.compact      if item.delay <= 0        Sound.play_buzzer        @log_window.display_use_item(@subject, item)        @subject.use_item(item)        refresh_status        show_animation(targets, item.animation_id)        targets.each {|target| item.repeats.times { invoke_item(target, item) } }      else        DELAY.add_item(@subject, targets, item)        @log_window.display_use_item(@subject, item)        @log_window.add_text("Attack delayed!")        @subject.use_item(item)        refresh_status      end      @subject.time_loss    end  end
I'd save them in an instance variable to the user... 
I don't get what you mean. The target data is saving fine for me.

It's just the target gets killed before the delayed skill activates then the game acts wierd.

Example-

1 actor vs 3 slimes.

Actor has a skill called missile with a delay of 15 seconds.

He fires of 2 missiles as his first 2 actions and they queue up about 5 seconds apart both aimed at slime c. Then while they are counting down he attacks and kills slimes b and c using regular attacks.

Then when the delay timer for missile 1 reaches 0 the missile hits, but because C is already dead the game gets confused.
 I thought the problem was that it was losing targets...

then add a check if the target is still alive, if yes proceed normally, if not, tell it what to do... you could trim the targets array so that it removes all non-alive targets for instance

Code:
targets_full = DELAY.delay_a[i][2]targets = []targets_full.each do |targ|  if not targ.state?(1)    targets.push(targ)  endend
Thank you, that works.

Ive changed it slightly so that it replaces any dead targets with random ones now as well.

Code:
if has_delay      @subject = DELAY.delay_a[i][1]      targets = DELAY.delay_a[i][2]      item = DELAY.delay_a[i][3]      targets.each do |t|        if t.state?(1)          targets.delete(t)          targets.push(@subject.opponents_unit.random_target)        end      end            DELAY.delay_a.delete_at(i)      show_animation(targets, item.animation_id)      refresh_status      @log_window.add_text("Delayed attack hits")      targets.each {|target| item.repeats.times { invoke_item(target, item) } }   .....
Ooh... never knew you could remove while iterating from the same array... That could be dangerous.. XD anyway glad that you got it working now. 🙂
I've swapped the .delete and .push around now, so at the very least there will always be something in the array.  But ive tested it out in every way i can think of and it works nicely.

I just need to make a HUD now that shows the missiles travelling across the screen, right now i have temp icons moving all over the place.

Original thread: https://forums.rpgmakerweb.com/threads/delayed-attacks-losing-targets.35295/