HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

How to get stuff compiled before structs?

03-24-2008, 04:30 PM#1
Patpup
I'm just a Jass-behinner who resently stumbled over vJass and thought, structures could make his code less ugly and his life a little easier...

I want to use some custom functions in the create and onDestroy funcs of a struct, but it wont let me, because structs seem to be compiled in front of everything else. It works if I put struct and extra-functions into the same library. This is, however, no nice solution as it makes the code hard to read... (these custom functions are used in at least 2 more places). Is there a better way to do this?

Btw, the structure is for projectiles and the functions are a noobish copy of iNfraNe's "Linked Lists"...

Thank you!
03-24-2008, 04:39 PM#2
Troll-Brain
you can also decide yourself how you manage the inits with this :

Extract of the jasshelper manual :

Code:
//! inject main
   //some function calls may go here

   // this places vjass initializations there, notice structs are first initialized then library initializers
   // are called
   //! dovjassinit


   //other calls may go here

   call InitCustomTriggers() //maybe you want to exploit that world editor function...
//! endinject

It's for write the main function, so all functions are above this one
03-24-2008, 04:46 PM#3
Ammorth
Create a linked-list in your struct.

Collapse JASS:
globals
    projectile first = 0
endglobals

struct projectile
    real x
    real y
    real z
    real size

    projectile next
    projectile prev
   
    static method create takes whatever returns projectile
        local projectile p = projectile.allocate()
        // do whatever
        // For simplicity, we will always make it add to the front
        if first == 0 then // make it the first in the list
            set first = p
        else // there is a projectile in front, so setup the links and push this infront
            set p.prev = first // make this one link to the first
            set first.next = p // make the first one link to this
            set first = p // make this one the new first
        endif
    endmethod

    method onDestroy takes nothing returns nothing
        //remove links
        if this == first then // its the first one
            if this.prev == 0 then // no projectile after
                set first = 0
            else // there is a projectile after, so remove the first one and make the next one the first one
                set this.prev = first
                set first.next = 0 // remove link to this projectile
                set this.prev = 0 // remove link to new first
            endif
        else // the projectile is somewhere in the middle
            if this.prev == 0 then // last one on the list
                set this.next.prev = 0 // break the link from the one in front to this one
                set this.next = 0 // break the link from this to the one in front
            else // is in the middle
                set this.next.prev = this.prev // link the next one to the one behind
                set this.prev.next = this.next // link the prev one to the one in front
                set this.prev = 0 // clean-up the links
                set this.next = 0
            endif
        endif
    endmethod 

That should be the main idea. The code should work correctly.
03-24-2008, 05:15 PM#4
Patpup
@Troll-Brain:
Sorry, but I don't get it... Should functions declared in front of "//! inject main" be compiled before the stuctures? I don't think so, 'cause it wont work.

@Ammorth:
I'm afraid I might not understand what you want to tell me eighter... *sweat*
If I got your code right, it adds all the instances of the struct to a linked list??! I might be wrong, but thats not what I'm trying to do. I need the LL as an array inside the struct (because you can't have arrays inside the struct).
03-24-2008, 05:18 PM#5
Ammorth
You can have arrays but only of fixed size.

Collapse JASS:
struct magic
    integer array fun[100]
endstruct

a struct magic with 100 integers of fun!
03-24-2008, 05:22 PM#6
Patpup
Hm, didn't know that... thanks.
Still having fixed values is not that nice and I'd prerfer the LL... ^^^