• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[vJASS] List

Status
Not open for further replies.
Level 19
Joined
Mar 18, 2012
Messages
1,716
Quick question: Is this basically correct? Since today, I was always using a table array -> static table array stack, when I had to iterate within one struct instance.
This comes along with some problems, particularly if one tries to iterate backwards and some members have already been removed.

I've read the API of List and the module List, I'm aware how push(), pop(), enqueue() , etc works.
But just as I said it's the first time I'm using it.

Edit: Deleted this piece of code, because it was total crap.
 
Last edited:
Try using the Shared version of the stuff and running it in debug mode. Use the Shared version until you get 0 errors in debug mode. You'll see why ^)^. Run your script with that Shared version in debug mode and you should get an error. It enforces correct usage ;P.

You have syntax errors too

edit
local thistype node = this.list.enqueue()
set node.i = 2
set node.test = CreateUnit(...)

if you used unique, then it'd be

call this.list.enqueue(attach.create())
 
Level 19
Joined
Mar 18, 2012
Messages
1,716
You have syntax errors too
Yeah, I never saved it. :(

Ok, I'll experiment with SharedList. Thank you.

edit:
So far it works, if i use one list for all instances of my main struct, named "test" in the example above.
But finally I need one list per instance of "test" and I want to couple it with CTL. Creating a unit is not a random example. In the end each node of one list should point to one unit. In practice I want to update the Meathook resource I've once created and I though a linked list might be handy.

I'll try to get it working tomorrow, if not I'll update this thread.
Also there is not much information on THW about how to use a linked list or I haven't found it, yet.
 
Last edited:
Level 19
Joined
Mar 18, 2012
Messages
1,716
I've come so far for today.
JASS:
scope testing
    
    globals
        private constant timer T = CreateTimer()
    endglobals
    
struct test extends array
    implement SharedList
    static thistype link
    static integer j = 0
    integer i
    private static method onLoop takes nothing returns nothing
        local thistype this = link.first
        local thistype node
        
        loop
            exitwhen this == link.sentinel
            set node = this.next
            call BJDebugMsg(I2S(this.i))
            set this = node
        endloop
    
        //if (link.first == sentinel) then
         //   call PauseTimer(T)
       // endif
    endmethod
    
    private static method run takes nothing returns boolean
        local thistype this = link.enqueue()
        set this.i = j
        set j = j + 1
        if (this == link.first) then 
            call TimerStart(T, 0.031250000, true, function thistype.onLoop)
        endif
        return false
    endmethod
        
    private static method onInit takes nothing returns nothing
        local trigger t = CreateTrigger()
        call TriggerRegisterPlayerEventEndCinematic(t, Player(0))
        call TriggerAddCondition(t, Condition(function thistype.run))
        set t = null
        set link = thistype.create()
    endmethod
endstruct
endscope
 
Level 19
Joined
Mar 18, 2012
Messages
1,716
After experimenting with SharedList I finally got it to work the way I wanted it. Though at the moment it looks very dirty, at least I thinks so.

Some help is definitly required to optimize/shorten what I've posted here. :/

Also at first I want to iterate from first to last(sentinel) and use push() if my conditions match

and afterwards backwards from last to first(sentinel) and use pop() within the loop.

Do I need extra protection for l.next / l.prev so the loop doesn't fail when adding or removing elements?

JASS:
scope testing

    private struct List extends array
        implement SharedList
        unit u
        
        method attach takes nothing returns nothing
            set this.u = CreateUnit(Player(0), 'hfoo', 0,0,0)
        endmethod
    endstruct

    private struct main extends array
        List list
        boolean forwards
        
        implement CTL
            local thistype node 
            local List l
        implement CTLExpire
        //if .forwards then <<-- from first to sentinel
            //if .... then
            set node     = .list.push()
            set l        = node
            call l.attach()
            //endif
            set l        = .list
            set l = l.first
            loop
                exitwhen l == l.sentinel
                //Code
                set l = l.next
            endloop
        //else <<-- Iterate from last to sentinel
        // ....
        //endif
        implement CTLEnd
    
        private static method run takes nothing returns boolean
            local thistype this = create()
            local thistype node
            local List l
            //set .forwards      = true
            set .list           = List.create()
            set node            = .list.enqueue()
            set l               = node
            call l.attach()
            return false
        endmethod
    
        private static method onInit takes nothing returns nothing
            local trigger t = CreateTrigger()
            call TriggerRegisterPlayerEventEndCinematic(t, Player(0))
            call TriggerAddCondition(t, Condition(function thistype.run))
            set t = null
        endmethod
    endstruct
endscope

Edit @Nes: You said SharedList is for getting started with collections. Which one should I use in the end, when everything works flawless? In case push() or enqueue() can be used for adding the first element of the list I do not need enqueue() at all, only push() and pop().
 
Last edited:
Status
Not open for further replies.
Top