# vJass Indexer Tutorial - Structs

#### Dynasti

Level 18
You Need:
1. The newest version of JNGP
2. Common vJass and Jass experience

This is a simple example of how you can use an indexer system. You can do alot more of the one thing i show you here.

If you find things that i need to add then please tell

And sorry for my english, from norway >.<
Example
The struct
JASS:
``````struct SomeStruct
// Just a random struct member used for an example
integer x = 0

// You need 2 static members( You can use 2 globals, but I find using static members a lot easier ):
static SomeStruct array Index // The array that stores the struct
static integer Total // The array that keeps track of how many structs there are in use

// Lets say we doing some stuff for a counting sys, we use this method
static method Loop takes nothing returns nothing
// Both of theese locals are needed for this loop
local SomeStruct dat
local integer i = 0

loop // Loop for the struct
exitwhen i >= dat.Total // The loop will end when all structs have passed the loop func
set dat = dat.Index[i] // Setting the local struct dat to the stored struct in the static metmber Index

// Lets say that if i < 10 it will do stuff
if dat.x < 10 then
set dat.x = dat.x + 1
else

// Just some random call, you dont need this
call BJDebugMsg("10 reached!")

// destroying the struct
call dat.destroy()

// Recycling the indexer system
set dat.Total = dat.Total - 1
set dat.Index[i] = dat.Index[dat.Total]

// If you dont set the i to i - 1 when destroying a struct the next struct will not be looped, so it is very important
set i = i - 1
endif

set i = i + 1
endloop

// This could be useful for looping functions
// If number of structs is equal to 0 then the timer will stop.
if dat.Total == 0 then
call PauseTimer(SomeTimer)
endif
endmethod
endstruct``````

For adding a struct to the loop you need to allocate a struct then add it to the indexer

Example
JASS:
``````static method Start takes nothing returns nothing
local SomeStruct dat = SomeStruct.allocate()

[Do Something]

// If you are doing a timer loop then this could be useful
// If the total amount of structs looping is equal to 0 a Timer will start
// Note: This have to be done before adding the struct to the indexer
if dat.Total == 0 then
call TimerStart(SomeTimer, 0.1, true, function SomeStruct.Loop)
endif

// Adding the struct to the indexer system
set dat.Index[dat.Total] = dat
// Setting the total amount of structs in loop to Total + 1
set dat.Total = dat.Total + 1
endmethod``````

How an indexer works
JASS:
``````  1   2   3   4   5   // initial arrangement
-   2   3   4   5   // struct 1 ends
5   2   3   4   -   // move struct 5 to struct 1's place and decrease the total number of structs running
5   -   3   4   -   // struct 2 ends
5   4   3   -   -   // repeat
5   4   -   -   -   // struct 3 ends
4   -   -   -   -   // struct 5 ends
-   -   -   -   -   // struct 4 ends, no structs running and timer stops``````

Last edited:

#### RolePlaynGamer

Level 9
In time someone made this tutorial.

There's a few spelling errors in the code, and you spelled static wrong in the Total member.
Besides that, your reputation just increased, =P

#### Dynasti

Level 18
In time someone made this tutorial.

There's a few spelling errors in the code, and you spelled static wrong in the Total member.
Besides that, your reputation just increased, =P

Osh--

And i did this by freehand into the web so dont complain!
( My errors are just some misspelling but that will show up in syntax so no worry ^^ )

#### Dynasti

Level 18
Thread title fixed. Thx Pyritie

[JASS] Slide Spell
Replies
1
Views
375
Replies
8
Views
413
Replies
8
Views
1K
Replies
6
Views
721
Replies
19
Views
658