| 07-21-2005, 10:13 AM | #1 |
Why does this function (UnitAddAbility(whichUnit, abilityId)) return a boolean? does it matter if i ignore the returned boolean? could someone look at the code i've written and see if there could be any problems? Code:
function NumofBonuses takes nothing returns integer
return 11
endfunction
function MaxBonus takes nothing returns integer
return 1024
//add 1 to all your positive bonuses added together
endfunction
function MinBonus takes nothing returns integer
return -1024
//your negative bonus
endfunction
function IndexBonus takes string a returns integer
if (a == "Dam") then
return 0
elseif (a == "Mp") then
return 1
elseif (a == "Hp") then
return 2
elseif (a == "Arm") then
return 3
elseif (a == "Str") then
return 4
elseif (a == "Int") then
return 5
elseif (a == "Agi") then
return 6
else
return 7
//If there is an error, no bonus is added
endif
endfunction
//You type in what bonuses are to be set, and then it returns the start of the type of
//bonuses you want in the 2d array, which is useful for...
function AddBonustoUnit takes string whattype, unit u, integer howmuch returns nothing
local integer i = IndexBonus(whattype)
local integer bit1 = MaxBonus()/2
local integer bit2 = 1
local integer a = (i*NumofBonuses())
if (howmuch >= MaxBonus()) then
set howmuch = MaxBonus()-1
elseif (howmuch < MinBonus()) then
set howmuch = MinBonus()-1
endif
//if it's too big/small, it corrected
if (howmuch < 0) then
set howmuch = howmuch - MinBonus()//Adds the stuff so it's positive
call UnitAddAbility(u, udg_AdvancedAbils[(i*NumofBonuses())+(NumofBonuses()-1)])//2d array time!
endif
//start adding the bonuses!
loop
exitwhen (a == (i*NumofBonuses()+10))
call UnitRemoveAbility(u,udg_AdvancedAbils[i*NumofBonuses()+a] )
set a = a + 1
endloop
loop
exitwhen (howmuch == 0)
if (howmuch > bit1) then
call UnitAddAbility(u, udg_AdvancedAbils[(i*NumofBonuses())+((NumofBonuses() - 1) -bit2)])
set howmuch = howmuch - bit1
endif
set bit1 = bit1/2
set bit2 = bit2+1
endloop
endfunctionEDIT: when i use this, it lags for ages the first time, and then kills the unit (even on the second time) :/ How would i fix this? But, it seems to work if you set the bonus to -1024 |
| 07-21-2005, 11:54 AM | #2 |
It lags the first time, as with some abilities, they lag when being added or a host of other reasons. It is best to have a lag fixer trigger that creates a temp unit at map init, adds a bonus of -1024 (i.e all abilities) and then removes it, so it then stops the first time lag occouring. The fact that it would kill the unit would be something to do with it's order of adding its skills. I dont get what you mean about unit add ability returning boolean. If you're talking about the native returning a boolean, then this probobly returns 'true' if adding the ability was successfull, and if you're calling the function you dont need to worry about it. Code:
function IndexBonus takes string a returns integer if (a == "Dam") then return 0 elseif (a == "Mp") then return 1 elseif (a == "Hp") then return 2 elseif (a == "Arm") then return 3 elseif (a == "Str") then return 4 elseif (a == "Int") then return 5 elseif (a == "Agi") then return 6 else return 7 //If there is an error, no bonus is added endif endfunction Correct me if im wrong, but i think you need to return something at the second last line of the function otherwise it will crash. If you want, you can look at the version i made: http://www.wc3sear.ch/index.php?p=Sp...78d4483c769185 (I bet weeaddar is gonna bitch about the code being inefficient :D Watch out!) |
| 07-21-2005, 12:22 PM | #3 |
The AddUnitAbility returns if the ability was succsesfull added I think. |
| 07-21-2005, 02:44 PM | #4 |
for bonusmod the correct value to add is always -1, as that is infact all abilities not -1024 which is just the last indice, or 1023 which is everything but -1024. As for optimization going, you at least are using all natives, but you should interleave the calls as such if(HowMuch>Bit1)then call UnitAddAbility(...) else call UnitRemoveAbility(...) endif although your array index getting way is wonky, why do you hate integer i so much? It would be easier to read if you just counted down from your highest index-1 to zero. Oh and don't use strings in your internal method as its slower then ints. You could add a fluffer method that calls bonusmod using those strings and converts it to the integers or you can define constants as thats roughly what you are doing anyway. Just make it so its like:: global integer DAMAGE =1 global integer ARMOR=2 etc.. thats pretty easy to read, but just realize when you keep working with the system, or if your building your own item equipment system your universally going to ignore the name of the type as your probably going to have a list of bonus values to add as parameters like (...,34,5,0,0,0,0,2) As for why it kills the unit, I'm going to assume the unit doesn't have enough HP to add the -1023 hp ability. To safely do it, when you set it to a negative value, you'll want to store it being a negative value as a boolean add 1024 to that value and when it gets to the end of your function then add the negative bonus. For example see mine or Vex's bonusmod. |
| 07-21-2005, 02:55 PM | #5 |
hmm ok, but can someone look at the edit? why on earth would the unit die if damage is added? |
| 07-21-2005, 03:07 PM | #6 |
the wrong ability is being added. UnitAddAbilities boolean returns weather it was added, its not very useful because only in very few cases will the ability not be added. The case I think is when you already have the ability then it won't be added. |
| 07-21-2005, 03:26 PM | #7 | |
Quote:
1589 native UnitAddAbility takes unit whichUnit, integer abilityId returns boolean Yep it does return true if the ability was aded and false if it could not be added I have no clue as for why does the unit die. T.T Maybe there is some obscure ability that kills the unit. and somewhere in the loop it adds it to the unit. try giving the bonuses to units without using loops and see if any unit gets killed. |
| 07-21-2005, 06:10 PM | #8 |
i made the pala have 1250 hp(lvl 10) and when i bonused, he lost 1 hp. as the losing hp is made before the adding hp, he got killed :$ anyway, there still is a problem - he loses 1 hp, mana and int. ill edit this post if something new comes up |
| 07-21-2005, 08:47 PM | #9 |
ok thanks for the help everyone, i got it fixed. I was a case of a lot of variables being in the wrong places, and loops having incorrect exitwhen's, but i now works! heres the code if any1 wants it Code:
function IndexBonus takes string a returns integer
if (a == "Dam") then
return 0
elseif (a == "Mp") then
return 1
elseif (a == "Hp") then
return 2
elseif (a == "Arm") then
return 3
elseif (a == "Str") then
return 4
elseif (a == "Int") then
return 5
elseif (a == "Agi") then
return 6
else
return 7
//If there is an error, no bonus is added
endif
endfunction
//You type in what bonuses are to be set, and then it returns the start of the type of
//bonuses you want in the 2d array, which is useful for...
function AddBonustoUnit takes string whattype, unit u, integer howmuch returns nothing
local boolean b = false
local integer i = IndexBonus(whattype)
local integer c = (i*NumofBonuses())
local integer bit1 = MaxBonus()/2
local integer bit2 = 0
local integer a = c
if (howmuch >= MaxBonus()) then
set howmuch = MaxBonus()-1
elseif (howmuch < MinBonus()) then
set howmuch = MinBonus()
endif
//if it's too big/small, it corrected
if (howmuch < 0) then
set howmuch = howmuch - MinBonus()//Adds the stuff so it's positive
set b = true
endif
//remove all of the previous abilities
loop
exitwhen (a > (c+10))
call UnitRemoveAbility(u, udg_AdvancedAbils[a])
set a = a + 1
endloop
set c = (i*NumofBonuses())
loop
exitwhen (howmuch == 0)
if (howmuch >= bit1) then
call UnitAddAbility(u, udg_AdvancedAbils[c+bit2])
set howmuch = howmuch - bit1
endif
set bit1 = bit1 / 2
set bit2 = bit2 + 1
endloop
if (b == true) then
call UnitAddAbility(u, udg_AdvancedAbils[i+(NumofBonuses()-1)])
endif
endfunction
//Just like the bonusmods... it may be strange the way i do it, but it was the only
//way i could think of doing it
function ItemSetBonuses takes integer whichitem, integer Agi, integer Int, integer Str, integer Dam, integer Arm, integer Hp, integer Mp returns nothing
call ItemSetAgi(whichitem, Agi)
call ItemSetStr(whichitem, Str)
call ItemSetInt(whichitem, Int)
call ItemSetHp(whichitem, Hp)
call ItemSetMp(whichitem, Mp)
call ItemSetDam(whichitem, Dam)
call ItemSetArm(whichitem, Arm)
endfunctionPS weaaddar you said strings are too slow and i should use integers, but in the interest of user compatability, should i keep the current system? there is no longer any lag, i think tht was to do with the dogey loops |
