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

Why doesn't this function work?

Status
Not open for further replies.

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,467
This doesn't give me any syntax errors, but mind-bogglingly doesn't give me any results.

This script is referenced...
JASS:
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:
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...
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
JASS:
...
        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:

JASS:
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.
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
JASS:
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

Bribe said:
This doesn't give me any syntax errors,

This function isn't even defined properly, it should load you with syntax errors.
 
Level 7
Joined
Oct 14, 2008
Messages
340
Uhh, it shouldn't be
JASS:
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:
JASS:
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.
 

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,467
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.
 
Level 7
Joined
Oct 14, 2008
Messages
340
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.
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
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:

JASS:
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
 
Status
Not open for further replies.
Top