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

[JASS] Variables?

Status
Not open for further replies.
Level 3
Joined
May 2, 2008
Messages
41
I got a trigger with some local variables, but is there a way that I can use those variables in another trigger without creating any globals? Or can I make specific actions already inside a trigger execute at an event? I hope you understand
 
Level 2
Joined
Jun 25, 2009
Messages
19
You could pass them to the other trigger function by using

JASS:
call OtherTriggerfunction(variable1, variable2, variable3, ...)

Other than that, the local variables are local to the function only, so you can use them in another trigger, unless you define them inside a function.
 
Level 6
Joined
May 7, 2009
Messages
228
You can't use local variables outside the scope in which they were declared. (Your trigger action function)
That's practically the definition of a local variable.

If you explain what you are trying to do, maybe we could find a way to redesign it.
 
Level 3
Joined
May 2, 2008
Messages
41
Example

Ok, this is an example, not my accual trigger:
JASS:
function Trig_Example_Actions takes nothing returns nothing
    local unit u = GetTriggerUnit()
    local location l = GetUnitLoc(u)
    local real x = GetLocationX(l)
    local real y = GetLocationY(l)
    
    // Some actions
endfunction

//===========================================================================
function InitTrig_Example takes nothing returns nothing
    set gg_trg_Example = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Example, EVENT_PLAYER_UNIT_SPELL_EFFECT )
    call TriggerAddAction( gg_trg_Example, function Trig_Example_Actions )
endfunction
Now I need to use the x and y for something, when something else happens such as EVENT_PLAYER_UNIT_ISSUED_ORDER
 
Level 6
Joined
May 7, 2009
Messages
228
I don't think there's anyway to do it without globals

If you want it per player, it's easy
If you want the data remembered per unit, you'll have to use either h2i + array or a hashtable depending on the version.

Or you could use VJass and let it do the same thing for you.
 
Level 12
Joined
Apr 27, 2008
Messages
1,228
There always is a way. But you have given too little input. Elaborate please( the description of what you want to do).
 
Level 3
Joined
May 2, 2008
Messages
41
Fine

I want to make a unit get the same orders as another unit, but with different coordinates.
 
Level 3
Joined
May 2, 2008
Messages
41
you can let the other function be called via parameters but this works only if you call it from your declaring trigger but where is a problem with globals ? u do this via the takes arguments in a function

Parameters?..
The problem is that I need lots of units to be using this trigger and every time I use it I create a unit which I obviously need to remember to make it work
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
Two unit global arrays, a timer, every iteration loop over all the indexes and match the direction of one global array to the other.

I posted an example of this multiple times already, so please try to search for it (although it's mostly seen in Jass, but I DID go and make a GUI example, so you might want to search that post).
 
Level 3
Joined
May 2, 2008
Messages
41
I posted an example of this multiple times already, so please try to search for it (although it's mostly seen in Jass, but I DID go and make a GUI example, so you might want to search that post).

Mind giving me a hint on what to search for?
 
Level 3
Joined
May 2, 2008
Messages
41
I don't think you understand

I need a MUI trigger(s) where the first part a unit is created, and afterwards that unit is ordered to move relative to the original unit.
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
Sadly I can't find my original post.
I do not have Warcraft anymore, so I'll try to teach it with text rather then the actual triggers.


Basically the idea is to separate the whole thing into two parts.
- Part 1 registers units that should be effected by your spell.
- Part 2 does the actual stuff that effects all the registered units.

Now, say your spell (as in this case) needs to know two things: the caster, and the target.
For this you need these variables (you will see exactly why later on):
- Unit array
- Unit array
- Integer (initialize at 0)
- Timer

Now, lets create the first part, which registers units.

Create a new trigger with whatever events and conditions you want.
Now you basically want to add the caster and the target to your unit arrays, and tell the trigger "I have registered a new unit and target".
You will set the index equal to the integer in the arrays (I'll call that integer "Total" from now on), then you will raise the value of Total by 1.

For example:
  • Set Casters[Total] = Caster
  • Set Targets[Total] = Target
  • Set Total = Total + 1
And that's it. You have registered a new "instance" of the spell.
Notice that it is good practice to check how many instances are running - if there are zero, you can pause the timer, if they become more then zero, you can unpause the timer.

Now lets go over part 2, where you actually DO stuff with the data you have.

Create a second trigger, with the event of your timer going off.
Now, create a loop that will loop from 0 (arrays start at 0), to [Total - 1] - that will be the count of the "running" instances.

Inside this loop, you will need to do two things, and can do one optional thing:

1) Do your actions.
Since you are looping through all the instances, you can refer to each caster as Casters[Integer A] and each target as Targets[Integer A] (assuming you called your arrays Casters and Targets of course).

2) Remove unneeded instances.
Once you don't need an instance (the spell's time is up, for example), you need to remove that instance, or in other words "unregister it".
The easiest way to do that, is take the LAST instance (Total - 1, since arrays start at 0), now you set that data to the current instance (the one that is being deleted), you set Total to its value minus 1, and that's it.

An example:
  • Set Casters[Integer A] = Casters[Total - 1]
  • Set Targets[Integer A] = Targets[Total - 1]
  • Set Total = Total - 1
2b) This is recommended but not necessary - whenever you unregister an instance, you can check if Total is equal to 0, and if it is you can pause the timer.

If you got it until here, then congratulate yourself. You have just learned how to make easy MUI repeatable-type-of-triggers in GUI (works in Jass too of course).
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
But wouldn't it be kind of slow if 40 units need to know what position they have in the same array at the same time?
Thanks for the great reply

Iterating through loops is very fast, you don't need to worry about it.

Notice that this isn't "fully" MUI, the maximum number of indexes in Warcraft arrays is 8192, but it will be enough for almost anything you'd ever want to make in Warcraft anyway.
 
Status
Not open for further replies.
Top