• 🏆 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!

Need feedback for my system

Status
Not open for further replies.
Level 9
Joined
Aug 2, 2008
Messages
219
As far as i know yousing struct indexing is one of the most efficient ways to deal with a bunch of data and it is often used in a combination with a timer to do those things periodically. I found my self often in the situation to do this struct indexing and i thought it might be useful to create a system which does periodicly struct indexing automatically.

It would be nice if someone can tell me if that system is complete crap and the whole idea is bad or if this could be useful even to other users (ofcourse suggestions for improvement are welcomed as well). So here take a look at it:
JASS:
library TNTI

    private interface Ext
    
        static integer Total   = 0
        static integer Current = 0
        static constant real Intervall = .01
        static timer TIM = null
        
        real runtime
        
        method onBreak    takes nothing returns boolean defaults true
        method onLoop     takes nothing returns nothing defaults nothing
        
    endinterface
        
    struct Indexable extends Ext
    
        private static Ext array Index
        private real timing = .0
        
        private method onDestroy takes nothing returns nothing
        
            set Ext.Total = Ext.Total -1
            set thistype.Index[Ext.Current] = thistype.Index[Ext.Total]
            set Ext.Current = Ext.Current-1
                        
            if Ext.Total == 0 then
                call PauseTimer(Ext.TIM)
            endif
                        
        endmethod
    
        private static method Loop takes nothing returns nothing
        
            local Ext this
            set Ext.Current = 0
            
            loop
                exitwhen Ext.Current == Ext.Total
                set this = thistype.Index[Ext.Current]
                                
                if this.onBreak() then
                    call this.destroy()
                else
                    set this.timing = this.timing - thistype.Intervall
                    if this.timing <= .0 then
                        call this.onLoop()
                        set this.timing = this.runtime
                    endif
                endif
                
                set Ext.Current = Ext.Current+1
            endloop
                        
        endmethod
        
        static method create takes nothing returns thistype
        
            local Ext this = thistype.allocate()
            set thistype.Index[Ext.Total] = this
            set Ext.Total = Ext.Total +1
                        
            if Ext.Total == 1 then
                call TimerStart(Ext.TIM,Ext.Intervall,true, function thistype.Loop)
            endif
            
            return this
            
        endmethod
            
        private static method onInit takes nothing returns nothing
            set Ext.TIM = CreateTimer()
        endmethod
        
    endstruct
    
endlibrary
How to use it
As i said this will automatically do a periodic struct indexing for, inclusive recycling.
  1. Declare a struct which extends the Indexable struct from the system.
  2. Add any members and methods to your struct as you want.
  3. Add a normal method with the signature method onLoop takes nothing returns nothing.
    In that method you can place the code which will deal with the data of your struct. This method will be called periodically for every instance of your struct.
  4. Add another normal method with the signature method onBreak takes nothing returns boolean. In the onBreak method you can place a bool expression which is responsible for removing one of the instances from the cycle. This method will always be executed before onLoop is called (similar to a condition of a trigger). If it returns true the instance will be removed from the cycle, if it returns false onLoop() will be executed.
  5. The onDestroy method will be executed when an instance is removed fromt the cycle, of course you can place your clean up code there as normal, but mind the option to add code which should be done when your instance expires.
  6. By adding a member of type real with the named runtime to your struct you can change the execution intervall for the onLoop() method. Leaving it out will execute onLoop in the default intervall which is 0.01.
[!]Leaving out onLoop will simply do nothing to your instances
[!]Leaving out onBreak will never start the cycle for your instances
[!]When you create/allocate a struct which extends Indexable will immedeatly add the instance to the cycle
JASS:
struct sampel extends Indexable
    
    integer val = 0
    real runtime = 1.
    
    method onDestroy takes nothing returns nothing
        call BJDebugMsg("We counted from zero to 10"+I2S(this))
    endmethod
    
    method onBreak takes nothing returns boolean
        return this.val > 10
    endmethod
    
    method onLoop takes nothing returns nothing
        set this.val = .val +1
    endmethod

endstruct
JASS:
struct TimedEffect extends Indexable

    real runtime = 0.2
    real duration
    effect sfx
    
    method onDestroy takes nothing returns nothing
        call DestroyEffect(this.sfx)
        set this.sfx = null
    endmethod
    
    method onBreak takes nothing returns boolean
        return this.duration <= 0.
    endmethod
    
    method onLoop takes nothing returns nothing
        set this.duration = this.duration - this.runtime
    endmethod
    
    static method create takes effect fx, real dur returns thistype
    
        local thistype this = thistype.allocate()
        
        set this.sfx = fx
        set this.duration = dur
        
        return this
        
    endmethod
    
endstruct
 
Status
Not open for further replies.
Top