SerraAvenger
Cuz I can
- Reaction score
- 234
The current version was not tested yet, so take care.
This is a small scope snipper that I have found useful.
It is open to suggestions of any kind.
All you have to do is:
BEFORE FIRST USE
I externalised all the hard work into a superstruct. Not tested yet
Here is the code of each spell instance
Perhaps I will change it to use Jesus4Lyf's initial event system.
This is a small scope snipper that I have found useful.
It is open to suggestions of any kind.
All you have to do is:
BEFORE FIRST USE
- copy the superstruct library into a custom script trigger.
- Copy the childstruct into a custom script section.
- Rename the scope to something unique (representing the spell, if possible)
- Specify the ABIL_ID (the raw-id of the ability that shall run the trigger) and the EVENT (the event that shall fire the trigger).
- Enter your spell code into the '.init' method. Also change the zero to the duration of the spell
- If the spell has a periodic component, you can use the global real variable 'PERIOD'. Every PERIOD seconds, all structs of type data will be run through with a call to the structs 'loopAction'. PERIOD should be smaller than 10% of the minimum struct life span, structs might live longer elsewise.
- After the struct's loopAction has been called, the code will automatically check wether the struct has reached the end of its life and needs to go the way of all mortal structs. If so, the structs '.clear' method will be called. This should be used to clear all data from the struct, like for example effects, dummy units or whatsoever.
- It is really easy and fast to use
- All the nasty background stuff is already done for you
- It is quite flexible
- Apart from JASSHelper, it is stand-alone
- It requires JASSHelper
- The Current Version is not Tested
- It needs one trigger per spell instance :/
I externalised all the hard work into a superstruct. Not tested yet
JASS:
library SJ_SPELL_DATA
struct sj_spell_data
unit caster
real x
real y
integer ownerId
integer level
real timeRemaining
integer id
static data_instance array self
static integer count = 0
static constant timer TIMER = CreateTimer()
stub method loopAction takes nothing returns nothing
endmethod
stub method init takes nothing returns nothing
endmethod
stub method clear takes nothing returns nothing
endmethod
static method handleData takes nothing returns nothing
local integer i = 0
local data_instance curData
loop
exitwhen i >= .count
set curdata_instance = .self[ i ]
call curdata_instance.loopAction()
set .timeRemaining = .timeRemaining - PERIOD
if .timeRemaining <= 0 then
call .destroy()
endif
set i = i + 1
endloop
endmethod
static method create takes nothing returns sj_spell_data
local sj_spell_data this = .allocate()
call .init()
set .caster = GetTriggerUnit()
set .x = GetUnitX( .caster )
set .y = GetUnitY( .caster )
set .level = GetUnitAbilityLevel( trigUnit, ABIL_ID )
set .ownerId = GetPlayerId( GetOwningPlayer( .caster ) )
set .timeRemaining = PERIOD
set .self[ .count ] = this
set .id = .count
set .count = .count + 1
if .count == 1 then
call TimerStart( .TIMER, PERIOD, true, function data_instance.handledata_instance )
endif
return this
endmethod
method setRemaining takes real timeremaining returns nothing
if timeremaining <= 0 then
call .destroy()
else
set .timeRemaining = timeremaining
endif
endmethod
method getRemaining takes nothing returns real
return .timeRemaining
endmethod
private method onDestroy takes nothing returns nothing
call .clear()
set .count = .count - 1
if .id != .count then
set .self[ .id ] = .self[ .count ]
set .self[ .id ].id = .id
endif
set .self[ .count ] = 0
if .count == 0 then
call PauseTimer( .TIMER )
endif
endmethod
endstruct
endlibrary
Here is the code of each spell instance
JASS:
scope Spell
globals
private constant real PERIOD = 0.04
private constant integer ABIL_ID = 039;A000039;
private constant playerunitevent EVENT = EVENT_PLAYER_UNIT_SPELL_EFFECT
endglobals
private struct data_instance extends sj_spell_data
//precreated variables...
// caster --> unit // the caster
// x --> real // the caster's x position
// y --> real // the caster's y position
// ownerId --> integer // the player id of the caster's owner
// level --> integer // the level of the spell
//add own variables here
method loopAction takes nothing returns nothing
// will be called every PERIOD
endmethod
method init takes nothing returns nothing
// will be called on struct creation
call .setRemaining( 0 ) // Replace 0 with the duration of the spell. 0 will instantly destroy the spell!
endmethod
method clear takes nothing returns nothing
// please clear all handle variables here. Will be called when the .destroy() method is evoked.
endmethod
// -----------------------------------
// Please don't change the following
// -----------------------------------
static constant trigger spellTrigger = CreateTrigger()
static method actions takes nothing returns nothing
if GetSpellAbilityId() == ABIL_ID then
call .create()
endif
endmethod
private static method onInit takes nothing returns nothing
call TriggerRegisterAnyUnitEventBJ( .spellTrigger, EVENT )
call TriggerAddAction( .spellTrigger, function .actions )
endmethod
endstruct
endscope
Perhaps I will change it to use Jesus4Lyf's initial event system.