• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

vJass Indexer Tutorial - Structs

Level 18
Joined
Oct 18, 2007
Messages
930
You Need:
  1. The newest version of JNGP
  2. Common vJass and Jass experience

Readme
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, :p from norway >.<
Example
The struct
JASS:
struct SomeStruct
    // [Your Strut Members]
    // 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
                [Your actions on end]

                // 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:
Top