# Why doesn't this function work?

Discussion in 'Triggers & Scripts' started by Bribe, Feb 11, 2010.

1. ### Bribe

Joined:
Sep 26, 2009
Messages:
8,015
Resources:
25
Maps:
3
Spells:
10
Tutorials:
3
JASS:
9
Resources:
25
This doesn't give me any syntax errors, but mind-bogglingly doesn't give me any results.

This script is referenced...
jass
Code (vJASS):
local location x = GetUnitLoc(GetTriggerUnit())
local real c
local real d
call xy(c,d,x)
call SetDoodadAnimation(c, d, 128, 'JOgr', false, "stand alternate", false)

and it references...
jass
Code (vJASS):
function xy takes real c, real d, location x returns real c, real d
set c = GetLocationX(x)
set d = GetLocationY(x)
return c
return d
endfunction

can anyone please tell me why nothing is happening?

btw this was working perfectly when I was manually extracting the coordinates, so the problem is somewhere in the relationship between these two functions...

2. ### Berb

Joined:
Jan 21, 2006
Messages:
2,539
Resources:
2
JASS:
2
Resources:
2
Code (vJASS):
...
local location x = GetUnitLoc(GetTriggerUnit())
local real c
local real d
call xy(c,d,x)
call SetDoodadAnimation(c, d, 128, 'JOgr', false, "stand alternate", false)
...

function xy takes real c, real d, location x returns real c, real d
set c = GetLocationX(x)
set d = GetLocationY(x)
return c
return d
endfunction

Functions cannot return multiple values; also you don't define the name of the variable in a return-type. What you should be doing is:

Code (vJASS):

local real x = GetUnitX(GetTriggerUnit())
local real y = GetUnitY(GetTriggerUnit())

call SetDoodadAnimation(x, y, 128, 'JOgr', false, "stand alternate", false)

There is no need for your
`xy()`
function.

3. ### Squiggy

Joined:
Mar 25, 2008
Messages:
2,603
Resources:
18
Maps:
2
Spells:
15
Tutorials:
1
Resources:
18
Why the efff.. do you want to do?

4. ### Berb

Joined:
Jan 21, 2006
Messages:
2,539
Resources:
2
JASS:
2
Resources:
2
Code (vJASS):
function xy takes real c, real d, location x returns real c, real d
set c = GetLocationX(x)
set d = GetLocationY(x)
return c
return d
endfunction

This function isn't even defined properly, it should load you with syntax errors.

5. ### Bribe

Joined:
Sep 26, 2009
Messages:
8,015
Resources:
25
Maps:
3
Spells:
10
Tutorials:
3
JASS:
9
Resources:
25
It's weird, game saves and loads just fine. I tried swapping the local origins into the x,y functions so that it only takes location x, and what's interesting is that even though locals c,d are not declared in the function itself it gives me no "undeclared" message even though I use c,d extensively... so weird...

6. ### Berb

Joined:
Jan 21, 2006
Messages:
2,539
Resources:
2
JASS:
2
Resources:
2
Yea that doesn't sound right.

7. ### Maximilianx

Joined:
Oct 14, 2008
Messages:
334
Resources:
0
Resources:
0
Uhh, it shouldn't be
Code (vJASS):
function xy takes real c, real d, location x returns real c, real d

Kind of impossible.. can only do "returns real" and the function will only return the first real you tell it to.
Maybe something more like this:
Code (vJASS):

struct pair
real x
real y
endstruct

function xy takes location x returns pair
local pair p = pair.create()
set p.x = GetLocationX(x)
set p.y = GetLocationY(x)
return p
endfunction

this uses vJass though, and berbenog is completely right by the way, this xy function is useless.

8. ### Bribe

Joined:
Sep 26, 2009
Messages:
8,015
Resources:
25
Maps:
3
Spells:
10
Tutorials:
3
JASS:
9
Resources:
25
it's not useless in that it saves having to write the same script over and over "get x of point, get y of point" where I could make a function that you just plug in one quick thing to return the desired result. I'm using NewGen to do this. I don't know the difference between Jass and VJass. In fact, I don't know 95% of what you guys know about JASS. I don't know structs and libraries and private/public. I understand globals and I understand constant functions, though.

9. ### Maximilianx

Joined:
Oct 14, 2008
Messages:
334
Resources:
0
Resources:
0
You should read the vJASS manual, your scripting life will be 90% easier even understanding just half of it. I'll try to give a brief explanation of a few things but I really recommend taking the time to read the manual, even if only parts of it.

Libraries are added to the top of your maps script when jass helper compiles it, that's pretty simple, but extremely useful.

Structs are basically a set of data stored in one easy to access object, in the example I just showed my struct is able to hold two integers, you can create a struct of that type any time you want and freely change the values, structs are accessable the same way handles are, but the syntax is different, again I suggest reading the manual.

Now for private, if something inside of a library/scope/struct is marked private, it will be inaccessible to all code outside of that library/scope/struct.. and public is the opposite, it allows other parts of your code outside of your library/scope/struct to access the function or handle, or whatever it is you marked public/private.

I'm not the greatest teacher ever, and the manual goes into much greater detail than I have.

10. ### Berb

Joined:
Jan 21, 2006
Messages:
2,539
Resources:
2
JASS:
2
Resources:
2
You obviously don't understand local variables. One function calling another can not change the value of a local variable inside either of the functions. You can only change a local variable from the scope in which it was declared (inside a function).

If you want to change the value of a variable by calling another function, then the variable needs to be a global. You can do this:

Code (vJASS):
globals
real locX
real locY
endglobals

function SomeOtherFunction takes location loc returns nothing
set locX = GetLocationX(loc)
set locY = GetLocationY(loc)
endfunction

function SomeFunction takes nothing returns nothing
local location unitLoc = GetUnitLocation( GetTriggerUnit() )
local real x
local real y

call SomeOtherFunction(unitLoc)
set x = locX
set y = locY

...
endfunction

11. ### Element of Water

Joined:
Aug 3, 2008
Messages:
2,298
Resources:
5
Spells:
3
Tutorials:
1
JASS:
1
Resources:
5
Might as well just use GetUnitX/Y...

12. ### Berb

Joined:
Jan 21, 2006
Messages:
2,539
Resources:
2
JASS:
2
Resources:
2
That's what she said.