• 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.

Condition: In Range Check

Status
Not open for further replies.
Level 5
Joined
Feb 22, 2013
Messages
161
Good day to ya'll, I just have a question.... How would I code a check to see if ALL units in a unit group are within range of another unit to be able to proceed with the mission? Otherwise, display a text saying that all players must be within range of course?
 
Level 16
Joined
Dec 15, 2011
Messages
1,423
I am not very sure about syntax so check it again first

JASS:
function RangeCheck takes group g, unit u returns boolean
local unit v
local unit array w
local integer i = 0
local boolean b = true
// Enum the group
loop
set v = FirstOfGroup (g)
exitwhen v == null
if not IsUnitInRange (u, v, your_range) then
// display message here
// if b is true, set it to false
if b then
set b = false
endif
endif
set w[i]= v
set i = i + 1
call GroupRemoveUnit (g, v)
endloop

// Readd units to group
loop
exitwhen i < 0
call GroupAddUnit (g, w[i])
set w[i] = null
set i = i - 1
endloop

return b
endfunction
 
Last edited:
Level 33
Joined
Mar 27, 2008
Messages
8,035
Must all the units be near to each other OR just only 1 unit must be near to each other (Example: A LinkedList sequence diagram) ?
Which one is the condition ?

I have created a test map with condition: All units must be nearby to each other
 

Attachments

  • Checking Units.w3x
    13.1 KB · Views: 49
Level 5
Joined
Feb 22, 2013
Messages
161
There are three units, one is a dummy unit for range check, the other two must be within a certain distance of the dummy unit in order to continue with the mission. If the two players are within that distance, then a cinematic occurs.
 
Level 16
Joined
Dec 15, 2011
Messages
1,423
What do you mean? I don't really understand but here is sth that may help

JASS:
function Callback takes nothing return nothing
call RangeCheck (g, GetEnumUnit ())

function blah takes something returns something
// blah blah
call ForGroup (g, function Callback)
endfunction

You could enum a group with a FirstOfGroup loop like I do.
 
Level 5
Joined
Feb 22, 2013
Messages
161
Doomlord said:
I am not very sure about syntax so check it again first

JASS:
function RangeCheck takes group g, unit u returns boolean
local unit v
local unit array w
local integer i = 0
loop
set v = FirstOfGroup (g)
exitwhen v == null
if not IsUnitInRange (u, v, your_range)
// display message here
return false
endif
set w[i]= v
set i = i + 1
call GroupRemoveUnit (g, v)
endloop

loop
exitwhen i < 0
call GroupAddUnit (g, w[i])
set w[i] = null
set i = i - 1
endloop

return true
endfunction

From the way it looks, it looks like it only registers one unit that comes within range, it looks like it doesn't condition if both player units are in range of the dummy unit... Now I'm not saying it doesn't, it just looks like it.
 
Level 16
Joined
Dec 15, 2011
Messages
1,423
It takes a unit group and unit u as arguments. Loop through group g, if a unit is out of range and the local boolean is true, set it to false. Afterward, it loops again to add the units back to group g and return the boolean. Btw it seems there are some bad code. Updated.

Explain your logic as stated above.
 
Level 5
Joined
Feb 22, 2013
Messages
161
It takes a unit group and unit u as arguments. Loop through group g, if a unit is out of range and the local boolean is true, set it to false. Afterward, it loops again to add the units back to group g and return the boolean. Btw it seems there are some bad code. Updated.

Explain your logic as stated above.

Sooo, I tested your function and I get an error reading "Syntax Error, unexpected: end of line?" Any ideas?

JASS:
function RangeCheck takes group g, unit u returns boolean
	local unit v
	local unit array w
	local integer i = 0
	
	loop
		set v = FirstOfGroup (g)
		exitwhen v == null
		if not IsUnitInRange(u, v, 543.05801)
			if ( IsPlayerInForce(GetLocalPlayer(), udg_UserForce) ) then
				call DisplayTextToPlayer(GetLocalPlayer(), 50.00, 50.00, "All heroes must be within the area to proceed.")
			endif
			return false
		endif
		set w[i]= v
		set i = i + 1
		call GroupRemoveUnit (g, v)
	endloop

	loop
		exitwhen i < 0
		call GroupAddUnit (g, w[i])
		set w[i] = null
		set i = i - 1
	endloop

	return true
endfunction

function TriggerAmbush takes nothing returns nothing
	if RangeCheck(udg_UserUnitGroup, gg_unit_du00_0001) == true then
        if TriggerEvaluate(gg_trg_Ambush_Cine) then
            call DisableTrigger(GetTriggeringTrigger())
            call TriggerExecute(gg_trg_Ambush_Cine)
			call RemoveUnit(gg_unit_du_0001)
        endif
    endif
endfunction

//===========================================================================
function InitTrig_Ambush_Trigger takes nothing returns nothing
	local trigger tri = CreateTrigger()
	local region re = CreateRegion()
	
	call TriggerAddAction(tri, function UserUnitGroupRegister)
	call RegionAddRect(re, gg_rct_Ambush_Trigger)
	call TriggerRegisterUnitInRange(tri, gg_unit_du00_0001, 543.05801, null)
	
	set re = null
	set tri = null
endfunction
 
Level 5
Joined
Feb 22, 2013
Messages
161
I updated the function. You should update too. Oh and I did say that there may be syntax errors since I wrote that snippet on the fly. Which line is it?

edit
Saw it. "Then" was missing in the loop's ITE.

Yep, exactly, that fixed it... Thank you very much, and for the other times as well :)
 
Level 5
Joined
Feb 22, 2013
Messages
161
Never mind, I figured it out now, I thought that when it returned false that is skips the remaining actions so that it doesn't loop through to the other unit.... I'm such a dunce sometimes :(
 
Status
Not open for further replies.
Top