• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[vJASS] Issued order is failing.

Status
Not open for further replies.

Chaosy

Tutorial Reviewer
Level 40
Joined
Jun 9, 2011
Messages
13,219
Yet another vjass problem.. yay..

the system is supposed to make a unit patrol between x number of way points.

JASS:
globals
	group patrolUnits = CreateGroup()
	hashtable patrolHash = InitHashtable()
endglobals

function patrol takes unit u, integer locationCount returns nothing
	local integer i = GetHandleId(u)
	local integer loopCount = 0
	local real array x
	local real array y
	call GroupAddUnit(patrolUnits, u)
	loop
		exitwhen loopCount > locationCount
		set loopCount = loopCount + 1
		set x[loopCount] = GetLocationX(udg_patrol_points[loopCount])
		set y[loopCount] = GetLocationY(udg_patrol_points[loopCount])
	endloop
	call SaveInteger(patrolHash, i, 0, locationCount)
	
	call SaveReal(patrolHash, i, 1, x[1])
	call SaveReal(patrolHash, i, 3, x[2])
	call SaveReal(patrolHash, i, 5, x[3])
	call SaveReal(patrolHash, i, 7, x[4])
	
	call SaveReal(patrolHash, i, 2, y[1])
	call SaveReal(patrolHash, i, 4, y[2])
	call SaveReal(patrolHash, i, 6, y[3])
	call SaveReal(patrolHash, i, 8, y[4])
	/*
	call CreateNUnitsAtLoc( 1, 'hfoo', Player(0), Location(LoadReal(patrolHash, i, 1), LoadReal(patrolHash, i, 2)), bj_UNIT_FACING )
	call CreateNUnitsAtLoc( 1, 'hfoo', Player(0), Location(LoadReal(patrolHash, i, 3), LoadReal(patrolHash, i, 4)), bj_UNIT_FACING )
	call CreateNUnitsAtLoc( 1, 'hfoo', Player(0), Location(LoadReal(patrolHash, i, 5), LoadReal(patrolHash, i, 6)), bj_UNIT_FACING )
	call CreateNUnitsAtLoc( 1, 'hfoo', Player(0), Location(LoadReal(patrolHash, i, 7), LoadReal(patrolHash, i, 8)), bj_UNIT_FACING )
	*/
	call IssuePointOrderLocBJ(u, "attack", Location(LoadReal(patrolHash, i, 1), LoadReal(patrolHash, i, 2)))
endfunction

function Group takes nothing returns nothing
	local unit u = GetEnumUnit()
	local integer h = GetHandleId(u)
	local location array locs
	local location uLoc = GetUnitLoc(u)
	local integer i = -1
	local integer i2 = 0
	loop
		exitwhen i2 >= LoadInteger(patrolHash, h, 0) * 2
		call BJDebugMsg(R2S(DistanceBetweenPoints(uLoc, locs[i])))
		set i = i + 2
		set i2 = i2 + 2
		call BJDebugMsg("i " + I2S(i))
		call BJDebugMsg("i2 " + I2S(i2))
		set locs[i2] = Location(LoadReal(patrolHash, h, i), LoadReal(patrolHash, h, i2))
		if DistanceBetweenPoints(uLoc, locs[i2]) < 50 then
			if i2 >= 4 then
				call IssuePointOrderLocBJ(u, "move", locs[1])
				call BJDebugMsg("moving to point 1")
			else
				call IssuePointOrderLocBJ(u, "move", locs[i2 + 1])
				call BJDebugMsg("moving to point" +  I2S(i2))
			endif
			
		endif
	endloop	
endfunction

function patrolUpdate takes nothing returns nothing
	call ForGroup(patrolUnits, function Group)
endfunction

//===========================================================================
function InitTrig_Patrol_System takes nothing returns nothing
    set gg_trg_Patrol_System = CreateTrigger(  )
    call TriggerRegisterTimerEventPeriodic( gg_trg_Patrol_System, 0.03 )
    call TriggerAddAction( gg_trg_Patrol_System, function patrolUpdate )
endfunction

Just to confirm a few things that I've found out so far:

1. the locations are successfully stored into the hashtable. The following code creates the units exactly where they are supposed to be.
JASS:
	/*
	call CreateNUnitsAtLoc( 1, 'hfoo', Player(0), Location(LoadReal(patrolHash, i, 1), LoadReal(patrolHash, i, 2)), bj_UNIT_FACING )
	call CreateNUnitsAtLoc( 1, 'hfoo', Player(0), Location(LoadReal(patrolHash, i, 3), LoadReal(patrolHash, i, 4)), bj_UNIT_FACING )
	call CreateNUnitsAtLoc( 1, 'hfoo', Player(0), Location(LoadReal(patrolHash, i, 5), LoadReal(patrolHash, i, 6)), bj_UNIT_FACING )
	call CreateNUnitsAtLoc( 1, 'hfoo', Player(0), Location(LoadReal(patrolHash, i, 7), LoadReal(patrolHash, i, 8)), bj_UNIT_FACING )
	*/

2. the loops numbers work fine too. The message displays the following:
i 1
i2 2

i 3
i2 4

i 5
i2 6

i 7
i2 8

3. if I use the following line the unit wont move at all, I can't issue anything. I suspect it's because the loop orders him to move to his current location.
JASS:
call IssuePointOrderLocBJ(u, "move", locs[i2])

however, if I use the following..

JASS:
call IssuePointOrderLocBJ(u, "move", locs[i2 + 1])

the unit wont move at all, I can order him around but he wont move to the next point.
 

Attachments

  • Advanced Patrol.w3x
    18.1 KB · Views: 43
Last edited:
Level 26
Joined
Aug 18, 2009
Messages
4,097
I do not get it? Why do you iterate over the whole list everytime? You only need to check if the unit has reached its current destination. Else once a unit is on a waypoint, the unit will be spammed with orders because it is still within range of the old one. Also it would react if the unit passed a checkpoint although it's not its current destination. Furthermore, your iteration limits are not quite right. For example in patrol, it goes from 1 to 5, the increment line should be at the bottom.
 

Chaosy

Tutorial Reviewer
Level 40
Joined
Jun 9, 2011
Messages
13,219
1. technically you are correct I only need to check one of the locs, and that's something I'll add later on. However that's not the issue since a unit only get ordered to move once it's in range of a point. So unless you are stupid enough to put many points within 50 units of each other it wont cause a bug. Proved by BJDebugMsg(), it prints "Moving to point 2" over and over and nothing else regarding the other points.

And even if you're right once more, it will re-order a unit to move to a location while it is leaving the old loc, yet again, it wont cause the problem I am facing.

2. Righty, so I move down the exitwhen to the bottom?

edit:
@edo
WIP, I don't bother with such details at this point.
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
locs[i2] is the location that was tested for range, in other words the current old destination. locs[i2+1] is not set in that iteration yet and you increment it by 2, not 1.

Anyway, it's quite a mess. Proper structuring of the code from the start yields more readable code and less debugging.
 
Status
Not open for further replies.
Top