Well, i need a simple dash using JNGP. I need it to learn more about jass and all, so please make it simple and easy to understand!

library DASH initializer init requires Alloc, GetWidgetMeasures // Alloc is a library to manage struct creation and deletion
globals // with vJASS we can define globals freely, here's an example
private constant real MINDIST = 116. //this is the min dist for melee units. this variable is only valid inside the library, for being "private"
private constant real DT = 0.02 // timer interval...
private constant real SPEED = 1300. // sets the unit speed...
private hashtable HT = InitHashtable() // this is the key to make any spell MUI...
private key DKEY // this "type" of data is a constant integer which is unique for this code. Useful with hastables...
endglobals
private struct data extends array // this struct allos to store data in one variable name
unit c // we define the type of objects or handle the struct will use
unit t // in this case, the caster unit and the target unit
timer tm
implement Alloc // injects alloc into the struct
method destroy takes nothing returns nothing
set .c = null // a short way to write "this.c"
set .t = null
call PauseTimer(.tm)
call .deallocate() // very important frees the data to be used later...
endmethod
private static method Loop takes nothing returns nothing
// this method is only callable inside the struct. Any function outside the struct won't be able to call it.
local thistype D = thistype(LoadInteger(HT, GetHandleId(GetExpiredTimer()), DKEY)) // we get the WHOLE DATA
local real a = GetWidgetsAngle(D.c, D.t) // we now can call any part of the data in this function!!
local real d = GetWidgetsDistance(D.c, D.t)
if d <= MINDIST then
call D.destroy()
return
endif
call SetUnitFacing(D.c, a)
call SetUnitX(D.c, GetUnitX(D.c) + SPEED * DT * Cos(a))
call SetUnitY(D.c, GetUnitY(D.c) + SPEED * DT * Sin(a))
endmethod
method start takes nothing returns nothing
call SaveInteger(HT, GetHandleId(this.tm), DKEY, integer(this)) // this is the data struct, integer(this) will get the index of the data
call TimerStart(this.tm, DT, true, function thistype.Loop)
endmethod
static method create takes unit caster, unit target returns thistype
local thistype d = thistype.allocate() // thistype refers to data, allocate creates the data struct
set d.c = caster // now we set the components of the struct...
set d.t = target
if d.tm == null then
set d.tm = CreateTimer()
endif
return d
endmethod
endstruct
private function dashit takes nothing returns boolean
local data D
if GetIssuedOrderId() == OrderId("Attack") then
call DisplayTimedTextFromPlayer(Player(0), 0,0,3, "c: " + GetUnitName(GetTriggerUnit())+ "| t: " + GetUnitName(GetOrderTargetUnit()))
set D = data.create(GetTriggerUnit(), GetOrderTargetUnit())
call D.start() // a variable can use their own functions :D
endif
return false
endfunction
private function init takes nothing returns nothing
local trigger t = CreateTrigger()
call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_ISSUED_TARGET_ORDER)
call TriggerAddCondition(t, Condition(function dashit)) // we use Conditions because they're faster then Actions...
set t = null
endfunction
endlibrary
Quote from: Judash137 on January 29, 2013, 10:44:44 AMDo you need an ability to cast it?? ok...
I cant see how it work if it cant even have a test ability trigger to do that, guy!
Quote from: rvonsonsnadtz on January 29, 2013, 12:08:00 PMYes, a global array is way faster but searching in it will be slow as more data is stored in the array. In the other hand, hashtables are a little bit slower but searching and getting data is way faster and totally direct. You won't notice any difference...
Hmm, Moyack, you mentioned hashtables. Now, I am very curious about the speed in those. I mean, a normal global variable with an array should be faster, but that speed difference, is it noticable, or am I a bit overanalytic?
local trigger t = CreateTrigger()
call RegisterEvent(t, blahblah)
call TriggerAddAction(t, blahblah)
set t = null
library DASH initializer init requires Alloc, GetWidgetMeasures // Alloc is a library to manage struct creation and deletion
globals // with vJASS we can define globals freely, here's an example
private constant integer ABILID = 'A000' //This will define the ability rawcode we'll use...
private constant real MINDIST = 116. //this is the min dist for melee units. this variable is only valid inside the library, for being "private"
private constant real DT = 0.02 // timer interval...
private constant real SPEED = 1300. // sets the unit speed...
private hashtable HT = InitHashtable() // this is the key to make any spell MUI...
private key DKEY // this "type" of data is a constant integer which is unique for this code. Useful with hastables...
endglobals
private struct data extends array // this struct allos to store data in one variable name
unit c // we define the type of objects or handle the struct will use
unit t // in this case, the caster unit and the target unit
timer tm
implement Alloc // injects alloc into the struct
method destroy takes nothing returns nothing
set .c = null // a short way to write "this.c"
set .t = null
call PauseTimer(.tm)
call .deallocate() // very important frees the data to be used later...
endmethod
private static method Loop takes nothing returns nothing
// this method is only callable inside the struct. Any function outside the struct won't be able to call it.
local thistype D = thistype(LoadInteger(HT, GetHandleId(GetExpiredTimer()), DKEY)) // we get the WHOLE DATA
local real a = GetWidgetsAngle(D.c, D.t) // we now can call any part of the data in this function!!
local real d = GetWidgetsDistance(D.c, D.t)
if d <= MINDIST then
call PauseUnit(D.c, false) // unpuase the unit
call IssueTargetOrder(D.c, "attack", D.t)
call D.destroy()
return
endif
call SetUnitFacing(D.c, a)
call SetUnitX(D.c, GetUnitX(D.c) + SPEED * DT * Cos(a))
call SetUnitY(D.c, GetUnitY(D.c) + SPEED * DT * Sin(a))
endmethod
method start takes nothing returns nothing
call SaveInteger(HT, GetHandleId(this.tm), DKEY, integer(this)) // this is the data struct, integer(this) will get the index of the data
call PauseUnit(this.c, true) // this is to make the unit facing change work...
call TimerStart(this.tm, DT, true, function thistype.Loop)
endmethod
static method create takes unit caster, unit target returns thistype
local thistype d = thistype.allocate() // thistype refers to data, allocate creates the data struct
set d.c = caster // now we set the components of the struct...
set d.t = target
if d.tm == null then
set d.tm = CreateTimer()
endif
return d
endmethod
endstruct
private function dashit takes nothing returns boolean
local data D
if GetSpellAbilityId() == ABILID then
set D = data.create(GetTriggerUnit(), GetSpellTargetUnit())
call D.start() // a variable can use their own functions :D
endif
return false
endfunction
private function init takes nothing returns nothing
local trigger t = CreateTrigger()
call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_EFFECT)
call TriggerAddCondition(t, Condition(function dashit)) // we use Conditions because they're faster then Actions...
set t = null
endfunction
endlibraryQuote from: rvonsonsnadtz on January 29, 2013, 06:10:39 PMHave you set the function as [lcode=jass]library bla initializer <your funciton name>[/lcode]??
Btw, a bit off-topic, but Moyack, I see u use the
local trigger t = CreateTrigger()
call RegisterEvent(t, blahblah)
call TriggerAddAction(t, blahblah)
set t = null
However.. By some unknown reasons, my triggers using this won't fire.. No clue why.
They want the "set gg_trg_blahblah = CreateTrigger()"
Quote from: Judash137 on January 30, 2013, 03:44:38 AMI've added an ability to the footmen with the slow icon.
Well, it still have no ability to test yet!