HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Returning multiple values with one function

06-24-2007, 03:31 PM#1
moyack
Hi:

I'm working with a coloring function, which returns the RGB values from one player color. First I did this by setting 3 functions where each one returns the R, G and B integer respectively. But then I saw the groupenum functions which uses the group argument to modify it, so I changed all to one function in this way:

Collapse JASS:
function GetColor_GetRGB takes player p, integer r, integer g, integer b returns nothing
    local playercolor c = GetPlayerColor(p)
    if c == PLAYER_COLOR_RED then
        set r = 255
        set g = 0
        set b = 0
    elseif c == PLAYER_COLOR_BLUE then
        set r = 0
        set g = 0
        set b = 255
    elseif c == PLAYER_COLOR_CYAN then
        set r = 24
        set g = 231
        set b = 189
    elseif c == PLAYER_COLOR_PURPLE then
        set r = 82
        set g = 0
        set b = 132
    elseif c == PLAYER_COLOR_YELLOW then
        set r = 255
        set g = 255
        set b = 0
    elseif c == PLAYER_COLOR_ORANGE then
        set r = 255
        set g = 138
        set b = 8
    elseif c == PLAYER_COLOR_GREEN then
        set r = 24
        set g = 190
        set b = 0
    elseif c == PLAYER_COLOR_PINK then
        set r = 231
        set g = 89
        set b = 173
    elseif c == PLAYER_COLOR_LIGHT_GRAY then
        set r = 148
        set g = 150
        set b = 148
    elseif c == PLAYER_COLOR_LIGHT_BLUE then
        set r = 123
        set g = 190
        set b = 247
    elseif c == PLAYER_COLOR_AQUA then
        set r = 8
        set g = 97
        set b = 66
    elseif c == PLAYER_COLOR_BROWN then
        set r = 74
        set g = 40
        set b = 0 
    else
        set r = 255
        set g = 255
        set b = 255
    endif 
endfunction

But now I have one problem.... it is always returning 0 to all the values. My question is: this option is possible with integers?? or does it only work with handles??
06-24-2007, 04:07 PM#2
zen87
Hmmm... I can't think of a better way but using a struct and extract all the variable you want within the struct
06-24-2007, 04:57 PM#3
SFilip
Or using three global variables.
Or a dynamical array, though I think that would be pointless with only three values.
06-24-2007, 05:12 PM#4
Earth-Fury
Quote:
Originally Posted by moyack
But now I have one problem.... it is always returning 0 to all the values. My question is: this option is possible with integers?? or does it only work with handles??
that would only work with handles. integers are native types, not pointers. (thus modifying an integer modifies the variables value, instead of a data structure independant of the variable itself)
06-24-2007, 05:29 PM#5
moyack
yes, probably I'll use a struct instead of this way.
06-24-2007, 08:22 PM#6
Toadcop
to use global vars would be faster ^^
cause local arrays are slower than global vars so global arrays are also slower than local arrays =) you see ?
06-24-2007, 08:25 PM#7
Captain Griffen
WTF are you on? Your logic makes no sense.

Individual variables are far, far faster than arrays. Local or global makes no difference, aside from a slight initing cost presumably (which, by the way, is basically nil as far as I'm aware).

Structs are global arrays.
06-24-2007, 08:35 PM#8
Toadcop
no it makes =) i don't tell what the difference is great but in some cases it makes sense to think about it.
+ btw you can use stack to store your return data.
Code:
call push(x1)
call push(x2)
call push(x3)
//....
set myvar1=pop() // x3
set myvar2=pop() // x2
set myvar3=pop() // x1
// and don't forget to pop all current data cause otherwise it will end in a mess =) or some kind leaks.
you can make different stacks for needed types etc...

Quote:
Individual variables are far, far faster than arrays. Local or global makes no difference
ha ha stop talking shit. cause it's not true !
06-24-2007, 08:38 PM#9
Captain Griffen
Care to provide some benchmarks with significant differences? If not, you have no evidence, so why should we listen to you...?
06-24-2007, 08:41 PM#10
Toadcop
ofc i can =) otherwise i would not write something like that <_<
06-24-2007, 09:07 PM#11
Toadcop
sorry ^^ (i mean double posting)
here the test map (you need JAPI, if you have JassNewGen pack so you can launch it from we or start .bat from grimoire directory)

yeap Toadcop tells true =) there are differnce between local and global vars and arrays. so globals are allways a bit slower. and the difference between var and array is max 2x (to get correct results run the test with out any "sets" only the loop and subtract this value from results and compare this result so you would get more precise results.) =)
Attached Files
File type: w3xX.w3x (40.1 KB)
06-24-2007, 09:32 PM#12
Captain Griffen
No conclusive results. If there is a difference, it simply isn't large enough to get a conclusive result. I had an 8% difference is speed over 5 readings, but given that between two readings of the same thing there was a 31% difference, that's not conclusive.

You simply cannot measure such low level stuff accurately.
06-24-2007, 11:24 PM#13
Toadcop
Captain Griffen but if you run this test alot of time you will see the difference and btw ! you can simple check the lowest values ! (so the best result !) for example run test 50 times a note the lowest value so you can compare it !

// i never told what the difference is great =) it's just theoretical shit ^^
06-25-2007, 02:22 AM#14
moyack
The funny thing with this is: you can get the arguments, you can modify it inside the function but they are not updated outside the function. One example about what I mean is this Blizzard function:

Collapse JASS:
function CreateNUnitsAtLoc takes integer count,integer unitId,player whichPlayer,location loc,real face returns group
    call GroupClear(bj_lastCreatedGroup)
    loop
        set count = count - 1 // <= you can modify the argument, but you can't get the value outside the function. weird :P
        exitwhen count < 0
        call CreateUnitAtLocSaveLast(whichPlayer, unitId, loc, face)
        call GroupAddUnit(bj_lastCreatedGroup, bj_lastCreatedUnit)
    endloop
    return bj_lastCreatedGroup
endfunction

You see?? it's something funny and disappointing at the same time.

That means that the argument in fact is copied if it's different from a handle, doesn't it?? I'll do a test.
06-25-2007, 03:12 AM#15
DioD
argument is nothing more then local value, you cant pass global variable as handle into function directly.

you have to use cache or other temporary storage