• 🏆 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!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece!🔗 Click here to enter!

[JASS] Functions and Locals

Status
Not open for further replies.
Level 7
Joined
Jun 16, 2008
Messages
253
Gah!

Heya, I've been using a lot of custom script in my triggers (still making the transition to JASS), and I've run across a small hitch in one of my triggers.

In my trigger I have a slew of locals.
local location l= null
being one of them. And further down the line I have a little If/Then/Else, with a bunch of conditions that use the local "l". But every time I tried to save my NewGen World Edit kept saying something like 'variable "l" undeclared', but the way the quotes are just for highlighting purposes.

Then I flipped the trigger over to full custom script and I found that the If/Then/Else conditions were in a seperate function.

From this I deduced that locals do not apply to the whole triggery thing, they exist only in their seperate functions (which in the overall scheme makes sense).

My question/problem is... how can I make the conditions in the seperate function take the "l" from the other function so they can recognise the point I'm talking 'bout?

I was wondering if there was some way just to stick that seperate condition function inside the main function thingy? Dad calls it a recursive function. That's the only way I can think of to get the locals to apply to the whole lot. And no I don't want to use globals, they won't work for the spell trigger.
And besides the conditions in the seperate function, there is also an action (also from the If/Then/Else) that uses the "l" as well, so the local does need to apply to both.
 
Level 21
Joined
Aug 21, 2005
Messages
3,699
The easiest way would simply be: not using separate functions for if/then/else statements.

The syntax in jass you're looking for is:

JASS:
local location l = null
set l = something
if (statement == true) then
    // do things with l
else
    // do other things with l
endif

It's also a good thing to leave behind locations and simply use x/y coordinates, for instance:
JASS:
local real x = GetUnitX(a unit variable) // gets x coordinate
local real y = GetUnitY(a unit variable) // gets y coordinate
// Locations need to be cleaned up using RemoveLocation(l), while reals (x and y) don't.
// 99% of the functions that need a location also have a counterpart that use x/y coordinates. 
// As a matter of fact, most of the functions using locations simply use the function using x/y coordinates, and use GetLocationX(l) and GetLocationY(l) to find the x/y coordinates.
 
Level 7
Joined
Jun 16, 2008
Messages
253
Thanks, that helps! I hope I can integrate it properly though (it's a converted GUI trigger), when in JASS, if not, maybe you could do it! :D
The location thingy is based on spelltarget not unit just FYI, if it makes a diff.
Bunch of great tips, thanks. I'll save it on my handy dandy Answers document.
 
Status
Not open for further replies.
Top