variable storage

Frizz.Meister

New Member
Reaction score
0
Ok im not sure entirly how to phrase this but i am trying to save a variable (a unit to be precise) to use elswhere in my trigger. However the trigger is ment to be mui so udgs are out of the question. Locals cant be used because the variable is needed in more than one function. Also private globals (in library) dont work because of the afore mentioned mui.

In short how can i save the unit needed whilst keeping the trigger mui and being able to access the variable in more than one function?

I have a good knowledge of gui, jass and have started to learn vjass if this helps.
 

Azlier

Old World Ghost
Reaction score
461
What are you trying to pass data to? A timer callback? A group callback? Some random function? In pretty much every case, having a struct would be a good idea. Please hold while I fetch a struct tutorial.

...

...

Here.
 

Frizz.Meister

New Member
Reaction score
0
a timer callback is the main function. Never heard of structs but ill take a look at the tut and see if it helps.
 

Frozenhelfir

set Gwypaas = Guhveepaws
Reaction score
56
You can pass it into the function:(freehand)

JASS:
private function PassUnitToMe takes unit u returns nothing
//the function now stores the passed in unit u as a local variable and can be used
call KillUnit(u)
endfunction

private function MyAction takes nothing returns nothing
local unit u = GetTriggerUnit()
call PassUnitToMe(u)
//can also do this
call PassUnitToMe(GetTriggerUnit())
endfunction
//sorry for no indents, tab doesn't work on the forum and I'm too lazy to space.
 

Azlier

Old World Ghost
Reaction score
461
Well, structs are pretty much used to pass as much data as you need around to functions as an integer. In your case, you need a timer attachment system. I reccommend TimerUtils (I always do, even if it's not the most efficient one out there.)

JASS:
library_once TimerUtils
//*********************************************************************
//* TimerUtils (Blue flavor)
//* ----------
//*
//*  To implement it , create a custom text trigger called TimerUtils
//* and paste the contents of this script there.
//*
//*  To copy from a map to another, copy the trigger holding this
//* library to your map.
//*
//* (requires vJass)   More scripts: htt://www.wc3campaigns.net
//*
//* For your timer needs:
//*  * Attaching
//*  * Recycling (with double-free protection)
//*
//* set t=NewTimer()      : Get a timer (alternative to CreateTimer)
//* ReleaseTimer(t)       : Relese a timer (alt to DestroyTimer)
//* SetTimerData(t,2)     : Attach value 2 to timer
//* GetTimerData(t)       : Get the timer's value.
//*                         You can assume a timer's value is 0
//*                         after NewTimer.
//*
//* Blue Flavor: Slower than the red flavor, it got a 408000 handle id
//*             limit, which means that if more than 408000 handle ids
//*             are used in your map, TimerUtils might fail, this
//*             value is quite big and it is much bigger than the 
//*             timer limit in Red flavor.
//*
//********************************************************************

//================================================================
    globals
        private constant integer MAX_HANDLE_ID_COUNT = 408000
        // values lower than 8191: very fast, but very unsafe.
        // values bigger than 8191: not that fast, the bigger the number is the slower the function gets
        // Most maps don't really need a value bigger than 50000 here, but if you are unsure, leave it
        // as the rather inflated value of 408000
    endglobals

    //=================================================================================================
    private function H2I takes handle h returns integer
        return h
        return 0
    endfunction

    //==================================================================================================
    globals
        private integer array data[MAX_HANDLE_ID_COUNT]
        private constant integer MIN_HANDLE_ID=0x100000
    endglobals

    //It is dependent on jasshelper's recent inlining optimization in order to perform correctly.
    function SetTimerData takes timer t, integer value returns nothing
        debug if(H2I(t)-MIN_HANDLE_ID>=MAX_HANDLE_ID_COUNT) then
        debug     call BJDebugMsg("SetTimerData: Handle id too big, increase the max handle id count or use gamecache instead")
        debug endif
        set data[H2I(t)-MIN_HANDLE_ID]=value
    endfunction

    function GetTimerData takes timer t returns integer
        debug if(H2I(t)-MIN_HANDLE_ID>=MAX_HANDLE_ID_COUNT) then
        debug     call BJDebugMsg("GetTimerData: Handle id too big, increase the max handle id count or use gamecache instead")
        debug endif
        return data[H2I(t)-MIN_HANDLE_ID]
    endfunction

    //==========================================================================================
    globals
        private timer array tT
        private integer tN = 0
        private constant integer HELD=0x28829022
        //use a totally random number here, the more improbable someone uses it, the better.
    endglobals

    //==========================================================================================
    function NewTimer takes nothing returns timer
        if (tN==0) then
            set tT[0]=CreateTimer()
        else
            set tN=tN-1
        endif
        call SetTimerData(tT[tN],0)
     return tT[tN]
    endfunction

    //==========================================================================================
    function ReleaseTimer takes timer t returns nothing
        if(t==null) then
            debug call BJDebugMsg("Warning: attempt to release a null timer")
            return
        endif
        if (tN==8191) then
            debug call BJDebugMsg("Warning: Timer stack is full, destroying timer!!")

            //stack is full, the map already has much more troubles than the chance of bug
            call DestroyTimer(t)
        else
            call PauseTimer(t)
            if(GetTimerData(t)==HELD) then
                debug call BJDebugMsg("Warning: ReleaseTimer: Double free!")
                return
            endif
            call SetTimerData(t,HELD)
            set tT[tN]=t
            set tN=tN+1
        endif    
    endfunction

endlibrary


Example on how to use:

JASS:
scope Lol initializer Init

private struct Data
    unit Caster
endstruct

private function Callback takes nothing returns nothing
    local timer t = GetExpiredTimer()
    local Data d = GetTimerData(t)
    call SetUnitExploded(d.Caster, true)
    call KillUnit(d.Caster)
    call ReleaseTimer(t)
    call d.destroy()
    set t = null
endfunction

private function Actions takes nothing returns nothing
    local timer t = NewTimer()
    local Data d = Data.create()
    set d.Caster = GetTriggerUnit()
    call SetTimerData(t, d)
    call TimerStart(t, 6., false, function Callback)
    set t = null
endfunction

private function Init takes nothing returns nothing
    local trigger t = CreateTrigger()
    local integer i = 0
    loop
        exitwhen i == 13
        call TriggerRegisterPlayerUnitEvent(t, Player(i), EVENT_PLAYER_UNIT_SPELL_EFFECT, True)
        set i = i + 1
    endloop
    call TriggerAddAction(t, function Actions)
endfunction

endscope
 

Frizz.Meister

New Member
Reaction score
0
cant use freehand passing because of the timer callback.

As for structs they seem to be the right thing but how to callback as the tut pointed me towards a crazy complicated CSData method which i dont really get.
 

Sooda

Diversity enchants
Reaction score
318
> Also private globals (in library)

News flash structs are actually global variables. Variable arrays work great for MUI.

Seeing all this I am going to freehand JASS which is MUI and passes unit to new function:
JASS:
globals
    unit transferUnit = null
endglobals

function A takes nothing returns nothing
    local unit whichUnit = trasnferUnit
    
    call TriggerSleepAction(1.)
    call KillUnit(whichUnit)
    set whichUnit = null
endfunction

function B takes nothing returns nothing
    local unit whichUnit = CreateUnit(Player(0), 'A001', 0., 0., 0.)

    call TriggerSleepAction(1.)
    set transferUnit = whichUnit
    set whichUnit = null

    call A()
endfunction

Easy way of doing it.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top