# [JASS]Arrays problem

Status
Not open for further replies.

#### Flame_Phoenix

Level 20
Hi guys, here I am again. This time i need help with the reward system for my map.
It basically creates a well ordered army in a square battle position. But I have a trouble. My arrays don't work.

JASS:
``````//creates a row of 4 melee dwarfs
//variables:
//local integer counter = 0
//local unit array m //it ill store the melee dwarfs

loop
exitwhen (counter == 4)
set m[counter] = CreateUnit(c, 'h00B', x, y, 270.00)
call UnitApplyTimedLife(CreateUnit(c, 'h01B', x, y, 0.00),'BTLF',1.00)
call SetUnitInvulnerable(m[counter], true)
call SetUnitMoveSpeed( m[counter], ( GetUnitDefaultMoveSpeed(m[counter])/2))
set x = x + 95
set counter = counter + 1
endloop
set counter = 0
set y = 4545.0
set x = -6160.0
//orders this created row to move forwards
loop
exitwhen (counter == 4)
call IssuePointOrder(m[counter], "move", x, y)
set x = x + 95
set counter = counter + 1
endloop
call TriggerSleepAction(0.75)
//creates a second row of melee dwarfs
set counter = 5
set x = -6160.0
set y = 5335.5
loop
exitwhen (counter == 9)
set m[counter] = CreateUnit(c, 'h00B', x, y, 270.00)
call UnitApplyTimedLife(CreateUnit(c, 'h01B', x, y, 0.00),'BTLF',1.00)
call SetUnitInvulnerable(m[counter], true)
call SetUnitMoveSpeed( m[counter], ( GetUnitDefaultMoveSpeed(m[counter])/2))
set x = x + 95
set counter = counter + 1
endloop
//orders the second row to move forwards
set counter = 5
set y = 4635.0
set x = -6160.0
loop
exitwhen (counter == 9)
call IssuePointOrder(m[counter], "move", x, y)
set x = x + 95
set counter = counter + 1
endloop``````
As you can see, it creates 4 melee dwarfs warriors. Then orders their row to move 95 meters forwards.
After that creates a second row of units. And there is the problem. The unit are created, but they don't seem to get the order. They just don't move forward. Can you guys tell me why ?

#### 2seksi

Level 3
I think your problem lies somewhere else because that looks fine.

Also, are those dwarves becoming invulnerable or not? That will give us more insight on the problem.

#### Flame_Phoenix

Level 20
Ahh, nvm already fixed the bug. It was a mistake with the variable "m" instead of another variable called "r".
Also, My array use was not correct. I was leaving an array spot un-used.
Still, thx fir you help =)

#### 2seksi

Level 3
Leaving an array spot unused would not matter, it seems that it was your other problem that was causing this. Anywho, good luck.

#### Blue_Jeans

Level 6
Also, I would set x and y equal to the position of the first dwarf that's created and never change them. After that, use everything as relative. `GetUnitX(r[counter-1])+95` kind of thing. This way if you decide to change where they are spawned, you change those initial settings and thats' all.

#### Flame_Phoenix

Level 20
Mm
Blue, That would take more time for the computer to read. This code I am talking about is huge, you have only seen a little portion of the code.

Also, leaving array spots not used is a waste of memory, which is very important for my map at this stage. I can't afford having much leaks or others.

Anyway, Do you guys know how I can turn the collision path off for a unit ?

#### 2seksi

Level 3
I can't check right now, but off of memory I think it is

SetUnitPathing( whichUnit, turnOff )

turnOff = boolean where true turns it off, I >think<

#### GhostWolf

Level 29
You can just use something like this so your "moving forward" will not be stuck into one place (the x,y you specified):

JASS:
``````    set angle = GetUnitFacing(m[counter]) * bj_DEGTORAD
set x = GetUnitX(m[counter]) + 100 * Cos(angle)
set y = GetUnitY(m[counter]) + 100 * Sin(angle)
call IssuePointOrder(m[counter], "move", x, y)``````

This will order them to move 100 to where they are faced.

#### Flame_Phoenix

Level 20
Woooow.
Thx wolf, but I actually don't need that. That is far to complicated to use in the code.

Anyway, that collision thing was just what I needed, thx. +rep.

Now I have a problem trying to optimize this part of the code:

JASS:
``````function Second_Seq_Effects takes nothing returns nothing
local unit eff1
local unit eff2
local unit eff3
local unit eff4
local unit eff5
local unit eff6
local unit eff7
local unit eff8
local unit eff9
local unit eff10
local unit eff11
local unit eff12
set eff1 = CreateUnit(Player(5),'h01N', -6260.0, 5335.5, 0.00)
call SetUnitAnimation(eff1, "stand second")
set eff2 = CreateUnit(Player(5),'h01N', -5775.5, 5335.5, 0.00)
call SetUnitAnimation(eff2, "stand second")
set eff3 = CreateUnit(Player(5),'h01N', -6260.0, 5135.5, 0.00)
call SetUnitAnimation(eff3, "stand second")
set eff4 = CreateUnit(Player(5),'h01N', -5775.5, 5135.5, 0.00)
call SetUnitAnimation(eff4, "stand second")
set eff5 = CreateUnit(Player(5),'h01N', -6260.0, 4935.5, 0.00)
call SetUnitAnimation(eff5, "stand second")
set eff6 = CreateUnit(Player(5),'h01N', -5775.5, 4935.5, 0.00)
call SetUnitAnimation(eff6, "stand second")
set eff7 = CreateUnit(Player(5),'h01N', -6260.0, 4735.5, 0.00)
call SetUnitAnimation(eff7, "stand second")
set eff8 = CreateUnit(Player(5),'h01N', -5775.5, 4735.5, 0.00)
call SetUnitAnimation(eff8, "stand second")
set eff9 = CreateUnit(Player(5),'h01N', -6260.0, 4535.5, 0.00)
call SetUnitAnimation(eff9, "stand second")
set eff10 = CreateUnit(Player(5),'h01N', -5775.5, 4535.5, 0.00)
call SetUnitAnimation(eff10, "stand second")
set eff11 = CreateUnit(Player(5),'h01N', -6260.0, 4335.5, 0.00)
call SetUnitAnimation(eff11, "stand second")
set eff12 = CreateUnit(Player(5),'h01N', -5775.5, 4335.5, 0.00)
call SetUnitAnimation(eff12, "stand second")
call TriggerSleepAction(3.0)
call KillUnit(eff1)
call KillUnit(eff2)
call KillUnit(eff3)
call KillUnit(eff4)
call KillUnit(eff5)
call KillUnit(eff6)
call KillUnit(eff7)
call KillUnit(eff8)
call KillUnit(eff9)
call KillUnit(eff10)
call KillUnit(eff11)
call KillUnit(eff12)
set eff1 = null
set eff2 = null
set eff3 = null
set eff4 = null
set eff5 = null
set eff6 = null
set eff7 = null
set eff8 = null
set eff9 = null
set eff10 = null
set eff11 = null
set eff12 = null
endfunction``````

I am trying to use a loop, but I just can't find the correct formulae.
Each time the loop rounds, i set "y = y - 200".
My way didn't work.
I also tried to use arrays, but the result was not good enough.

Can you guys help me ?

#### Blue_Jeans

Level 6
Edit: The server when down as I was editing this post, now fixed
First off, when you declare a local variable, you can assign it a value. So your code can already be more efficient like this:
JASS:
``````function Second_Seq_Effects takes nothing returns nothing
local unit eff1 = CreateUnit(Player(5),'h01N', -6260.0, 5335.5, 0.00)
local unit eff2 = CreateUnit(Player(5),'h01N', -5775.5, 5335.5, 0.00)
local unit eff3 = CreateUnit(Player(5),'h01N', -6260.0, 5135.5, 0.00)
local unit eff4 = CreateUnit(Player(5),'h01N', -5775.5, 5135.5, 0.00)
local unit eff5 = CreateUnit(Player(5),'h01N', -6260.0, 4935.5, 0.00)
local unit eff6 = CreateUnit(Player(5),'h01N', -5775.5, 4935.5, 0.00)
local unit eff7 = CreateUnit(Player(5),'h01N', -6260.0, 4735.5, 0.00)
local unit eff8 = CreateUnit(Player(5),'h01N', -5775.5, 4735.5, 0.00)
local unit eff9 = CreateUnit(Player(5),'h01N', -6260.0, 4535.5, 0.00)
local unit eff10 = CreateUnit(Player(5),'h01N', -5775.5, 4535.5, 0.00)
local unit eff11 = CreateUnit(Player(5),'h01N', -6260.0, 4335.5, 0.00)
local unit eff12 = CreateUnit(Player(5),'h01N', -5775.5, 4335.5, 0.00)
call SetUnitAnimation(eff1, "stand second")
call SetUnitAnimation(eff2, "stand second")
call SetUnitAnimation(eff3, "stand second")
call SetUnitAnimation(eff4, "stand second")
call SetUnitAnimation(eff5, "stand second")
call SetUnitAnimation(eff6, "stand second")
call SetUnitAnimation(eff7, "stand second")
call SetUnitAnimation(eff8, "stand second")
call SetUnitAnimation(eff9, "stand second")
call SetUnitAnimation(eff10, "stand second")
call SetUnitAnimation(eff11, "stand second")
call SetUnitAnimation(eff12, "stand second")
call TriggerSleepAction(3.0)
call KillUnit(eff1)
call KillUnit(eff2)
call KillUnit(eff3)
call KillUnit(eff4)
call KillUnit(eff5)
call KillUnit(eff6)
call KillUnit(eff7)
call KillUnit(eff8)
call KillUnit(eff9)
call KillUnit(eff10)
call KillUnit(eff11)
call KillUnit(eff12)
set eff1 = null
set eff2 = null
set eff3 = null
set eff4 = null
set eff5 = null
set eff6 = null
set eff7 = null
set eff8 = null
set eff9 = null
set eff10 = null
set eff11 = null
set eff12 = null
endfunction``````

Second, this desperately needs to use arrays. Whenever you have large amount of data, you need someway to effectively organize it:
JASS:
``````function Second_Seq_Effects takes nothing returns nothing
local unit array eff
set eff[0] = CreateUnit(Player(5),'h01N', -6260.0, 5335.5, 0.00)
set eff[1] = CreateUnit(Player(5),'h01N', -5775.5, 5335.5, 0.00)
set eff[2] = CreateUnit(Player(5),'h01N', -6260.0, 5135.5, 0.00)
set eff[3] = CreateUnit(Player(5),'h01N', -5775.5, 5135.5, 0.00)
set eff[4] = CreateUnit(Player(5),'h01N', -6260.0, 4935.5, 0.00)
set eff[5] = CreateUnit(Player(5),'h01N', -5775.5, 4935.5, 0.00)
set eff[6] = CreateUnit(Player(5),'h01N', -6260.0, 4735.5, 0.00)
set eff[7] = CreateUnit(Player(5),'h01N', -5775.5, 4735.5, 0.00)
set eff[8] = CreateUnit(Player(5),'h01N', -6260.0, 4535.5, 0.00)
set eff[9] = CreateUnit(Player(5),'h01N', -5775.5, 4535.5, 0.00)
set eff[10] = CreateUnit(Player(5),'h01N', -6260.0, 4335.5, 0.00)
set eff[11] = CreateUnit(Player(5),'h01N', -5775.5, 4335.5, 0.00)
call SetUnitAnimation(eff[0], "stand second")
call SetUnitAnimation(eff[1], "stand second")
call SetUnitAnimation(eff[2], "stand second")
call SetUnitAnimation(eff[3], "stand second")
call SetUnitAnimation(eff[4], "stand second")
call SetUnitAnimation(eff[5], "stand second")
call SetUnitAnimation(eff[6], "stand second")
call SetUnitAnimation(eff[7], "stand second")
call SetUnitAnimation(eff[8], "stand second")
call SetUnitAnimation(eff[9], "stand second")
call SetUnitAnimation(eff[10], "stand second")
call SetUnitAnimation(eff[11], "stand second")
call TriggerSleepAction(3.0)
call KillUnit(eff[0])
call KillUnit(eff[1])
call KillUnit(eff[2])
call KillUnit(eff[3])
call KillUnit(eff[4])
call KillUnit(eff[5])
call KillUnit(eff[6])
call KillUnit(eff[7])
call KillUnit(eff[8])
call KillUnit(eff[9])
call KillUnit(eff[10])
call KillUnit(eff[11])
set eff[0] = null
set eff[1] = null
set eff[2] = null
set eff[3] = null
set eff[4] = null
set eff[5] = null
set eff[6] = null
set eff[7] = null
set eff[8] = null
set eff[9]  = null
set eff[10] = null
set eff[11] = null
endfunction``````

What's the point of organized data if you can't automate code? Hence:
JASS:
``````function Second_Seq_Effects takes nothing returns nothing
local integer i // i is almost always the counter
local unit array eff
set eff[0] = CreateUnit(Player(5),'h01N', -6260.0, 5335.5, 0.00)
set eff[1] = CreateUnit(Player(5),'h01N', -5775.5, 5335.5, 0.00)
set eff[2] = CreateUnit(Player(5),'h01N', -6260.0, 5135.5, 0.00)
set eff[3] = CreateUnit(Player(5),'h01N', -5775.5, 5135.5, 0.00)
set eff[4] = CreateUnit(Player(5),'h01N', -6260.0, 4935.5, 0.00)
set eff[5] = CreateUnit(Player(5),'h01N', -5775.5, 4935.5, 0.00)
set eff[6] = CreateUnit(Player(5),'h01N', -6260.0, 4735.5, 0.00)
set eff[7] = CreateUnit(Player(5),'h01N', -5775.5, 4735.5, 0.00)
set eff[8] = CreateUnit(Player(5),'h01N', -6260.0, 4535.5, 0.00)
set eff[9] = CreateUnit(Player(5),'h01N', -5775.5, 4535.5, 0.00)
set eff[10] = CreateUnit(Player(5),'h01N', -6260.0, 4335.5, 0.00)
set eff[11] = CreateUnit(Player(5),'h01N', -5775.5, 4335.5, 0.00)
loop
exitwhen i>11
call SetUnitAnimation(eff[i], "stand second")
set i = i + 1
endloop
call TriggerSleepAction(3.0)
set i=0
loop
exitwhen i>11
call KillUnit(eff[i])
set eff[i] = null
set i = i + 1
endloop
endfunction``````
Something to consider would be using groups:
JASS:
``````function Second_Seq_Actions takes nothing returns nothing
call SetUnitAnimation(GetEnumUnit(), "stand second")
call TriggerSleepAction(3.0)
call KillUnit(GetEnumUnit())
endfunction
function Second_Seq_Effects takes nothing returns nothing
local group g = CreateGroup()
call ForGroup(g, function Second_Seq_Actions)
call DestroyGroup(g)
set g = null
endfunction``````

Last edited:

#### Flame_Phoenix

Level 20
Mmmm i had somethinf in mind like:
JASS:
``````function Second_Seq_Effects takes nothing returns nothing
local integer counter = 0
local real array x
local real y
local unit array ef
local player p = Player(5)

set x[0] = -6260.0
set x[1] = -5775.5
set y = 5335.5

loop
exitwhen (counter == 20)
set ef[counter] = CreateUnit(p,'h01N', x[0], y, 0.00)
call SetUnitAnimation(ef[counter], "stand second")
set ef[counter + 1] = CreateUnit(p,'h01N', x[1], y, 0.00)
call SetUnitAnimation(ef[counter + 1], "stand second")
set y = 2 - 200
set counter = counter + 2
endloop

call TriggerSleepAction(3.0)
set counter = 0

loop
exitwhen (counter == 10)
call KillUnit(ef[counter])
set ef[counter] = null
endloop
set p = null
endfunction``````

But when i tested it didn't work ... do you know why ?
Oh and thx for all the codes =P however groups will be an answer, they will make the game lag like hell (I think), so loops are kinda better here.

#### Blue_Jeans

Level 6
They won't lag the game, but because the forums keeping temporarily going down, I didnt' get a chance to remove the groups code. I had forgotten that waits/sleeps in a ForGroup will crash the thread. So they don't work anyways.

Helping you with each part like this is kind of... I don't know the word. My point is that I can give you better code if you post the whole trigger and then I can keep that in mind while coding each part. I mean, we might get all done and you will post the trigger and I might go... "Oh no... you went about this all wrong." I don't want that to happen. Could you please post the whole trigger?

Edit: By the way, I think for this to work well you will HAVE to use a handle system... and if we use a handle system, might as well be a fast one since you are so concerned about speed. Which means we can't use Handle Vars.

Last edited:

#### Flame_Phoenix

Level 20
WOooOW. Thx but I won't post the entire code. It would be huge, and besides, it's still not complete yet.
But I know what I am asking. This is a function that is called in separate. So, optimizing it won't affect the other codes, it will only optimize this section of the code (which is what I want).

Also the system is likely to suffer future changes in the future, so i must understand it 100 %.
Anyway, The other codes are all optimized by me (I use loops and arrays with no probs with them).
My only problem is this section of the code.

Also, the system worked perfectly in past without any Handle system, so I believe they are not needed now. I am just making a major optimization of the code.
I can still post it here, but it won't help. Also I am still testing a section of it (this one).

Anyway, can you tell me what is wrong with my optimization loop ?? whay doesn't it work ?

#### Blue_Jeans

Level 6
I think that `set y = 2 - 200` should probably be `set y = y - 200`. Also, on your second loop you never increment the counter, mate. That means you need a `set counter = counter + 2` after you do `set ef[counter] = null`

I'm don't know if anyone has told you this, but sleeps/waits are inaccurate. Plus, you can't always use them. Using them in certain places results in a crash of the game. That means that you should use timers. If you use timers, you will need to have a handle system to send data from place to place.

#### Flame_Phoenix

Level 20
MMM crap.
Well the real code has those problems you pointed fixed. I just posted here incorrectly. And it still doesn't work. CAn you tell me why ?

Mmm, I know that sleeps can't be used with groups, but in this case, I don't see the problem ...
Anyway, perhaps using timers wouldn't be as bad as you say ...
I will post here a version of the trigger soon, but be warned, it has more than 400 code lines, all done by me.

Last edited:

#### Blue_Jeans

Level 6
You can't use the sleep. I just tested this one my map:
JASS:
``````function Second_Seq_Effects takes nothing returns nothing
local integer i = 0
local real array x
local real y=GetUnitY(GetTriggerUnit())
local player p = Player(0)
local unit array u
set x[0]=GetUnitX(GetTriggerUnit())
set x[1]=GetUnitX(GetTriggerUnit())+484.5
loop
exitwhen (i > 19)
set u[i] = CreateUnit(p,'hpea', x[0], y, 0.00)
set u[i + 1] = CreateUnit(p,'hpea', x[1], y, 0.00)
call SetUnitAnimation(u[i], "stand second")
call SetUnitAnimation(u[i + 1], "stand second")
set y = y - 200
set i = i + 2
endloop
call TriggerSleepAction(3.0)
loop
exitwhen (i < 0)
call KillUnit(u[i])
set u[i] = null
set i = i-1
endloop
set p = null
endfunction``````

And it works IF the sleep isn't there. Otherwise, the peasants don't get deleted.

If you would, just attach the map and say which trigger it is.
Edit: Do you wanna MSN over this? broshizzledizzle [@t] hotmail [d0t] com

#### Flame_Phoenix

Level 20
Mmm MSN ... well I can't attach the map. I would risk losing 2-3 years of hard work all alone.
I can give you the codes however.

About the MSN, I will soon create a new acc, which will remain public for those I trust. You can eventually be added, but under the condition of not telling any personal information about yourself (or me) that may compromise our privacy.

Sorry about this, but I always have this rules with everyone from the internet (it's not for me, it's actually for them, but anyway).

Btw, thx for telling me that =) I am now using you code =)
News soon =)

#### Blue_Jeans

Level 6
Lol, I only use MSN for talking to people I don't know. I'm not a big fan of MSN, but a lot of people have it.

I guess attaching it would leave it open to public, but how about mailing it to my MSN?

#### Flame_Phoenix

Level 20
Mmmm. My map is kinda huge. It has +/- 3.8 Mb, it will take an eternity (yes, I am trying very hard not to reach 4 MB... why? because map limit is 5 lol).

Anyway, I shall have my msn e-mail ready by tomorrow. We shall speak then tomorrow =)

#### Blue_Jeans

Level 6
Okay, I'll try to be online in about an hour from this post and I'll try to be online for several hours.

#### Flame_Phoenix

Level 20
hey, are you here ?? I mean are you online now ?? Because I have already added you, and I am just waiting for you to be online.
Also, I won't be here more than an hour (i have to leave before 21.30 GMT)

Status
Not open for further replies.

Replies
12
Views
1K
Replies
34
Views
2K
Replies
2
Views
930
Replies
21
Views
2K
Replies
16
Views
2K