- Joined
- Mar 29, 2016
- Messages
- 688
RESOURCE PRELOADER v1.5.0
This snippet allows to preload your map resources. When first time a resource is created in your map, it lags a bit. In order to prevent that, this system preloads them at map initialization.
This system simplifies your tasks in preloading with its simple APIs. Furthermore, this system ensures that there will be no duplicates on the resource that will be preloaded which is nice.
Importing:
- Create an empty script in your map
- Copy the script below and then paste it to the empty script stated above
- You're done
Resource Preloader Script
Sample Usage
This snippet allows to preload your map resources. When first time a resource is created in your map, it lags a bit. In order to prevent that, this system preloads them at map initialization.
This system simplifies your tasks in preloading with its simple APIs. Furthermore, this system ensures that there will be no duplicates on the resource that will be preloaded which is nice.
Importing:
- Create an empty script in your map
- Copy the script below and then paste it to the empty script stated above
- You're done
Resource Preloader Script
JASS:
library ResourcePreloader /* v1.5.0 https://www.hiveworkshop.com/threads/287358/
*/uses /*
*/optional BJObjectId /* http://www.hiveworkshop.com/threads/287128/
*/optional Table /* http://www.hiveworkshop.com/threads/188084/
*/optional UnitRecycler /* http://www.hiveworkshop.com/threads/286701/
*///! novjass
[CREDITS]
/*
AGD - Author
IcemanBo - for suggesting further improvements
Silvenon - for the sound preloading method
JAKEZINC - Suggestions
*/
|-----|
| API |
|-----|
function PreloadUnit takes integer rawcode returns nothing/*
- Assigns a certain type of unit to be preloaded
*/function PreloadItem takes integer rawcode returns nothing/*
- Assigns a certain type of item to be preloaded
*/function PreloadAbility takes integer rawcode returns nothing/*
- Assigns a certain type of ability to be preloaded
*/function PreloadEffect takes string modelPath returns nothing/*
- Assigns a certain type of effect to be preloaded
*/function PreloadSound takes string soundPath returns nothing/*
- Assigns a certain type of sound to be preloaded
The following functions requires the BJObjectId library:
*/function PreloadUnitEx takes integer start, integer end returns nothing/*
- Assigns a range of units to be preloaded
*/function PreloadItemEx takes integer start, integer end returns nothing/*
- Assigns a range of items to be preloaded
*/function PreloadAbilityEx takes integer start, integer end returns nothing/*
- Assigns a range of abilities to be preloaded
*///! endnovjass
/*
* Configuration
*/
globals
/*
* Preload dummy unit type id
*/
public integer PRELOAD_UNIT_TYPE_ID = 'uloc'
/*
* Owner of the preload dummy
*/
public player PRELOAD_UNIT_OWNER = Player(PLAYER_NEUTRAL_PASSIVE)
/*
* Dummy unit's y-coordinate will be positioned at (Max Y of WorldBounds) + this value
*/
public real PRELOAD_UNIT_Y_BOUNDS_EXTENSION = 0.00
endglobals
/*========================================================================================================*/
/* Do not try to change below this line if you're not so sure on what you're doing. */
/*========================================================================================================*/
private keyword S
/*============================================== TextMacros ==============================================*/
//! textmacro PRELOAD_TYPE takes NAME, ARG, TYPE, INDEX, I
function Preload$NAME$ takes $ARG$ what returns nothing
static if LIBRARY_Table then
if S.tb[$I$].boolean[$INDEX$] then
return
endif
set S.tb[$I$].boolean[$INDEX$] = true
call Do$NAME$Preload(what)
else
if LoadBoolean(S.tb, $I$, $INDEX$) then
return
endif
call SaveBoolean(S.tb, $I$, $INDEX$, true)
call Do$NAME$Preload(what)
endif
endfunction
//! endtextmacro
//! textmacro RANGED_PRELOAD_TYPE takes NAME
function Preload$NAME$Ex takes integer start, integer end returns nothing
local boolean forward = start < end
loop
call Preload$NAME$(start)
exitwhen start == end
if forward then
set start = BJObjectId(start).plus_1()
exitwhen start > end
else
set start = BJObjectId(start).minus_1()
exitwhen start < end
endif
endloop
endfunction
//! endtextmacro
/*========================================================================================================*/
private function DoUnitPreload takes integer id returns nothing
static if LIBRARY_UnitRecycler then
call RecycleUnitEx(CreateUnit(Player(PLAYER_NEUTRAL_PASSIVE), id, 0, 0, 270))
else
call RemoveUnit(CreateUnit(Player(PLAYER_NEUTRAL_PASSIVE), id, 0, 0, 0))
endif
endfunction
private function DoItemPreload takes integer id returns nothing
call RemoveItem(UnitAddItemById(S.dummy, id))
endfunction
private function DoAbilityPreload takes integer id returns boolean
return UnitAddAbility(S.dummy, id) and UnitRemoveAbility(S.dummy, id)
endfunction
private function DoEffectPreload takes string path returns nothing
call DestroyEffect(AddSpecialEffect(path, GetUnitX(S.dummy), GetUnitY(S.dummy)))
endfunction
private function DoSoundPreload takes string path returns nothing
local sound s = CreateSound(path, false, false, false, 10, 10, "")
call SetSoundVolume(s, 0)
call StartSound(s)
call KillSoundWhenDone(s)
set s = null
endfunction
//! runtextmacro PRELOAD_TYPE("Unit", "integer", "unit", "what", "0")
//! runtextmacro PRELOAD_TYPE("Item", "integer", "item", "what", "1")
//! runtextmacro PRELOAD_TYPE("Ability", "integer", "ability", "what", "2")
//! runtextmacro PRELOAD_TYPE("Effect", "string", "effect", "StringHash(what)", "3")
//! runtextmacro PRELOAD_TYPE("Sound", "string", "sound", "StringHash(what)", "4")
static if LIBRARY_BJObjectId then
//! runtextmacro RANGED_PRELOAD_TYPE("Unit")
//! runtextmacro RANGED_PRELOAD_TYPE("Item")
//! runtextmacro RANGED_PRELOAD_TYPE("Ability")
endif
/*========================================================================================================*/
private module Init
private static method onInit takes nothing returns nothing
local rect world = GetWorldBounds()
static if LIBRARY_Table then
set tb = TableArray[5]
endif
set dummy = CreateUnit(PRELOAD_UNIT_OWNER, PRELOAD_UNIT_TYPE_ID, 0, 0, 0)
call SetUnitY(dummy, GetRectMaxY(world) + PRELOAD_UNIT_Y_BOUNDS_EXTENSION)
call UnitAddAbility(dummy, 'AInv')
call UnitAddAbility(dummy, 'Avul')
call UnitRemoveAbility(dummy, 'Amov')
call RemoveRect(world)
set world = null
endmethod
endmodule
private struct S extends array
static if LIBRARY_Table then
static TableArray tb
else
static hashtable tb = InitHashtable()
endif
static unit dummy
implement Init
endstruct
endlibrary
Sample Usage
JASS:
scope Test initializer onInit
private function OnInit takes nothing returns nothing
call FogEnable(false)
call FogMaskEnable(false)
call PanCameraToTimed(0, 0, 0)
call PreloadUnit('hpea')
call PreloadUnit('hfoo')
call PreloadUnit('hrif')
call PreloadUnit('ogru')
call PreloadUnit('hfoo')// If debug mode is on, this will display "Operation Cancelled: Entered unit data was already preloaded"
call PreloadEffect("Abilities\\Weapons\\RocketMissile\\RocketMissile.mdl")
call PreloadEffect("Abilities\\Spells\\Human\\Heal\\HealTarget.mdl")
call PreloadEffect("Abilities\\Weapons\\RocketMissile\\RocketMissile.mdl")// If debug mode is on, this will display "Operation Cancelled: Entered effect data was already preloaded"
endfunction
endscope
v1.5.0
- Fixed a bug that can cause game crash in patch 1.31.1.
- Moved the important constant variables on top
- Preloading special effects now also no longer plays a sound
v1.4e
- Fixed a bug regarding the preload dummy not being hidden beyond map boundaries
v1.4d
- Changed
v1.4c
- Made BJObjectId an optional requirement
- Removed the debug messages
v1.4b
- Fixed a bug in the Ranged preloading in which the condition start == end won't be met and therefore the loop will not exit thus resulting to hitting the op limit (important update)
- Reduced function DoAbilityPreload into 1 line
- Uses GetWorldBounds() instead of bj_mapInitialPlayableArea as the distance basis of the dummy unit's placement
- Improved function DoUnitPreload
v1.4
- Made Table optional
- Added UnitRecycler as an optional requirement
- Upon calling PreloadUnit(), if the unit is not a hero and UnitRecycler is found, the unit will be added to the unit stock instead. Otherwise, it goes with normal preloading.
- Dummy unit's movement is disabled to prevent possible game crash.
- Other changes
v1.3
- You can now preload at any time during the game instead of only during the map initialization
- Significantly optimized the code
- Removed the unnecessary custom function for checking preload duplicates
- Added Table to the library requirements
- Other changes
v1.2
- Preloading does not anymore happen in a single phase at the GUI Map Initialization
- Resources are now preloaded at the instant you call the preload function
v1.1
- Replaced the struct API
- Added sounds to the preloadable types and removed destructables
- Added the functionality for ranged preloading
- Some code restructuring
v1.0
- First Release
- Fixed a bug that can cause game crash in patch 1.31.1.
- Moved the important constant variables on top
- Preloading special effects now also no longer plays a sound
v1.4e
- Fixed a bug regarding the preload dummy not being hidden beyond map boundaries
v1.4d
- Changed
Player(15)
to Player(PLAYER_NEUTRAL_PASSIVE)
in order to be compatible with newer patchesv1.4c
- Made BJObjectId an optional requirement
- Removed the debug messages
v1.4b
- Fixed a bug in the Ranged preloading in which the condition start == end won't be met and therefore the loop will not exit thus resulting to hitting the op limit (important update)
- Reduced function DoAbilityPreload into 1 line
- Uses GetWorldBounds() instead of bj_mapInitialPlayableArea as the distance basis of the dummy unit's placement
- Improved function DoUnitPreload
v1.4
- Made Table optional
- Added UnitRecycler as an optional requirement
- Upon calling PreloadUnit(), if the unit is not a hero and UnitRecycler is found, the unit will be added to the unit stock instead. Otherwise, it goes with normal preloading.
- Dummy unit's movement is disabled to prevent possible game crash.
- Other changes
v1.3
- You can now preload at any time during the game instead of only during the map initialization
- Significantly optimized the code
- Removed the unnecessary custom function for checking preload duplicates
- Added Table to the library requirements
- Other changes
v1.2
- Preloading does not anymore happen in a single phase at the GUI Map Initialization
- Resources are now preloaded at the instant you call the preload function
v1.1
- Replaced the struct API
Preload.$TYPE$()
to Preload$TYPE$()
because using a reserved word as the struct name can be buggy- Added sounds to the preloadable types and removed destructables
- Added the functionality for ranged preloading
- Some code restructuring
v1.0
- First Release
Last edited: