Which unit?

tommerbob

Minecraft. :D
Reaction score
110
I've got a spell that when a unit comes within 50 of a trap, the unit is ensnared, and the trap is removed from the game. Code:

JASS:

private struct Trap extends array
    //! runtextmacro AIDS()
    private trigger t
    private triggeraction ta
	
    private static method Act takes nothing returns nothing
        local thistype d = thistype[GetTriggerUnit()]
        call UnitAddAbility(DUMMY, TRAP_ID)   //Add dummy buff
        call IssueTargetOrder(DUMMY, "ensnare", d.unit)
        call UnitRemoveAbility(DUMMY, TRAP_ID)
        call RemoveUnit(????)   //                       <---------- How do I reference the trap unit to remove it?
    endmethod
    
    private static method AIDS_filter takes unit u returns boolean
        return GetUnitTypeId(u) == TRAP 
    endmethod
    
    private method AIDS_onCreate takes nothing returns nothing
        set this.t = CreateTrigger()
        set this.ta = TriggerAddAction(this.t,function thistype.Act)
        call TriggerRegisterUnitInRange(this.t, this.unit, 50., null)
    endmethod
    
    private method AIDS_onDestroy takes nothing returns nothing
        call TriggerRemoveAction(this.t, this.ta)
        call DestroyTrigger(this.t)
        set this.ta = null
        set this.t = null
    endmethod
endstruct


In the Act method, the trap is supposed to be removed. The problem is, how do I reference the trap?
 

Laiev

Hey Listen!!
Reaction score
188
Use a variable member to define it.

Also AIDS should be used bellow everything.
 

Dirac

22710180
Reaction score
147
No wait i'm a bit confused.
first your struct indexes only TRAP type units.
When they're indexed a trigger is created for each one of them that detects whenever a unit comes unto range
When this trigger fires is when the mess begins
[ljass]local thistype d = thistype[GetTriggerUnit()][/ljass] I suppose that here you're reffering to the TRAP, to avoid a trigger evaluation just use [ljass]local thistype d = GetUnitUserData(GetTriggerUnit())[/ljass]
Then you're adding an ability to a global dummy and make it cast it upon the TRAP? why?
There's a native called GetEnterUnit or GetEnteringUnit.
Also you should know that the event "unit enters range" is a periodic check (every 0.1 seconds) for every unit nearby it, you could do it manually.
And yes, the AIDS textmacro should be run at the end.
 

Sgqvur

FullOfUltimateTruthsAndEt ernalPrinciples, i.e shi
Reaction score
62
Hm, the best thing that comes to my mind is to attach the whole Trap struct to the trigger:

JASS:
private struct Trap extends array
    //! runtextmacro AIDS()
    private trigger t
    private triggeraction ta
	
    private static method Act takes nothing returns nothing
        local Trap t = LoadInteger(<some hashtable>, H2I(GetTriggeringTrigger()), 0xDirac) // get the attached Trap struct from the trigger
        call UnitAddAbility(DUMMY, TRAP_ID)
        call IssueTargetOrder(DUMMY, "ensnare", d.unit)
        call UnitRemoveAbility(DUMMY, TRAP_ID)
        call RemoveUnit(t.unit) // t.unit shold work =)
    endmethod
    
    private static method AIDS_filter takes unit u returns boolean
        return GetUnitTypeId(u) == TRAP 
    endmethod
    
    private method AIDS_onCreate takes nothing returns nothing
        set this.t = CreateTrigger()
        set this.ta = TriggerAddAction(this.t,function thistype.Act)
        call TriggerRegisterUnitInRange(this.t, this.unit, 50., null)
        call SaveInteger(<some hashtable>, H2I(this.t), 0xDirac /* joke poke =)*/,  this) // attach the Trap struct to the trigger
    endmethod
    
    private method AIDS_onDestroy takes nothing returns nothing
        call TriggerRemoveAction(this.t, this.ta)
        call DestroyTrigger(this.t)
        set this.ta = null
        set this.t = null
    endmethod
endstruct


@Dirac
The GetEnterUnit or GetEnteringUnit is a reference to the unit that enters in range of the trap, not the trap itself, and it's equvalent to the GetTriggerUnit() native.
 

tommerbob

Minecraft. :D
Reaction score
110
No wait i'm a bit confused.
first your struct indexes only TRAP type units.
When they're indexed a trigger is created for each one of them that detects whenever a unit comes unto range
When this trigger fires is when the mess begins
[ljass]local thistype d = thistype[GetTriggerUnit()][/ljass] I suppose that here you're reffering to the TRAP, to avoid a trigger evaluation just use [ljass]local thistype d = GetUnitUserData(GetTriggerUnit())[/ljass]
Then you're adding an ability to a global dummy and make it cast it upon the TRAP? why?
There's a native called GetEnterUnit or GetEnteringUnit.
Also you should know that the event "unit enters range" is a periodic check (every 0.1 seconds) for every unit nearby it, you could do it manually.
And yes, the AIDS textmacro should be run at the end.

GetTriggerUnit() and GetEnteringUnit() return the same thing, which is not what I need.

JASS:


This references the entering unit, not the trap. I need it to reference the trap so that I can remove it. How do I do that?

@Sgqvur: Is using a hashtable the only way? I really don't want to use one if I can avoid it. I just don't like them.

Also, why do I need to run Aids textmacro at the end? I've always done it at the beginning, and everything works fine.
 

Dirac

22710180
Reaction score
147
There is a very obvious reason you have to run it at the end: Trigger evaluations.
Remember that JASS can't read functions below other functions, only above.
The textmacro calls the AIDS_Whatever function everytime an indexing event is fired, but these functions would be below them if the textmacro is at the top.
And yes, it seems that the only way to reffer to that unit is by hashtable use, to store the trigger's handle.
Again, use the GetUnitUserData instead of the operator.
 

tommerbob

Minecraft. :D
Reaction score
110
There is a very obvious reason you have to run it at the end: Trigger evaluations.

Its not obvious to everyone. I don't even know what a trigger evaluation is.

Remember that JASS can't read functions below other functions, only above.
The textmacro calls the AIDS_Whatever function everytime an indexing event is fired, but these functions would be below them if the textmacro is at the top.

I realize this is true, but then why don't I get errors from all of my Aids structs? Cause I always do the textmacro's at the top.
 

Dirac

22710180
Reaction score
147
Because vJass compensates with trigger evaluations (which are way slower) and code repetition (which sucks) to be able to call the function. This behavior happens only inside structs.

A trigger evaluation in a nutshell...
JASS:
local trigger trig = CreateTrigger()
call TriggerAddCondition(trig,Filter(function SomeFunc))
call TriggerEvaluate(trig)

The function SomeFunc is called, but through a trigger evaluation, instead of just
JASS:
call SomeFunc()
 

tommerbob

Minecraft. :D
Reaction score
110
Hmm, okay. I think that makes sense.

I created a hashtable, but I'm getting all sorts of syntax errors. What is the H2I function? It doesn't work.

JASS:
private struct Trap extends array
    private trigger t
    private triggeraction ta
	
    private static method Act takes nothing returns nothing
        local Trap t = LoadInteger(H, H2I(GetTriggeringTrigger()), 0) // get the attached Trap struct from the trigger
        call UnitAddAbility(DUMMY, TRAP_ID)
        call IssueTargetOrder(DUMMY, "ensnare", GetTriggerUnit())
        call UnitRemoveAbility(DUMMY, TRAP_ID)
        call RemoveUnit(t.unit)
        call FlushChildHashtable(H, H2I(t.t))
    endmethod
    
    private static method AIDS_filter takes unit u returns boolean
        return GetUnitTypeId(u) == TRAP 
    endmethod
    
    private method AIDS_onCreate takes nothing returns nothing
        set this.t = CreateTrigger()
        set this.ta = TriggerAddAction(this.t,function thistype.Act)
        call TriggerRegisterUnitInRange(this.t, this.unit, 50., null)
        call SaveInteger(H, H2I(this.t), 0, this) // attach the Trap struct to the trigger
    endmethod
    
    private method AIDS_onDestroy takes nothing returns nothing
        call TriggerRemoveAction(this.t, this.ta)
        call DestroyTrigger(this.t)
        set this.ta = null
        set this.t = null
    endmethod
    //! runtextmacro AIDS()
endstruct
 

luorax

Invasion in Duskwood
Reaction score
67
H2I = [ljass]GetHandleId[/ljass]. There was a bug before 1.24; by returning 0 at the end of any function, you were able to convert things to different types. Just like this:

JASS:
function H2I takes handle h returns integer
    return h
    return 0
endfunction

function I2H takes integer i returns handle 
    return i
    return 0
endfunction


And this worked for everything. You could convert a destructable to a trackable, using its pointer. But it won't work anymore, it was fixed in 1.24
 

tommerbob

Minecraft. :D
Reaction score
110
Okay it works. :)

Am I flushing the hashtable correctly? I never use them, so I don't know what I'm doing. :p
 

luorax

Invasion in Duskwood
Reaction score
67
You should use a Table instead IMO.

There were a higher version on THW but I couldn't find it. Maybe it got deleted after Vexorian decided to update the old one? IDK, Bribe's one looked better.
 

luorax

Invasion in Duskwood
Reaction score
67
Well, then THW's search function is bugged; I searched for "Table" and set "Bribe" as user name and didn't show me that topic. W/E, that's the latest version, use that then.

Extremely useful +1
 
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