• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Integer issue

Status
Not open for further replies.
Level 12
Joined
Dec 2, 2016
Messages
733
So the following code is for a tower that when it attacks a unit, it will add -x armor to the unit. The -armour depends on the type of the tower, as there is two towers I'm using. One being -1 armor, the upgraded tower being -2 per hit.

So I detect when a unit is attacked, and then use 'OnAttack()' to check which unit the attacker is. If he's tower#1 (-1 armor) then set boolean 'towerType' to 1. If he's tower#2 then set boolean 'towerType' to 2.

Now I've tested with messaging inside the OnAttack() function, and it does work like if the attacker is 'Tower1' it will output 'Tower1' and if attacker is 'Tower2' then it outputs 'Tower2'.

But this part

JASS:
   OnAttack();
   if (towerType == 1) {
   DisplayTextToForce( GetPlayersAll(),GetUnitName(d)+"-1"   );
   AddUnitBonus(u, BONUS_ARMOR, -1);
   }
   if (towerType == 2) {
   DisplayTextToForce( GetPlayersAll(),GetUnitName(d)+"-2"   );
   AddUnitBonus(u, BONUS_ARMOR, -2);
   }

Even though the 'OnAttack()' function should be changing the Boolean to 2. The 2nd part

JASS:
   if (towerType == 2) {
   DisplayTextToForce( GetPlayersAll(),GetUnitName(d)+"-2"   );
   AddUnitBonus(u, BONUS_ARMOR, -2);
   }

Never seems to run. For some reason my boolean isn't changing or my 2nd if statement here isn't running properly. Is there anything wrong you guys can see? Did I code this wrong? Thanks. Full code below:


JASS:
library bloodTower requires libMisc, libSetup, unitClassification, BonusMod {
public integer towerType;
HandleTable bloodTowersTable;
timer t = null;
integer total = 0;
bloodTowerData bloodTowers[];


private function OnAttack() -> boolean {
  unit u = GetTriggerUnit();
  unit d = GetAttacker();
 
     if (GetUnitTypeId(d) == 'hT0a' ) {
    // AddUnitBonus(u, BONUS_ARMOR, -1);
    towerType = 1;
    //DisplayTextToForce( GetPlayersAll(),GetUnitName(d)+"-1"   );
    return true;
 }
   if (GetUnitTypeId(d) == 'h00S' ) {
    // AddUnitBonus(u, BONUS_ARMOR, -2);
    towerType = 2;
    //DisplayTextToForce( GetPlayersAll(),GetUnitName(d)+"-2"   );
    return true;
 }
  return false;
 
 
}



public function CheckBloodTower(unit u) {
  //unit u = GetTriggerUnit();
  unit d = GetAttacker();
 

   OnAttack();
   if (towerType == 1) {
   DisplayTextToForce( GetPlayersAll(),GetUnitName(d)+"-1"   );
   AddUnitBonus(u, BONUS_ARMOR, -1);
   }
   if (towerType == 2) {
   DisplayTextToForce( GetPlayersAll(),GetUnitName(d)+"-2"   );
   AddUnitBonus(u, BONUS_ARMOR, -2);
   }

}

private function onInit() {
trigger trg;
  bloodTowersTable = HandleTable.create();
 
  trg = TriggerCreateOnGameStarting();
   TriggerRegisterAnyPlayerUnitEvent(trg, EVENT_PLAYER_UNIT_ATTACKED, null);
   TriggerAddCondition(trg, Condition(function OnAttack));
 
 
}
}


Edit: So I added this in

JASS:
DisplayTextToForce( GetPlayersAll(),I2S(towerType)   );

to check what the variable was at, it outputs '1' when Tower#1 and doesn't output anything when Tower#2

So for some reason my code is not changing 'towerType' to 2 when it's the Tower#2 attacking.
 
Last edited:
Level 12
Joined
Dec 2, 2016
Messages
733
Try initializing the variable public integer towerType = 0;

I did that and still no message when it's tower 2 attacking. So when OnAttack() runs, it must be preventing the rest of the code from running. Because the message below should at least output '0'.

JASS:
public function OnAttack() {
  unit d = GetAttacker();
 
   if (GetUnitTypeId(d) == 'hT0a' ) {
    towerType = 1;

 }
   if (GetUnitTypeId(d) == 'h00S' ) {
    towerType = 2;
 }

 
 
}

The error must be in this function, I guess changing 'towerType' to 2 is the culprit, since changing it to 1 works fine.


btw, this is the entire full code that I'm running. I edited out the unnessasary parts in the previous posts. But let me know if you see anything that might interfere with this.


JASS:
library bloodTower requires libMisc, libSetup, unitClassification, BonusMod {
public integer towerType = 0;
HandleTable bloodTowersTable;
timer t = null;
integer total = 0;
bloodTowerData bloodTowers[];


function onTimer() {
  integer n = 0;
  integer i = 0;
  bloodTowerData btd;
 
  while (i < total) {
    btd = bloodTowers[i];

    if (btd.qb() == true) {
      bloodTowers[n] = btd;
      n = n+1;
    }
    else {
      btd.destroy();
    }
    i = i + 1;
  }
  total = n;
 
  if (n == 0) {
    PauseTimer(t);
  }
}

struct bloodTowerData {
  unit u;
  real lastHitTime;

  static method create(unit u) -> bloodTowerData {
    thistype this = thistype.allocate();
    this.u = u;
    this.lastHitTime = 5.0;
    bloodTowersTable[this.u] = this;

   
    this.sb();
    //DisplayTextToForce( GetPlayersAll(),GetUnitName(u)  );
    return this;
  }
 
  method sb() {
    bloodTowers[total] = this;
    total = total + 1;
   
    if (total == 1) {
      if (t == null) {
        t = CreateTimer();
      }
      TimerStart(t, 0.1, true, function onTimer);
    }
  }
 
  method qb() -> boolean {
    this.lastHitTime = this.lastHitTime - 0.1;
   // DisplayTextToForce( GetPlayersAll(),GetUnitName(this.u)  );
    if (IsUnitType(this.u, UNIT_TYPE_DEAD) || GetUnitTypeId(this.u) == 0) {
      bloodTowersTable.flush(this.u);
      this.u = null;

      return false;
    }
   
    if (this.lastHitTime < 0) {
      RemoveUnitBonus(this.u, BONUS_ARMOR);
      bloodTowersTable.flush(this.u);
      this.u = null;
      return false;
    }
   
    return true;
  }
}

public function OnAttack() {
  unit u = GetTriggerUnit();
  unit d = GetAttacker();
 
     if (GetUnitTypeId(d) == 'hT0a' ) {
    towerType = 1;
 }
   if (GetUnitTypeId(d) == 'h00S' ) {
    towerType = 2;
 }
 // return false;
 
 
}


public function CheckBloodTower(unit u) {

  unit d = GetAttacker();
 
    bloodTowerData btd;
  OnAttack();
     
  if (bloodTowersTable.exists(u)) {
    btd = bloodTowersTable[u];
    btd.lastHitTime = 5.0;
  }
  else {
    bloodTowerData.create(u);
  }
   
   DisplayTextToForce( GetPlayersAll(),I2S(towerType)   );
   if (towerType == 1) {
   DisplayTextToForce( GetPlayersAll(),GetUnitName(d)+"-1"   );
   AddUnitBonus(u, BONUS_ARMOR, -1);
   }
   if (towerType == 2) {
   DisplayTextToForce( GetPlayersAll(),GetUnitName(d)+"-2"   );
   AddUnitBonus(u, BONUS_ARMOR, -2);
   }

}



private function onInit() {
trigger trg;
  bloodTowersTable = HandleTable.create();
 
  trg = TriggerCreateOnGameStarting();
   TriggerRegisterAnyPlayerUnitEvent(trg, EVENT_PLAYER_UNIT_ATTACKED, null);
   TriggerAddAction(trg, (function OnAttack));

 
}




}
 
Last edited:
Level 9
Joined
Jul 30, 2018
Messages
445
Are you not having any errors? I may be off here, but that doesn't look like JASS at all. It should look something like:

JASS:
if (towerType == 1) then
   DisplayTextToForce( GetPlayersAll(),GetUnitName(d)+"-1"   )
   AddUnitBonus(u, BONUS_ARMOR, -1)
endif

Also, there is no semicolons in JASS.
 
Level 12
Joined
Dec 2, 2016
Messages
733
Are you not having any errors? I may be off here, but that doesn't look like JASS at all. It should look something like:

JASS:
if (towerType == 1) then
   DisplayTextToForce( GetPlayersAll(),GetUnitName(d)+"-1"   )
   AddUnitBonus(u, BONUS_ARMOR, -1)
endif

Also, there is no semicolons in JASS.
It's Zinc.
Hmm, I suppose having function onAttack as the callback function might be the culprit behind this. Try changing the action callback to function CheckBloodTower

I fixed it thanks.
 
Status
Not open for further replies.
Top