Bug with selected unit

There’s a weird bug with the selected unit function, I’m not sure how to explain it so I’m just gonna explain it using images:


So you see this script right? When it activates it sends all “ghosts” to (0,0). But then it only sends ONE “student” to the center of the map, when it should send all of the students to the center of the map. But when I use a global unit variable instead of selected unit, it teleports all students to the center of the map, when it should’ve teleported all students to the center of the map with the selected unit function as well. Please fix this bug, cuz I spent like 30 minutes figuring this out

3 Likes

hello @FootSoldier try his and see if it work for you and me


if it do then hart and comment if it work!

2 Likes

This script does not solve FootSoldier’s problem…

4 Likes

The inner loop is probably overriding the selected unit variable of the outer loop. You can move the outer loop’s reference to selected unit above the inner loop, or store the outer loop’s value of selected unit in a global variable before running the inner loop.

3 Likes

image

2 Likes

You can circumvent the override selected unit by writing as following while reducing time complexity dramatically:

if (number of Student >= 1) {
 for all units in the game {
  if (unit type of selected unit == Ghost) {
   move selected unit to (0, 0)
  } else if (unit type of selected unit == Student) {
   move selected unit to center of entire map region
  }
 }
} else {
  // do nothing
}

Explanation

What you want to do is that

  1. Move all students to the center of the map.
  2. i) if there is any student, more all ghosts to (0, 0).
    ii) if there is no student, do not move them to (0, 0).

It’s equal to
i) if there is any student, more all ghosts & students to (0, 0).
ii) if there is no student, do not move any ghost to (0, 0). It means doing nothing (no student + no ghosts to move).

(n = number of all units in the game)
Your script requires O(n^2) time complexity because there’s a double for loop, and the ghosts will end up moving the number of students times. But mine will reduce time complexity to O(n) and each ghost will not move more than one time.

2 Likes

oh yeah uh by the way i only made this forum post because i think in t2 engine it doesnt do this, idk i forgot but it doesnt really matter now and thinking back this is a dumb post lol

2 Likes