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

Whats hrong here?

Status
Not open for further replies.
Level 15
Joined
Nov 30, 2007
Messages
1,202
Noticed I forgot to actually create the trigger. GG. Another question though, if i only want to use integer array stackTime and real array stackDmg and not actually Stack array. How do I change it without having to define the array size inside the struct?

JASS:
library FireSystem 
    
    globals
        Stack array stack
        private constant integer FIRE_DMG = 'A000'
    endglobals
    
    struct Fire
    
    endstruct 
    
    struct Stack 
        private integer stackTime 
        private real stackDmg
        static group stackGroup = CreateGroup()
        static integer count 
        
        static method onAttackFilter takes unit u returns boolean
           return IsUnitType(u, UNIT_TYPE_STRUCTURE) and GetUnitAbilityLevel(GetAttacker(), FIRE_DMG) > 0
        endmethod
        
        static method onAttack takes nothing returns boolean
            local unit attacked = GetTriggerUnit()
            if Stack.onAttackFilter(attacked) then
                call BJDebugMsg(GetUnitName(attacked))
            endif
            return false 
        endmethod
        
        static method onInit takes nothing returns nothing
            local trigger t = CreateTrigger()
            local integer i = 0
            loop
                call TriggerRegisterPlayerUnitEvent(t, Player(i), EVENT_PLAYER_UNIT_ATTACKED, null)
                set i = i + 1
                exitwhen i == bj_MAX_PLAYER_SLOTS
            endloop
            call TriggerAddCondition(t, Condition(function Stack.onAttack))
        endmethod
    endstruct
endlibrary

Another similar question: How do i put the variables from the Target struct with the prefix "stack" inside the Stack struct but refer to them through target?

JASS:
library FireSystem 
    
    globals
        Target array target
        private constant integer FIRE_DMG           = 'A000'
        private constant integer STACK_DURATION      = 10
    endglobals
    
    struct Stack 
        static group gr
        static integer count
    endstruct 
    
    struct Target 
        unit u
        real stackDmg // Can I put these inside my Stack struct and how would I find them through target[i]? 
        real stackTime
        
        private static method onAttackFilter takes unit u returns boolean
           return IsUnitType(u, UNIT_TYPE_STRUCTURE) and GetUnitAbilityLevel(GetAttacker(), FIRE_DMG) > 0 
        endmethod
        
        private static method onAttack takes nothing returns boolean
            local unit attacked = GetTriggerUnit()
            local integer i = GetUnitUserData(attacked)
            if Target.onAttackFilter(attacked) then
                
                if target[i] == null then
                    set target[i] = Target.create()
                    set target[i].u = attacked
                endif
                
            endif
            set attacked = null
            return false 
        endmethod
        
        private static method create takes nothing returns Target 
            return .allocate()
        endmethod
        
        static method onInit takes nothing returns nothing
            local trigger t = CreateTrigger()
            local integer i = 0
            loop
                call TriggerRegisterPlayerUnitEvent(t, Player(i), EVENT_PLAYER_UNIT_ATTACKED, null)
                set i = i + 1
                exitwhen i == bj_MAX_PLAYER_SLOTS
            endloop
            call TriggerAddCondition(t, Condition(function Target.onAttack))
        endmethod
    endstruct 
    
    
    
    
endlibrary
 
Last edited:

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,198
Another question though, if i only want to use integer array stackTime and real array stackDmg and not actually Stack array. How do I change it without having to define the array size inside the struct?
Does not really make sense.

All structs (as far as I know) are implemented in the form of parallel arrays (a separate array for each member). When you allocate a struct it returns you a "free" index from those arrays. Deallocation frees that index for reallocation.

When you create a "struct type" array then actually you are creating an array for struct references (the index used by a struct instance) and not actual struct data. All struct data is stored in the struct's parallel arrays and all method calls (non-static) take a struct "reference" (integer) as an parameter.

This is very different from C++ classes/structs which keep all member variables together as a block of memory. An array of classes/structs then becomes an array of the appropriate blocks of memory.

Methods then use a reference to the block of memory to get the member variables as they are at known offsets inside that block. JASS has no such "memory block with offset" capability so separate variables are used for each member variable (as they act as offsets). Since variable references in JASS are not supported, all variable names have to be hard-wired into the function definitions. By using arrays to back the member variables with a index parameter it allows for one function to work on multiple instances of a struct as well as allow the support to have dynamic creation of struct instances etc. In theory one should be able to hard-code "singleton" structs such that they are non-array backed and have hard-coded functions that only work on that instance however I am not sure vJASS supports this.

For logical reasons arrays inside structs are done by allocating a block of indicies from a backing array which shortens the maximum struct instance count. Additionally I have seen in the past that by making a struct extend "array" you can then treat it as an array of the struct as opposed to created/destroyed instances (useful for data storage of related data).
 
Status
Not open for further replies.
Top