- Joined
- Feb 4, 2009
- Messages
- 1,314
Trackables
I've seen many people who belive that one can't use trackables in GUI so here is a tutorial which shows how it is done.
What are trackables?
Trackables are models which can be placed on the map and are used to detect click- or mouse-hover-events. They can't be moved or destroyed. The function to create them is the following:
native CreateTrackable takes string trackableModelPath, real x, real y, real facing returns trackable
How to handle them
We start with creating a local variable for our trackable. This is necessary because we can't create global variables of type trackable with the variable editor.
-
Custom script: local trackable tr
s: the path of the model (string)
x: the x-position (real)
y: the y-position (real)
z: the z-position (real)
a: the facing angle (real)
d: a destructable
p: a player
i/i2/i3: integers
Track_Table: a hashtable
To use global variables for custom script code you will have to add an "udg_" to them because warcraft renames them internally. So "x" becomes "udg_x" and "Set x = x + 1.00" becomes "set udg_x = udg_x + 1.00".
Next we create the hashtable to hold all the data about our trackables.
-
Hashtable - Create a hashtable
-
Set Track_Table = (Last created hashtable)
-
Set x = 100.00
-
Set y = 200.00
-
Set z = 300.00
-
Custom script: set udg_d = CreateDestructableZ( 'OTip', udg_x, udg_y, udg_z, 0.00, 1, 0 )
-
Custom script: set tr = CreateTrackable(udg_s, udg_x, udg_y, udg_a)
The first problem is easy to solve. We save all data of the trackable at creation and since it can't be destroyed or moved the data won't change. We will save the data in a hashtable and use the handle id of the trackable to find it again later.
-
Custom script: set udg_i = GetHandleId(tr)
-
Hashtable - Save x as 0 of i in Track_Table
-
Hashtable - Save y as 1 of i in Track_Table
-
Hashtable - Save z as 2 of i in Track_Table
-
Hashtable - Save s as 3 of i in Track_Table
-
Hashtable - Save Handle Ofp as 4 of i in Track_Table
-
Set s = units\human\Footman\Footman.mdl
-
Custom script: if udg_p != GetLocalPlayer() then
-
Set s = <Empty String>
-
Custom script: endif
-
Custom script: call TriggerRegisterTrackableHitEvent(gg_trg_Hit, tr)
-
Custom script: call TriggerRegisterTrackableTrackEvent(gg_trg_Track, tr)
-
Ini
-
Events
-
Map initialization
-
-
Conditions
-
Actions
-
-------- Create a local variable for our trackable --------
-
Custom script: local trackable tr
-
-------- And one for a destructable which will be used to raise our trackable in the air --------
-
-------- Create a hashtable to save our trackables' data --------
-
Hashtable - Create a hashtable
-
Set Track_Table = (Last created hashtable)
-
-------- Facing angle of the trackables --------
-
Set a = 270.00
-
For each (Integer i2) from 1 to 50, do (Actions)
-
Loop - Actions
-
-------- X/Y/Z-position of our next trackable --------
-
-------- We will create a spiral of them just for fun --------
-
Set x = ((Cos((20.00 x (Real(i2))))) x 150.00)
-
Set y = ((Sin((20.00 x (Real(i2))))) x 150.00)
-
Set z = ((Real(i2)) x 15.00)
-
-------- Create 12 of them --------
-
-------- 1 for every player so it is MPI --------
-
-------- Usually you don't know which player clicked on it so we use this workaround --------
-
For each (Integer i3) from 1 to 12, do (Actions)
-
Loop - Actions
-
Set p = (Player(i3))
-
Set s = units\human\Footman\Footman.mdl
-
-------- Make model visible for 1 player only --------
-
Custom script: if udg_p != GetLocalPlayer() then
-
Set s = <Empty String>
-
Custom script: endif
-
-------- Create an invisible platform with z-height to put our trackable on --------
-
Custom script: set udg_d = CreateDestructableZ( 'OTip', udg_x, udg_y, udg_z, 0.00, 1, 0 )
-
-------- Create trackable --------
-
Custom script: set tr = CreateTrackable(udg_s, udg_x, udg_y, udg_a)
-
-------- Remove invisible platform --------
-
Custom script: call RemoveDestructable(udg_d)
-
-------- Register Click event --------
-
Custom script: call TriggerRegisterTrackableHitEvent(gg_trg_Hit, tr)
-
-------- Register Move event --------
-
Custom script: call TriggerRegisterTrackableTrackEvent(gg_trg_Track, tr)
-
-------- Get handle id of our trackable --------
-
Custom script: set udg_i = GetHandleId(tr)
-
-------- Save all data related to our trackable --------
-
Hashtable - Save x as 0 of i in Track_Table
-
Hashtable - Save y as 1 of i in Track_Table
-
Hashtable - Save z as 2 of i in Track_Table
-
Hashtable - Save s as 3 of i in Track_Table
-
Hashtable - Save Handle Ofp as 4 of i in Track_Table
-
-
-
-
-
-------- Null handle leaks --------
-
Custom script: set tr = null
-
-
Again we start with a local variable and save the triggering trackable in it.
-
Custom script: local trackable tr = GetTriggeringTrackable()
-
Custom script: set udg_i = GetHandleId(tr)
-
Set x = (Load 0 of i from Track_Table)
-
Set y = (Load 1 of i from Track_Table)
-
Set z = (Load 2 of i from Track_Table)
-
Set s = (Load 3 of i from Track_Table)
-
Set p = (Load 4 of i in Track_Table)
-
Hit
-
Events
-
Conditions
-
Actions
-
-------- Get trackable --------
-
Custom script: local trackable tr = GetTriggeringTrackable()
-
-------- Get its handle it --------
-
Custom script: set udg_i = GetHandleId(tr)
-
-------- Load data associated with it --------
-
Set x = (Load 0 of i from Track_Table)
-
Set y = (Load 1 of i from Track_Table)
-
Set z = (Load 2 of i from Track_Table)
-
Set s = (Load 3 of i from Track_Table)
-
Set p = (Load 4 of i in Track_Table)
-
-------- Clear text messages --------
-
Cinematic - Clear the screen of text messages for (All players)
-
-------- Display data --------
-
Game - Display to (All players) the text: Event type: Hit
-
Game - Display to (All players) the text: (X: + (String(x)))
-
Game - Display to (All players) the text: (Y: + (String(y)))
-
Game - Display to (All players) the text: (Z: + (String(z)))
-
Game - Display to (All players) the text: (Model path: + s)
-
Game - Display to (All players) the text: (Triggering player: + (Name of p))
-
Custom script: set tr = null
-
-
Attachments
Last edited by a moderator: