AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   How to make a function return 2 values ? (https://forums.alliedmods.net/showthread.php?t=62337)

ConnorMcLeod 10-24-2007 12:35

How to make a function return 2 values ?
 
Hi,

I would like to make a function like get_user_weapon.

For example get_user_hp(id, ap) , no matter if ap is optional or not.

Code:
// in whatever function in the plugin new ap, hp = get_user_hp(id, ap) get_user_hp(id, ap) {     // suppose the player has 100HP, 75HP     // how can it return hp and also apply 75 to the var ap ? }

Any other way to return 2 values will be fine.


-edit-
I'm about to try somthing like :
Code:
get_user_hp(id, & ap) {     // suppose the player has 100HP, 75HP     // how can it return hp and also apply 75 to the var ap ?     ap = 75     return 100 }

Alka 10-24-2007 12:49

Re: How to make a function return 2 values ?
 
Simple...Something like ?
Code:

stock get_user_hp_ap(index)
{
 new Values[2];
 
 Values[0] = get_user_health(index);
 Values[1] = get_user_armor(index);
 
 return Values;
}


ConnorMcLeod 10-24-2007 12:53

Re: How to make a function return 2 values ?
 
Thanks, i tried something like this but it crashed my listenserver, but i may have made an error somewhere else...

Alka 10-24-2007 13:04

Re: How to make a function return 2 values ?
 
Oh, np, if not working i will think at something else... :)

Arkshine 10-24-2007 13:27

Re: How to make a function return 2 values ?
 
Something like should work :

Code:
my_function( id ) {     new health, armor;     get_user_hp_ap( id, health, armor );         client_print( id, print_chat, "Health: %d | Armor: %d", health, armor ); } get_user_hp_ap( index, &health, &armor ) {     health = get_user_health( index );     armor  = get_user_armor( index ); }

Alka 10-24-2007 13:30

Re: How to make a function return 2 values ?
 
Oh yeah lol, i think arkshine is right :s

MaximusBrood 10-24-2007 16:49

Re: How to make a function return 2 values ?
 
Arkshine is wrong. Unlike arrays, simple cells are passed by value, so you have to explicitly pass them by reference.

Code:

getHPAndAP(user, &hp, &ap)
{
        hp = get_user_health(user);
        ap = get_user_armor(user);
}

mainFunction(id)
{
        new hp, ap;
        getHPAndAP(id, hp, ap);
       
        //...
}


ConnorMcLeod 10-24-2007 17:13

Re: How to make a function return 2 values ?
 
Thanks, now it works like a charm.


All times are GMT -4. The time now is 01:12.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.