To keep out a potential deluge of threads with very small functions, I've created this thread for you to post relatively short functions you wish to share.
Normal or long length snippets (nearing coding systems or libraries and so on) still go the same place as always.
So, if you have your own special GetClosestUnit, Log, or whatever function, post it here!
D'you guys think I should merge the GetPlayerColorTag functions into here? They sort of fit into either place, due to the fact that there have been several of them presented.
Functions
Get Closest Unit
Silvenon
//Uses global unit udg_ClosestUnit function GetClosestUnit takesreal x, real y, boolexpr e returnsunit localreal md = 100000 localgroup g = CreateGroup() localunit u localreal dx localreal dy call GroupEnumUnitsInRect(g, bj_mapInitialPlayableArea, e) loop set u = FirstOfGroup(g) exitwhen u == null call GroupRemoveUnit(g, u) set dx = GetUnitX(u) - x set dy = GetUnitY(u) - y if(dx * dx + dy * dy) / 100000 < md then set udg_ClosestUnit = u set md = (dx * dx + dy * dy) / 100000 endif endloop call DestroyGroup(g) call DestroyBoolExpr(e) set g = null return udg_ClosestUnit endfunction
quraji
//*************************** //----GetUnitSomewhere by Quraji---- //*************************** // // To use in your map simply paste this whole code into your custom script // section, or an empty trigger (above all functions that will use this code) // // (feel free to omit this section from your code, I only ask credit if // you copy this function for your own system/submitted code, no credits // needed if you use it in a map) // // Contains one main function: // // // GetUnitSomewhere takes real sourceX, real sourceY, real range, boolean outsiderange, boolean nearest, boolean considerdead, boolexpr cond returns unit // Don't let the number of parameters scare you, this is easy to use. This // function allows you to select the nearest or farthest unit inside or outside // a designated circle. // // Parameters: // // real sourceX and real sourceY // these are the X and Y coordinates of the point at the center of the area // to be considered. // // real range // this is the range of the function, or the radius of the area to be considered // (a circle). // // boolean outsiderange // if false, the function will consider units inside the circle, if true it will // consider all units selected BUT the ones in the circle. // (remember that the circle's radius is decided by the range parameter) // // boolean nearest // if true, the function will select the nearest unit to the center point inside // the area to be considered (as you'd expect a normal GetClosestUnit() // function to do. if false, however, it will select the farthest unit // // boolean considerdead // if false the function will ignore dead units. // (leave this at false unless you need the function to select recently killed // units) // // boolexpr cond // finally, this is where you add your own conditions if you need them by // using a boolexpr. if you don't, pass "Condition(function ReturnTrue)" // // function GetUnitSomewhereEx takes real x, real y, real range returns unit // A simplified version, returns a non-dead unit within range // // function GetUnitSomewhereExCond takes real x, real y, real range, boolexpr cond returns unit // A simplified version, returns a non-dead unit within range, matching condition // // Extra feature: // // This function sets the bj_lastHauntedGoldMine game variable to the unit // it selects, as well as returning the unit. This enables JASS users to use // the function as normal, but provides a convenience for GUI users, as they // can simply select the unit from the drop down menu for any unit field by // selecting "Last Haunted Gold Mine". This also allows all variables to be // nulled. If you wish this variable not to be tampered with, uncomment the // "return winner" line of code at the bottom of the function. //**********************
function ReturnTrue takesnothingreturnsboolean returntrue endfunction
function GetUnitSomewhere takesreal sourceX, real sourceY, real range, boolean outsiderange, boolean nearest, boolean considerdead, boolexpr cond returnsunit localgroup g=CreateGroup() localunit current localunit winner localreal currentX localreal currentY localreal dist localreal rangeorig = range
if outsiderange then call GroupEnumUnitsInRect(g,bj_mapInitialPlayableArea,cond) else call GroupEnumUnitsInRange(g,sourceX,sourceY,range,cond) endif
ifnot nearest then set range=.0 endif
loop
set current=FirstOfGroup(g) exitwhen current==null
if GetWidgetLife(current)>0.405 or considerdead then
set currentX=GetWidgetX(current)-sourceX set currentY=GetWidgetY(current)-sourceY set dist=SquareRoot(currentX*currentX+currentY*currentY)
if outsiderange then set range=dist set outsiderange = not outsiderange endif
if(nearest and dist<=range)or(not nearest and dist>=range)then
set winner=current set range=dist
endif
endif
endif
call GroupRemoveUnit(g,current) endloop
call DestroyGroup(g)
set g=null set current=null
//return winner //uncomment above line if you wish bj_lastHauntedGoldMine to remain //untampered with
set bj_lastHauntedGoldMine=winner set winner=null
return bj_lastHauntedGoldMine endfunction
function GetUnitSomewhereEx takesreal x, real y, real range returnsunit return GetUnitSomewhere(x,y,range,false,true,false, Condition(function ReturnTrue)) endfunction
function GetUnitSomewhereExCond takesreal x, real y, real range, boolexpr cond returnsunit return GetUnitSomewhere(x,y,range,false,true,false,cond) endfunction
Silvenon, Alternate version (fixes) by PurplePoot
//Uses global group udg_enumGrp //Uses global unit udg_ClosestUnit function GetClosestUnit takesreal x, real y, boolexpr e returnsunit localreal md = 100000 localunit u localreal dx localreal dy call GroupEnumUnitsInRect(udg_enumGrp, bj_mapInitialPlayableArea, e) loop set u = FirstOfGroup(udg_enumGrp) exitwhen u == null call GroupRemoveUnit(udg_enumGrp, u) set dx = GetUnitX(u) - x set dy = GetUnitY(u) - y if(dx * dx + dy * dy) / 100000 < md then set udg_ClosestUnit = u set md = (dx * dx + dy * dy) / 100000 endif endloop return udg_ClosestUnit endfunction
struct stundata unit u=null real d=0 boolean destroyplease=false endstruct
function StunUnit_KeepStunned takesnothingreturnsnothing localinteger i = 0 local stundata dat loop exitwhen i==Stun_Total set dat=StunArray[i] if dat.destroyplease then set Stun_Total=Stun_Total-1 set StunArray[i]=StunArray[Stun_Total] call dat.destroy() set i=i-1 else set dat.d=dat.d-0.01 call SetUnitPosition(dat.u,GetUnitX(dat.u),GetUnitY(dat.u)) set dat.destroyplease=(dat.d<=0) endif set i=i+1 endloop if Stun_Total==0 then call ReleaseTimer(GetExpiredTimer()) endif endfunction
function StunUnit takesunit u,real d returnsnothing local stundata dat=stundata.create() set dat.d=d set dat.u=u if(Stun_Total==0)then call TimerStart(NewTimer(),.01,true,function StunUnit_KeepStunned) endif set Stun_Total=Stun_Total+1 set StunArray[Stun_Total-1]=dat endfunction
library QuickTimer //============================================================================ // Attaches and integer to a timer and makes it expire after about 0 seconds. // function TimerExpireQuick takestimer t, integer data, code handlerFunc returnsnothing call TimerStart(t, data/2147483648., false, handlerFunc) endfunction
//============================================================================ // Returns the integer attached to a timer. // function QuickTimerGetData takestimer t returnsinteger return R2I(TimerGetTimeout(t)*2147483648.) endfunction endlibrary
Last edited by PurplePoot; 09-13-2008 at 09:40 PM..
function GetClosestUnit takesreal x, real y, boolexpr e returnsunit localreal md = 100000 localreal d localgroup g = CreateGroup() localunit u localreal dx localreal dy call GroupEnumUnitsInRect(g, bj_mapInitialPlayableArea, e) loop set u = FirstOfGroup(g) exitwhen u == null call GroupRemoveUnit(g, u) set dx = GetUnitX(u) - x set dy = GetUnitY(u) - y if(dx * dx + dy * dy) / 100000 < md then set udg_ClosestUnit = u set md = (dx * dx + dy * dy) / 100000 endif endloop call DestroyGroup(g) call DestroyBoolExpr(e) set g = null return udg_ClosestUnit endfunction
Instructions:
Before using anything, you must create a global variable named ClosestUnit of type unit.
Parameters:
real x - x coordinate of the point from where the function searches for the closest unit
real y - y coordinate of the point from where the function searches for the closest unit
boolexpr e - used for filtering (enemy, illusion, hero...), you can put null if you don't want any filter
You can use the function in two ways:
function FirstWay takesnothingreturnsnothing localunit u = SomeUnit() localunit c set c = GetClosestUnit(GetUnitX(u), GetUnitY(u)) call DoSomethingWithUnit(c) set u = null set c = null endfunction
Or:
function SecondWay takesnothingreturnsnothing localunit u = SomeUnit() call GetClosestUnit(GetUnitX(u), GetUnitY(u)) call DoSomethingWithUnit(udg_ClosestUnit) set u = null endfunction
__________________
PurgeandFire111: Then you can delete the sound and whala... You have the label,filepath, and other parameters. Silvenon: It's voila, not whala, you... stupidhead :) PurgeandFire111: Yeah, I was in a rush. =( *cry* Herman: Voila is an instrument that I quit playing in 6th grade, whala works just fine if not better PurgeandFire111: No, viola is an instrument. Stupidhead... =D
Well, I suppose it's easier to use Closest as a global rather than the H2I trick...
Yeah, that's what I thought too, so I hope it's ok if I leave it like that.
Quote:
Skip the player, let them pass a boolexpr.
Yeah you're right, that way they can filter with more flexibility than just a IsUnitEnemy filter :P
__________________
PurgeandFire111: Then you can delete the sound and whala... You have the label,filepath, and other parameters. Silvenon: It's voila, not whala, you... stupidhead :) PurgeandFire111: Yeah, I was in a rush. =( *cry* Herman: Voila is an instrument that I quit playing in 6th grade, whala works just fine if not better PurgeandFire111: No, viola is an instrument. Stupidhead... =D
struct stundata unit u=null real d=0 boolean destroyplease=false endstruct
function StunUnit_KeepStunned takesnothingreturnsnothing localinteger i = 0 local stundata dat loop exitwhen i==Stun_Total set dat=StunArray[i] if dat.destroyplease then set Stun_Total=Stun_Total-1 set StunArray[i]=StunArray[Stun_Total] call dat.destroy() set i=i-1 else set dat.d=dat.d-0.01 call SetUnitPosition(dat.u,GetUnitX(dat.u),GetUnitY(dat.u)) set dat.destroyplease=(dat.d<=0) endif set i=i+1 endloop if Stun_Total==0 then call ReleaseTimer(GetExpiredTimer()) endif endfunction
function StunUnit takesunit u,real d returnsnothing local stundata dat=stundata.create() set dat.d=d set dat.u=u if(Stun_Total==0)then call TimerStart(NewTimer(),.01,true,function StunUnit_KeepStunned) endif set Stun_Total=Stun_Total+1 set StunArray[Stun_Total-1]=dat endfunction
You should probably mention there is a global integer array StunArray and also that the function requires JassNewGen, also I don't see any actual stunning going on (just moving a unit).
It also uses a timer stack (release timer) which you havent specified, other then that the function looks good and efficient
Yea its an alternative way of stunning without using A Stormbolt spell with 100 levels.
On my map the function also takes an effect + attachment point so you can customise the overhead buff. I'd recommend the same.
(Id need to included the whole AddTimedSfx for that).
Yeah what I was saying is that you dont have IssueTargetOrder order for the spell or whatever you need to do to stun. Someone will put this in their map and say WTF this doesnt do anything. I know how custom stun systems work, they stun forever and then a timer removes the buff. There is however none of that in what you posted
it would be nice if you did an alternate version though since not everyone uses vJass (though it would be nice if they did...)
I can do that if you want. I just have to tell them to create a global.
__________________
PurgeandFire111: Then you can delete the sound and whala... You have the label,filepath, and other parameters. Silvenon: It's voila, not whala, you... stupidhead :) PurgeandFire111: Yeah, I was in a rush. =( *cry* Herman: Voila is an instrument that I quit playing in 6th grade, whala works just fine if not better PurgeandFire111: No, viola is an instrument. Stupidhead... =D
Also, it'd be easier to just store the struct to a cache on a timer with the duration of the stun, and then remove the stun after the end of the duration. (However you need to Pause them this way)
Or cast a dummy ability that stuns forever then remove the buff after long enough, thus also adding a buff.
Anyways, boolexpr e? Reading a few functions recently? -.- (That's what they use in common and blizzard)
I didn't see this. Hahaha......yeah, I have been reading a few functions recently :)
__________________
PurgeandFire111: Then you can delete the sound and whala... You have the label,filepath, and other parameters. Silvenon: It's voila, not whala, you... stupidhead :) PurgeandFire111: Yeah, I was in a rush. =( *cry* Herman: Voila is an instrument that I quit playing in 6th grade, whala works just fine if not better PurgeandFire111: No, viola is an instrument. Stupidhead... =D