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.
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top