Starting jass

Discussion in 'JASS Zone' started by rexpim, Nov 11, 2010.

  1. rexpim

    rexpim Member

    Ratings:
    +8 / 0 / -0
    I'm moving from gui to jass this is my first spell, so i wanna know if i got mistakes in this code

    JASS:
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    //
    //              -Made By REX  
    //
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    //===========================================================================
    
    scope NatureStrike
    
    globals
        private constant integer NATURESTRIKE = 'A01U'   // Nature Strike RAW CODE
        private constant integer TREE = 'BTtw'               // Tree RAW CODE
        private constant integer VAR = 2                     // Tree variation
        private constant real SCALE = 2.00                 // Tree scale size
        private constant real LIFE = 20.00                 // Tree life time
    endglobals
    
    //===========================================================================
    //===========================================================================
    
    private function Trig_NatureStrike_Conditions takes nothing returns boolean
        return GetSpellAbilityId() == NATURESTRIKE       
    endfunction
    
    function Trig_NatureStrike_Actions takes nothing returns nothing
        local destructable FSTREE
        local location FORCETARGET = GetSpellTargetLoc()
        call CreateDestructableLoc( TREE, FORCETARGET, GetRandomDirectionDeg(), SCALE, VAR )
        set FSTREE = GetLastCreatedDestructable()
        call TriggerSleepAction( LIFE )
        call RemoveDestructable( FSTREE )
    endfunction
    
    //===========================================================================
    
    function InitTrig_NatureStrike takes nothing returns nothing
        set gg_trg_NatureStrike = CreateTrigger(  )
        call TriggerRegisterAnyUnitEventBJ( gg_trg_NatureStrike, EVENT_PLAYER_UNIT_SPELL_EFFECT )
        call TriggerAddCondition( gg_trg_NatureStrike, Condition( function Trig_NatureStrike_Conditions ) )
        call TriggerAddAction( gg_trg_NatureStrike, function Trig_NatureStrike_Actions )
    endfunction
    
    endscope
    
    //===========================================================================
    
    //===========================================================================


    I'm using sleep action, can any1 tell me another action to get the life of the tree
     
  2. luorax

    luorax Invasion in Duskwood

    Ratings:
    +67 / 0 / -0
    1, Don't use BJs. BJs use natives, so you can do everything, what they do calling the right natives directly. Also, "CreateDestructableLoc" returns a destructible, so you can use this:

    JASS:
        local location FORCETARGET = GetSpellTargetLoc()
        local destructable FSTREE = CreateDestructableLoc( TREE, FORCETARGET, GetRandomDirectionDeg(), SCALE, VAR )


    2, Try to avoid using locations. Using real coordinates is more powerful, and faster than using locations.

    3, Remove leaks! Don't forget to null handle variables (like timers, units, destructables and so on)

    4, It's just a suggestion: naming. I suggest you to use variable names like this:

    GLOBAL_CONSTANT (Example: TICK_DMG)
    Global_Dynamic (Example: Current_Target)
    lOcal (Example: local real xPos)

    Or use some "theme". It makes the things easier.

    JASS:
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    //
    //              -Made By REX  
    //
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    //===========================================================================
    
    scope NatureStrike
    
    globals
        private constant integer NATURESTRIKE = 'A01U'       // Nature Strike RAW CODE
        private constant integer TREE = 'BTtw'              // Tree RAW CODE
        private constant integer VAR = 2                    // Tree variation
        private constant real SCALE = 2.                 // Tree scale size
        private constant real LIFE = 20.                 // Tree life time
    endglobals
    
    //===========================================================================
    //===========================================================================
    
    private function Trig_NatureStrike_Conditions takes nothing returns boolean
        return GetSpellAbilityId() == NATURESTRIKE       
    endfunction
    
    function Trig_NatureStrike_Actions takes nothing returns nothing
        local destructable dTree = CreateDestructable(TREE, GetSpellTargetX(), GetSpellTargetY(), GetRandomReal(0, 360), SCALE, VAR) //We don't need BJ's, and Locations!
    
        call TriggerSleepAction( LIFE )
        call RemoveDestructable( dTree)
    
        //Cleaning leaks
        set dTree = null
    endfunction
    
    //===========================================================================
    
    function InitTrig_NatureStrike takes nothing returns nothing
        set gg_trg_NatureStrike = CreateTrigger(  )
        call TriggerRegisterAnyUnitEventBJ( gg_trg_NatureStrike, EVENT_PLAYER_UNIT_SPELL_EFFECT )
        call TriggerAddCondition( gg_trg_NatureStrike, Condition( function Trig_NatureStrike_Conditions ) )
        call TriggerAddAction( gg_trg_NatureStrike, function Trig_NatureStrike_Actions )
    endfunction
    
    endscope
    
    //===========================================================================
    
    //===========================================================================
     
  3. rexpim

    rexpim Member

    Ratings:
    +8 / 0 / -0
  4. luorax

    luorax Invasion in Duskwood

    Ratings:
    +67 / 0 / -0
    Too much wait can cause lag, but if you don't spamm the ability, it won't cause too much problem. Also, using a timer to destroy the destructible should be a better way. Using Vexorian's TimerUtils you can easily pass the required data to a timer, using a struct.
    Or another useful way is a periodic timer, what reduces every "Tree"'s life by a previously defined ammount every x.y seconds.
     
  5. Zeth

    Zeth Member

    Ratings:
    +3 / 0 / -0
    JASS:
    scope NatureStrike
    
    globals
        private constant integer NATURESTRIKE = 'A01U'   // Nature Strike RAW CODE
        private constant integer TREE = 'BTtw'               // Tree RAW CODE
        private constant integer VAR = 2                     // Tree variation
        private constant real SCALE = 2.00                 // Tree scale size
        private constant real LIFE = 20.00                 // Tree life time
    endglobals
    
    //===========================================================================
    //===========================================================================
    
    private function Trig_NatureStrike_Conditions takes nothing returns boolean
        return GetSpellAbilityId() == NATURESTRIKE       
    endfunction
    
    private struct data
    
        private timer t
        private destructable FSTREE
    
    static method kill takes nothing returns nothing
        local thistype this=GetTimerData(GetExpiredTimer())
        call RemoveDestructable(.FSTREE)
        call ReleaseTimer(GetExpiredTimer())
        set .FSTREE=null
        set .t=null
    endmethod
    
    static method create takes nothing returns thistype
        local thistype this=.allocate()
        set .t=NewTimer()
        set .FSTREE = CreateDestructable(TREE,GetSpellTargetX(),GetSpellTargetY(),GetRandomReal(1.,360.),SCALE,VAR) // use of coordinates instead of locations
        call SetTimerData(.t,this)
        call TimerStart(t,LIFE,false,function thistype.kill)
        return this
    endmethod
    
    endstruct
    
    private function Trig_NatureStrike_Actions takes nothing returns nothing
        local data t=data.create()
    endfunction
    
    //===========================================================================
    
    private function InitTrig_NatureStrike takes nothing returns nothing
        set gg_trg_NatureStrike = CreateTrigger(  )
        call TriggerRegisterAnyUnitEventBJ( gg_trg_NatureStrike, EVENT_PLAYER_UNIT_SPELL_EFFECT )
        call TriggerAddCondition( gg_trg_NatureStrike, Condition( function Trig_NatureStrike_Conditions ) )
        call TriggerAddAction( gg_trg_NatureStrike, function Trig_NatureStrike_Actions )
    endfunction
    
    endscope


    Note the use of the Vexorian's TimerUtils. SetTimerData, GetTimerData, and ReleaseTimer.

    If you do not understand something, just ask.
     
  6. Laiev

    Laiev Hey Listen!!

    Ratings:
    +187 / 0 / -0
  7. rexpim

    rexpim Member

    Ratings:
    +8 / 0 / -0
    You are talking about this Vexorian's TimerUtils
    didn't get this part "local thistype"

    This works for units to?

    i know that's why i asked for another way to do it, i will try Vexorian's TimerUtils
     
  8. Laiev

    Laiev Hey Listen!!

    Ratings:
    +187 / 0 / -0
    >> You are talking about this Vexorian's TimerUtils
    Yes

    >> didn't get this part "local thistype"
    local = you know what is, variable local and thistype refer to the the struct type/name
    for example:

    JASS:
    struct A
        private static method create takes nothing returns thistype //thistype because will return this struct
           return A //return A because A is the same as thistype, but you should use thistype instead of the direct name
        endmethod
    endstruct


    >> This works for units to?
    This work for Widgets, I'm not sure what is all Widgets but I'm sure that Units and Items works.

    PS: GetWidgetLive is fastest then GetUnitState
     
  9. Zeth

    Zeth Member

    Ratings:
    +3 / 0 / -0
    I think he talks about the lifetime of the destructable (constant real LIFE = 20.00), not life itself.
     
  10. Sevion

    Sevion The DIY Ninja

    Ratings:
    +423 / 0 / -0
    Methods and thistype are all part of structs (data structures).

    Basically, imagine that it's a new variable type with members and methods (functions pertaining to that specific type).

    JASS:
    struct myStruct
        integer myData
        public method getData takes nothing returns integer
            return this.myData // this implies "this instance"
        endmethod
        public method setData takes integer newData returns nothing
            set this.myData = newData
        endmethod
        public static method create takes integer data returns thistype
            // static method create always returns thistype and is public (public need not be explicitly set
            local thistype data = thistype.allocate() // allocates an address for variable "data" of type "thistype" or myStruct
            // It's the same as typing local myStruct data = myStruct.allocate()
            // Imagine the create method as a two-step process like UnitCreate broken into two. Create -> Allocate
            set data.myData = data
            return data
        endmethod
    endstruct
     
  11. Bribe

    Bribe vJass errors are legion

    Ratings:
    +67 / 0 / -0
    Just avoid structs whenever you can, because you'll really screw yourself in efficiency 99% of the time. Structs don't work in vJass like they do in truly-integrated programming languages, so stick to plain JASS and keep things simple.
     
  12. Sgqvur

    Sgqvur FullOfUltimateTruthsAndEt ernalPrinciples, i.e shi

    Ratings:
    +62 / 0 / -0
    Structs are parallel arrays. Arrays are the fastest(as in most efficient) thing in Jass2.
    Structs also reduce the complexity of a jass script immensely.

    What is a "truly-integrated programming language" anyway?
     
  13. Bribe

    Bribe vJass errors are legion

    Ratings:
    +67 / 0 / -0
    Nothing wrong with using them if you know what you're doing, but most people reference the same "struct member" over and over and over, not realising the efficiency hit because of all the excessive array references instead of storing them temporarily to local variables.
     
  14. rexpim

    rexpim Member

    Ratings:
    +8 / 0 / -0
    Ty for the answers

    i want to create the tree and then remove it from the game

    I got a big problem I implemented TimerUtils to my map to test it and i got this error "Function redeclared: New Timer" what I'm doing wrong?
    Solved
    But now i got error "Syntax error" in this line " static if(TimerUtils___USE_HASH_TABLE) then"
     
  15. Laiev

    Laiev Hey Listen!!

    Ratings:
    +187 / 0 / -0
    update your JassHelper
     
  16. Zeth

    Zeth Member

    Ratings:
    +3 / 0 / -0
    Ok, then you don't need the life of the tree. Destructables are handles, then you can use a system like TimedHandles too (i don't know if that system is updated.)
     
  17. rexpim

    rexpim Member

    Ratings:
    +8 / 0 / -0
    I have Jass NewGen Pack v5d, jasshelper version:0.9.I.2 can't find any updates
     
  18. Laiev

    Laiev Hey Listen!!

    Ratings:
    +187 / 0 / -0
  19. rexpim

    rexpim Member

    Ratings:
    +8 / 0 / -0
  20. Laiev

    Laiev Hey Listen!!

    Ratings:
    +187 / 0 / -0
    ya, you create trigger and register events, conditions and actions to that trigger, then that trigger can run ingame
     

Share This Page