1. Updated Resource Submission Rules: All model & skin resource submissions must now include an in-game screenshot. This is to help speed up the moderation process and to show how the model and/or texture looks like from the in-game camera.
    Dismiss Notice
  2. DID YOU KNOW - That you can unlock new rank icons by posting on the forums or winning contests? Click here to customize your rank or read our User Rank Policy to see a list of ranks that you can unlock. Have you won a contest and still haven't received your rank award? Then please contact the administration.
    Dismiss Notice
  3. Let your favorite entries duke it out in the 15th Techtree Contest Poll.
    Dismiss Notice
  4. Weave light to take you to your highest hopes - the 6th Special Effect Contest is here!
    Dismiss Notice
  5. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

Small Code Snippets

Discussion in 'Triggers & Scripts' started by PurplePoot, Sep 29, 2007.

  1. Aniki

    Aniki

    Joined:
    Nov 7, 2014
    Messages:
    545
    Resources:
    6
    Tools:
    1
    Maps:
    1
    Spells:
    1
    JASS:
    3
    Resources:
    6
    That dies with a "FATAL ERROR", the recursive death is more silent, I think =).
     
  2. AGD

    AGD

    Joined:
    Mar 29, 2016
    Messages:
    538
    Resources:
    13
    Spells:
    7
    Tutorials:
    1
    JASS:
    5
    Resources:
    13
    Nice! lol, I always complicate myself =D

    Edited my post

    EDIT:

    How about
    hook DoNothing ExitWarcraft
    ? GUI users should not be using this function after all =).
     
    Last edited: Dec 19, 2016
  3. AGD

    AGD

    Joined:
    Mar 29, 2016
    Messages:
    538
    Resources:
    13
    Spells:
    7
    Tutorials:
    1
    JASS:
    5
    Resources:
    13
    Another one
    Code (vJASS):
    library CodeArray /*


        */
    requires /*

        */
    Typecast /*  - http://www.hiveworkshop.com/threads/accessing-memory-from-the-script-its-time-of-the-revolution.279262/

        |=====|
        | API |
        |=====|

            //! runtextmacro GLOBAL_CODE_ARRAY("PRIVACY", "NAME")
                - Declares a global code array

        */

        //! textmacro GLOBAL_CODE_ARRAY takes PRIVACY, NAME
        $PRIVACY$ struct $NAME$ extends array

            private static integer codes

            static method operator [] takes integer index returns code
                return I2C(codes[index])
            endmethod

            static method operator []= takes integer index, code c returns nothing
                set codes[index] = C2I(c)
            endmethod

        endstruct
        //! endtextmacro


    endlibrary
     
     
    Last edited: Dec 21, 2016
  4. Wietlol

    Wietlol

    Joined:
    Aug 1, 2013
    Messages:
    4,638
    Resources:
    3
    Spells:
    3
    Resources:
    3
    Is the "Code" really required in the name?
     
  5. AGD

    AGD

    Joined:
    Mar 29, 2016
    Messages:
    538
    Resources:
    13
    Spells:
    7
    Tutorials:
    1
    JASS:
    5
    Resources:
    13
    Oh surely not. It should be $NAME$ only. Edited.
     
  6. Almia

    Almia

    Joined:
    Apr 24, 2012
    Messages:
    4,842
    Resources:
    35
    Spells:
    30
    Tutorials:
    4
    JASS:
    1
    Resources:
    35
    Code (vJASS):
    /**********************************************************
    *
    *   FatalError
    *   v1.1.2.1
    *   By Magtheridon96 (Special Thanks to Nestharus)
    *
    *   Uses:
    *   -----
    *
    *       - Annoying a player. lol
    *
    *   API:
    *   ----
    *
    *       - function ExitGame             takes nothing           returns nothing
    *       - function ExitGameForPlayer    takes player p          returns nothing
    *
    *       - function FreezeGame           takes nothing           returns nothing
    *       - function FreezeGameForPlayer  takes player p          returns nothing
    *
    *       - function CrashGame            takes nothing           returns nothing
    *       - function CrashGameForPlayer   takes player p          returns nothing
    *
    *       - function DesyncGameForPlayer  takes player p          returns nothing
    *
    *       - function EatRAM               takes nothing           returns nothing
    *           - This function can take up about 1 GB of memory in a minute.
    *           - Use this on cheaters ;D
    *           - This function may crash the game at one point.
    *
    *       - function StopThread          takes nothing           returns nothing
    *
    **********************************************************/

    library FatalError
       
        globals
            private trigger Q = CreateTrigger()
        endglobals
       
        function ExitGame takes nothing returns nothing
            loop
                call ExecuteFunc("ExitGame")
            endloop
        endfunction
       
        function ExitGameForPlayer takes player p returns nothing
            if GetLocalPlayer()==p then
                call ExitGame()
            endif
        endfunction
       
        function FreezeGame takes nothing returns nothing
            loop
                call TriggerSyncReady()
                call ExecuteFunc("FreezeGame")
            endloop
        endfunction
       
        function FreezeGameForPlayer takes player p returns nothing
            if GetLocalPlayer()==p then
                call FreezeGame()
            endif
        endfunction
       
        function CrashGame takes nothing returns nothing
            call Player(-1)
        endfunction
       
        function CrashGameForPlayer takes player p returns nothing
            if GetLocalPlayer()==p then
                call CrashGame()
            endif
        endfunction
       
        function DesyncGameForPlayer takes player p returns nothing
            if GetLocalPlayer()==p then
                call CreateTrigger()
            endif
        endfunction
       
        private function Z takes nothing returns boolean
            local location l
            local integer i = 1000
            loop
                exitwhen 0==i
                set l = Location(0,0)
                set l = Location(0,0)
                set l = Location(0,0)
                set i=i-1
            endloop
            return false
        endfunction
       
        function EatRAM takes nothing returns boolean
            loop
                call TriggerEvaluate(Q)
            endloop
            return false
        endfunction
       
        function StopThread takes nothing returns nothing
            local integer i = 1/0
        endfunction
       
        private module Init
            private static method onInit takes nothing returns nothing
                call TriggerAddCondition(Q,Condition(function Z))
            endmethod
        endmodule
        private struct Inits extends array
            implement Init
        endstruct
    endlibrary


    you forgot that this existed lol

    link: Fatal Error
     
  7. Pyrogasm

    Pyrogasm

    Joined:
    Feb 27, 2007
    Messages:
    3,788
    Resources:
    1
    Spells:
    1
    Resources:
    1
    This is dumb and really simple, but I needed a way to remove an effect without showing its death animation. (Specifically to replace it with something else.) I didn't see something like this in this thread from my searching.

    Edit: see below
     
    Last edited: Jul 16, 2019
  8. Bribe

    Bribe

    Joined:
    Sep 26, 2009
    Messages:
    8,283
    Resources:
    25
    Maps:
    3
    Spells:
    10
    Tutorials:
    3
    JASS:
    9
    Resources:
    25
    If the effect is some kind of missile with a trail (like Phoenix Fire) this will cause a trail to appear between the special effect and its destination.
     
  9. Pyrogasm

    Pyrogasm

    Joined:
    Feb 27, 2007
    Messages:
    3,788
    Resources:
    1
    Spells:
    1
    Resources:
    1
    Huh, didn't notice that. This one shouldn't.
    Code (vJASS):
    library RemoveEffect
      //Destroying an effect always plays its death animation and any associated sounds.
      //This library fixes it in a simple way: move the effect to a location where nobody can see or hear it.

      globals
        private real SAFE_X = -3900.
        private real SAFE_Y =  3900.
        private real SAFE_Z = -1000. //not strictly necessary

        private real TIMESCALE = 10. //doesn't really matter but we set it to > 0 so the animations actually finish
      endglobals

      function RemoveEffect takes effect e returns nothing
        call BlzSetSpecialEffectAlpha(e, 255)
        call BlzSetSpecialEffectZ(e, SAFE_Z)
        call BlzSetSpecialEffectPosition(e, SAFE_X, SAFE_Y, SAFE_Z)
        call BlzSetSpecialEffectTimeScale(e, TIMESCALE)
        call DestroyEffect(e)
      endfunction
    endlibrary
     
  10. Insanity_AI

    Insanity_AI

    Joined:
    Jun 30, 2017
    Messages:
    30
    Resources:
    0
    Resources:
    0
    A bit of code used for functions that are not allowed to have arguments, such as timer functions, triggeraction/condition functions, enumDestruct/Item functions, filterfuncs, etc...

    Code (Lua):
    FunctionUtils = {}

    function SetFunctionParameters(func, ...)
        FunctionUtils[func] = {...}
    end

    function GetFunctionParameters(func)
        return table.unpack(FunctionUtils[func])
    end
    How to use:

    Code (Lua):
    function test1()

        local argument1, argument2, argument3 = GetFunctionParameters(test1)

    -- function does stuff...

    end

    SetFunctionParameters(test1, argument1, argument2, argument3)
    -- call the test1 function in whatever way.
     
    Last edited: Jan 10, 2020
  11. chopinski

    chopinski

    Joined:
    May 16, 2012
    Messages:
    384
    Resources:
    3
    Spells:
    3
    Resources:
    3
    Sorry for replaying to a post from 9 years, but i think this is very interesting and potentially useful to me, since this concept could be extrapolated to multiple timeouts. I'm trying to understand how i would convert multiple triggers that use a timer and a hash-table that maps handles to the timer created to this snipped.
    Example: How something as simple as this could be done with this snippet? What i mean is, how can i access the stuff that i would normally save in a hash-table in the parent key of the timer with this code?

    Code (vJASS):

    scope Example initializer Init

    private function DoSomethingPeriodically takes nothing returns nothing
        local timer t = GetExpiredTimer()
        local unit  u = LoadUnitHandle(aHashTable, GetHandleId(t), 1)

        //Doing something periodically with unit u
     
        //if conditions were met then i would do this
        call FlushChildHashtable(aHashTable, GetHandleId(t))
        call PauseTimer(t)
        call DestroyTimer(t)
     
        set t = null
        set u = null
    endfunction

    private function Conditions takes nothing returns nothing
        local unit  u = GetTriggerUnit()
        local timer t = CreateTimer()

        call SaveUnitHandle(aHashTable, GetHandleId(t), 1, u)
        call TimerStart(t, 1.0, true, function DoSomethingPeriodically)

        set u = null
        set t = null
    endfunction
    //===========================================================================
    private function Init takes nothing returns nothing
        call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_SPELL_EFFECT, function Conditions)
    endfunction

    endscope
     
     
  12. AGD

    AGD

    Joined:
    Mar 29, 2016
    Messages:
    538
    Resources:
    13
    Spells:
    7
    Tutorials:
    1
    JASS:
    5
    Resources:
    13
    I'm afraid you can't do this with Mag's snippet or other snippet that uses only one timer
     
  13. chopinski

    chopinski

    Joined:
    May 16, 2012
    Messages:
    384
    Resources:
    3
    Spells:
    3
    Resources:
    3
    It can be done using dynamic indexing in structs. I teach myself that. Actually, I expanded mag's snippet to a set of common used timers and now I run everything that is periodic on very few timers instead of hundreds.
     
  14. AGD

    AGD

    Joined:
    Mar 29, 2016
    Messages:
    538
    Resources:
    13
    Spells:
    7
    Tutorials:
    1
    JASS:
    5
    Resources:
    13
    I said it's impossible to do it using only one timer in the manner of method you described above, like
    i.e, using hashtable + GetHandleId() as parent key.

    But of course no problem cannot be solved by adding layers of abstraction in one's approach.
    Code (vJASS):
        module TimerOne
            private thistype prev
            private thistype next
            private thistype data

            private static method onTimerExpire takes nothing returns nothing
                local thistype node = thistype(0).next
                loop
                    exitwhen node == list
                    if not node.data.onTimerPeriod() then
                        set node.next.prev = node.prev
                        set node.prev.next = node.next
                    endif
                    set node = node.next
                endloop
            endmethod

            static method startTimerOne takes integer data returns nothing
                local thistype node = Node.allocate()
                if thistype(0).next == 0 then
                    call TimerOne.register(function thistype.onTimerExpire)
                endif
                set node.prev = list.prev
                set node.next = list
                set list.prev = node
                set node.prev.next = node
                set node.data = data
            endmethod
        endmodule

    ...

    struct MyStruct
        private method onTimerPeriod takes nothing returns boolean
            set this.xxx // this -> the attached data

            ...

            return <conditionToContinue>
        endmethod

        implement TimerOne

        private static method onInit takes nothing returns nothing
            call startTimerOne(<dataAttachment>) // Now you can attach data while still using only 1 timer
        endmethod
    endstruct
     
     
  15. Bribe

    Bribe

    Joined:
    Sep 26, 2009
    Messages:
    8,283
    Resources:
    25
    Maps:
    3
    Spells:
    10
    Tutorials:
    3
    JASS:
    9
    Resources:
    25
    And we've now reinvented Timer32 by Jesus4Lyf System - Timer32. Time to go back to reinventing the wheel.
     
  16. AGD

    AGD

    Joined:
    Mar 29, 2016
    Messages:
    538
    Resources:
    13
    Spells:
    7
    Tutorials:
    1
    JASS:
    5
    Resources:
    13
    Exactly ^