Leash system, not detecting range?

NeuroToxin

New Member
Reaction score
46
I made a leash system, and so far, it works except, it doesnt detect when the unit in question, leaves the range. heres the part of the trigger for that.

JASS:

private function IfIsElse takes nothing returns nothing
    local real X = GetUnitX(GetEnumUnit())
    local real Y = GetUnitY(GetEnumUnit())
    local real dx = GetUnitX(GetEnumUnit()) - GetUnitX(GetTriggerUnit())
    local real dy = GetUnitY(GetEnumUnit()) - GetUnitY(GetTriggerUnit())
    set TempReal = SquareRoot(dx * dx + dy * dy)
        if TempReal >= range then
            if DoesDamage == true then
                call SetUnitState(GetEnumUnit(), UNIT_STATE_LIFE, GetWidgetLife(GetEnumUnit()) - Damage )
                call SetUnitX(GetEnumUnit(), X - 20)
                call SetUnitY(GetEnumUnit(), Y - 20)
                call IssueImmediateOrder(GetEnumUnit(), "Stop")
            else
        call SetUnitX(GetEnumUnit(), X - 20)
        call SetUnitY(GetEnumUnit(), Y - 20)
        call IssueImmediateOrder(GetEnumUnit(), "Stop")
    endif
endif
endfunction
 

Bribe

vJass errors are legion
Reaction score
67
JASS:
private function IfIsElse takes nothing returns nothing
    local unit u = GetEnumUnit()
    local real x = GetUnitX(u)
    local real y = GetUnitY(u)
    local real dx = x - GetUnitX(GetTriggerUnit())
    local real dy = y - GetUnitY(GetTriggerUnit())
    if SquareRoot(dx * dx + dy * dy) >= range then
        if DoesDamage then
            call SetWidgetLife(u, GetWidgetLife(u) - Damage)
        endif
        call SetUnitPosition(u, x - 20, y - 20)
    endif
    set u = null
endfunction


What is the unit in question? What is the triggering unit? Where are DoesDamage and Damage set? What group is enumerating these things and how are they even added? Things to know.
 

NeuroToxin

New Member
Reaction score
46
Heres the full trigger and by the way, once you read it, it stops at "Acquired Unit" And by the way, this is a system to put in when a spell is cast, to leash a certain unit.
JASS:
library LeashSystem
//===========================================================================
//==================================SETUP====================================
//===========================================================================
globals

real range = 1000//This is the range of the leash.
unit LeashedUnit//LeashedUnit
group LeashedUnits//The leashed units.
boolean DoesDamage = false//Deals Damage to the unit for everytime it goes out, initially set to false, can be set to true.
real Damage = 30//If DoesDamage = true, then it will do This damage everytime it goes out.
real TempReal//Don't Touch

endglobals
//===========================================================================
//=================================ENDSETUP==================================
//===========================================================================

private function IfIsElse takes nothing returns nothing
    local real X = GetUnitX(GetEnumUnit())
    local real Y = GetUnitY(GetEnumUnit())
    local real dx = GetUnitX(GetEnumUnit()) - GetUnitX(GetTriggerUnit())
    local real dy = GetUnitY(GetEnumUnit()) - GetUnitY(GetTriggerUnit())
    set TempReal = SquareRoot(dx * dx + dy * dy)
    call BJDebugMsg("IfIsElse function")
        if TempReal >= range then
            if DoesDamage == true then
                call BJDebugMsg("Damaging Unit/Moving")
                call SetUnitState(GetEnumUnit(), UNIT_STATE_LIFE, GetWidgetLife(GetEnumUnit()) - Damage )
                call SetUnitX(GetEnumUnit(), X - 20)
                call SetUnitY(GetEnumUnit(), Y - 20)
                call IssueImmediateOrder(GetEnumUnit(), "Stop")
            else
        call BJDebugMsg("Moving Unit")
        call SetUnitX(GetEnumUnit(), X - 20)
        call SetUnitY(GetEnumUnit(), Y - 20)
        call IssueImmediateOrder(GetEnumUnit(), "Stop")
    endif
endif
endfunction

private function ForFun takes nothing returns nothing
       call ForGroup(LeashedUnits, function IfIsElse)
       call BJDebugMsg("Starting Group")
endfunction

function Leashocity takes nothing returns nothing
        set LeashedUnit = GetSpellTargetUnit()
            call BJDebugMsg("Acquired Unit")
            call GroupAddUnit(LeashedUnits, LeashedUnit)
            call BJDebugMsg("Added Unit")
            call ForFun()
        set LeashedUnit = null
endfunction
            
        
//===========================================================================
//=================================INIT======================================
//===========================================================================
function InitTrig_Leash_System takes nothing returns nothing
    set gg_trg_Leash_System = CreateTrigger(  )
    call TriggerRegisterTimerEvent(gg_trg_Leash_System, .03, true)
endfunction

endlibrary
 

hgkjfhfdsj

Active Member
Reaction score
55
- no timer expire action is attached
eg. call TimerStart(CreateTimer(), 0.03, true, function ForFun)
or TriggerAddAction(...)
- GetTriggerUnit() returns nulls > function IfIsElse compares LeashedUnit and null's position.
- also unit will always travel south westly and not towards the caster
 

NeuroToxin

New Member
Reaction score
46
Heres what I have, at least what I changed it to, and it's not doing anything after the GroupAddUnit(LeashedUnits, LeashedUnit) And im not sure what you mean when you say Function IfIsElse compares leashed unit and null's position...
JASS:
library LeashSystem
//===========================================================================
//==================================SETUP====================================
//===========================================================================
globals

timer t = CreateTimer()
real range = 1000//This is the range of the leash.
unit LeashedUnit//LeashedUnit
group LeashedUnits//The leashed units.
boolean DoesDamage = false//Deals Damage to the unit for everytime it goes out, initially set to false, can be set to true.
real Damage = 30//If DoesDamage = true, then it will do This damage everytime it goes out.
real TempReal//Don't Touch

endglobals
//===========================================================================
//=================================ENDSETUP==================================
//===========================================================================

private function IfIsElse takes nothing returns nothing
    local real X = GetUnitX(GetEnumUnit())
    local real Y = GetUnitY(GetEnumUnit())
    local real dx = GetUnitX(GetEnumUnit()) - GetUnitX(GetTriggerUnit())
    local real dy = GetUnitY(GetEnumUnit()) - GetUnitY(GetTriggerUnit())
    set TempReal = SquareRoot(dx * dx + dy * dy)
    call BJDebugMsg("IfIsElse function")
        if TempReal >= range then
            if DoesDamage == true then
                call BJDebugMsg("Damaging Unit/Moving")
                call SetUnitState(GetEnumUnit(), UNIT_STATE_LIFE, GetWidgetLife(GetEnumUnit()) - Damage )
                call SetUnitX(GetEnumUnit(), X - 20)
                call SetUnitY(GetEnumUnit(), Y - 20)
                call IssueImmediateOrder(GetEnumUnit(), "Stop")
            else
        call BJDebugMsg("Moving Unit")
        call SetUnitX(GetEnumUnit(), X - 20)
        call SetUnitY(GetEnumUnit(), Y - 20)
        call IssueImmediateOrder(GetEnumUnit(), "Stop")
    endif
endif
endfunction

private function ForFun takes nothing returns nothing
       call ForGroup(LeashedUnits, function IfIsElse)
       call BJDebugMsg("Starting Group")
endfunction

function Leashocity takes nothing returns nothing
        set LeashedUnit = GetSpellTargetUnit()
            call BJDebugMsg("Acquired Unit")
            call GroupAddUnit(LeashedUnits, LeashedUnit)
            call BJDebugMsg("Added Unit")
            call ForFun()
            call TimerStart(t, 0.03, true, function ForFun)
        set LeashedUnit = null
endfunction
            
        
//===========================================================================
//=================================INIT======================================
//===========================================================================
function InitTrig_Leash_System takes nothing returns nothing
endfunction

endlibrary
 

tooltiperror

Super Moderator
Reaction score
231
1) Give your functions proper names.
2) Where are you starting the timer for this one? I don't see why anything should run at all.
 

NeuroToxin

New Member
Reaction score
46
1) Give your functions proper names.
I will, after I get it working.
2) Where are you starting the timer for this one? I don't see why anything should run at all.
The timer is so the unit can be detected properly.

Sorry for double post, IDK How to remove the post I had.
 

tooltiperror

Super Moderator
Reaction score
231
Just edit your first post.

JASS:
function InitTrig_Leash_System takes nothing returns nothing
endfunction


It's empty, why do you think anything will start?
 

NeuroToxin

New Member
Reaction score
46
library LeashSystem
//===========================================================================
//==================================SETUP====================================
//===========================================================================
globals

timer t = CreateTimer()
real range = 1000//This is the range of the leash.
unit LeashedUnit//LeashedUnit
group LeashedUnits//The leashed units.
boolean DoesDamage = false//Deals Damage to the unit for everytime it goes out, initially set to false, can be set to true.
real Damage = 30//If DoesDamage = true, then it will do This damage everytime it goes out.
real TempReal//Don't Touch

endglobals
//===========================================================================
//=================================ENDSETUP==================================
//===========================================================================

private function IfIsElse takes nothing returns nothing
local real X = GetUnitX(GetEnumUnit())
local real Y = GetUnitY(GetEnumUnit())
local real dx = GetUnitX(GetEnumUnit()) - GetUnitX(GetTriggerUnit())
local real dy = GetUnitY(GetEnumUnit()) - GetUnitY(GetTriggerUnit())
set TempReal = SquareRoot(dx * dx + dy * dy)
call BJDebugMsg("IfIsElse function")
if TempReal >= range then
if DoesDamage == true then
call BJDebugMsg("Damaging Unit/Moving")
call SetUnitState(GetEnumUnit(), UNIT_STATE_LIFE, GetWidgetLife(GetEnumUnit()) - Damage )
call SetUnitX(GetEnumUnit(), X - 20)
call SetUnitY(GetEnumUnit(), Y - 20)
call IssueImmediateOrder(GetEnumUnit(), "Stop")
else
call BJDebugMsg("Moving Unit")
call SetUnitX(GetEnumUnit(), X - 20)
call SetUnitY(GetEnumUnit(), Y - 20)
call IssueImmediateOrder(GetEnumUnit(), "Stop")
endif
endif
endfunction

private function ForFun takes nothing returns nothing
call ForGroup(LeashedUnits, function IfIsElse)
call BJDebugMsg("Starting Group")
endfunction

function Leashocity takes nothing returns nothing
set LeashedUnit = GetSpellTargetUnit()
call BJDebugMsg("Acquired Unit")
call GroupAddUnit(LeashedUnits, LeashedUnit)
call BJDebugMsg("Added Unit")
call ForFun()
call TimerStart(t, 0.03, true, function ForFun)
set LeashedUnit = null
endfunction


//===========================================================================
//=================================INIT======================================
//===========================================================================
function InitTrig_Leash_System takes nothing returns nothing
endfunction

endlibrary
 

tooltiperror

Super Moderator
Reaction score
231
And why will that function run? What is running Leashocity? I don't see it being called ...

Edit: Why the fuck are you writing half the code the way you are? This isn't the 2005 JASS stone age, we're not in midst of discovering return bug.

EditEdit: Shouldn't you also be using a different interface? Right now this is a complete mess.

EditEditEdit: I cleaned up a bit for you (untested) and it's still leaking with [LJASS]ForGroup[/LJASS]. You also make some useless code I don't understand.

JASS:
library Leash initializer onInit

	// Configuration
	globals
		// The Range of the Leash
		private constant real LEASH_RANGE=1000
		// If True, deals damage to a unit that tries to break its leash
		private constant boolean ENABLE_DAMAGE=false
		// Damage to deal if ENABLE_DAMAGE is true
		private constant real DAMAGE=30
	endglobals

	// System Core
	globals
		private timer t=CreateTimer()
		private unit LeashedUnit
		private group LeashedUnits
		private real TempReal
	endglobals
	
	private function IfIsElse takes nothing returns nothing
		local real X = GetUnitX(GetEnumUnit()) // Don't use GetEnumUnit() so much, too many function calls,
		local real Y = GetUnitY(GetEnumUnit()) // just use a variable (quicker).
		local real dx = GetUnitX(GetEnumUnit()) - GetUnitX(GetTriggerUnit())
		local real dy = GetUnitY(GetEnumUnit()) - GetUnitY(GetTriggerUnit())
		set TempReal = SquareRoot(dx * dx + dy * dy) // Are you trying to get distance here? This could all be one or two lines.
		call BJDebugMsg("IfIsElse function")
		if (TempReal>=range) then
			static if ENABLE_DAMAGE then
				call SetUnitState(GetEnumUnit(),UNIT_STATE_LIFE,GetWidgetLife(GetEnumUnit())-DAMAGE)
			endif
			call BJDebugMsg("Moving Unit")
			call SetUnitX(GetEnumUnit(), X - 20)
			call SetUnitY(GetEnumUnit(), Y - 20)
			call IssueImmediateOrder(GetEnumUnit(),"Stop")
		endif
	endfunction
	
	private function ForFun takes nothing returns nothing
		call ForGroup(LeashedUnits, function IfIsElse)
		call BJDebugMsg("Starting Group")
	endfunction

	function Leashocity takes nothing returns nothing
		set LeashedUnit = GetSpellTargetUnit()
		call BJDebugMsg("Acquired Unit")
		call GroupAddUnit(LeashedUnits, LeashedUnit)	
		call BJDebugMsg("Added Unit")	
		call ForFun()
		call TimerStart(t, 0.03, true, function ForFun)
		set LeashedUnit = null
	endfunction

	function onInit takes nothing returns nothing
		// Useless function?
	endfunction

endlibrary


EditEditEdit: I'm hoping for something like this: [LJASS]UnitAddLeash takes unit which, real leashX, real leashY, real leashLength[/LJASS].
 

hgkjfhfdsj

Active Member
Reaction score
55
>What is running Leashocity?
i think how this system is used is actually by calling Leashocity() in a starts effect Event. eg

[GUI]Event
A Unit Starts the Effect of an Ability
--
Action
call Leashocity()[/GUI]
 

Ayanami

칼리
Reaction score
288
>What is running Leashocity?
i think how this system is used is actually by calling Leashocity() in a starts effect Event. eg

[GUI]Event
A Unit Starts the Effect of an Ability
--
Action
call Leashocity()[/GUI]

But wouldn't that cause "GetSpellTargetUnit()" in function "Leashocity" to be null?
 

Bribe

vJass errors are legion
Reaction score
67
> But wouldn't that cause "GetSpellTargetUnit()" in function "Leashocity" to be null?

No. Calling a function does not cause any problems. ExecuteFunc("FuncName") will not cause any, either. Calling TriggerEvaluate() or TriggerExecute() will obviously cause GetTriggeringTrigger() to return that trigger instead of the original event trigger, which is just one more reason why function interfaces are poor.

I don't know why any one has the idea to clean up the IfElseThen function when it's clear I did that in the first reply because no one optimizes like I do.
 

Bribe

vJass errors are legion
Reaction score
67
Indeed, but it factors in collision size ;)

(which has pros and cons)
 

hgkjfhfdsj

Active Member
Reaction score
55
>function ifiselse compares getenumunit with null unit's position
JASS:
//showing you on your code since you might be more familiar with it..
private function IfIsElse takes nothing returns nothing
    local real X = GetUnitX(GetEnumUnit())
    local real Y = GetUnitY(GetEnumUnit())
    local real dx = GetUnitX(GetEnumUnit()) - GetUnitX(GetTriggerUnit()) //if this function is executed on timer expire, there isnt a gettriggerunit. its the incorrect event response. in other words, your not minusing the getenumunit's position with the caster..
    local real dy = GetUnitY(GetEnumUnit()) - GetUnitY(GetTriggerUnit()) //you would need to attach the caster to theenumunit, or otherwise an index/array (and not having to worry about leaks)
    set TempReal = SquareRoot(dx * dx + dy * dy)
    call BJDebugMsg("IfIsElse function")
        if TempReal >= range then
            if DoesDamage == true then
                call BJDebugMsg("Damaging Unit/Moving")
                call SetUnitState(GetEnumUnit(), UNIT_STATE_LIFE, GetWidgetLife(GetEnumUnit()) - Damage )
                call SetUnitX(GetEnumUnit(), X - 20)
                call SetUnitY(GetEnumUnit(), Y - 20)
                call IssueImmediateOrder(GetEnumUnit(), "Stop")
            else
        call BJDebugMsg("Moving Unit")
        call SetUnitX(GetEnumUnit(), X - 20)
        call SetUnitY(GetEnumUnit(), Y - 20)
        call IssueImmediateOrder(GetEnumUnit(), "Stop")
    endif
endif
endfunction
 

NeuroToxin

New Member
Reaction score
46
So, how would I do this? Hashtable?

EDIT: After staring at my code, heres what I came up with
EDIT:EDIT: If I have it taking a unit u, a unit t, and a range, will writing call UnitAddLeash(GetSpellTargetUnit(), GetTriggerUnit(), 1000) bug?
JASS:
library LeashSystem
//===========================================================================
//==================================SETUP====================================
//===========================================================================
globals
//These values are indented as priority, ones that aren't indented require no touching.
timer time = CreateTimer()
        real range = 1000//This is the range of the leash.
unit LeashedUnit//LeashedUnit
unit Caster//Caster
group LeashedUnits//The leashed units.
    boolean DoesDamage = false//Deals Damage to the unit for everytime it goes out, initially set to false, can be set to true.
        real Damage = 30//If DoesDamage = true, then it will do This damage everytime it goes out.
real TempReal//Don't Touch

endglobals
//===========================================================================
//=================================ENDSETUP==================================
//===========================================================================

private function IfIsElse takes nothing returns nothing
    local real tx
    local real ty
    local real X
    local real Y
    local real dx
    local real dy
        set Caster = GetTriggerUnit()
        set LeashedUnit = GetEnumUnit()
        set tx = GetUnitX(LeashedUnit)
        set ty = GetUnitY(LeashedUnit)
        set X = GetUnitX(Caster)
        set Y = GetUnitY(Caster)
        set dx = X - tx
        set dy = Y - ty
        set TempReal = SquareRoot(dx * dx + dy * dy)
            if TempReal >= range then
                if DoesDamage == true then
                    call SetUnitState(GetEnumUnit(), UNIT_STATE_LIFE, GetWidgetLife(GetEnumUnit()) - Damage )
                    call SetUnitX(GetEnumUnit(), X - 20)
                    call SetUnitY(GetEnumUnit(), Y - 20)
                    call IssueImmediateOrder(GetEnumUnit(), "Stop")
                else
            call SetUnitX(GetEnumUnit(), X - 20)
            call SetUnitY(GetEnumUnit(), Y - 20)
            call IssueImmediateOrder(GetEnumUnit(), "Stop")
        endif
    endif
endfunction

private function ForFun takes nothing returns nothing
       call ForGroup(LeashedUnits, function IfIsElse)
endfunction

function UnitAddLeash takes unit u, unit t, real r returns nothing
        set LeashedUnit = u
        set Caster = t
        set range = r
            call GroupAddUnit(LeashedUnits, LeashedUnit)
        set LeashedUnit = null
        set Caster = null
            call TimerStart(time, .03, true, function ForFun)
endfunction


//===========================================================================
//=================================INIT======================================
//===========================================================================
private function InitTrig_Leash_System takes nothing returns nothing
endfunction

endlibrary
 

Laiev

Hey Listen!!
Reaction score
188
you can remove this:

JASS:
//===========================================================================
//=================================INIT======================================
//===========================================================================
private function InitTrig_Leash_System takes nothing returns nothing
endfunction
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top