Raised This Month: $ Target: $400
 0% 

How to make a function return 2 values ?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 10-24-2007 , 12:35   How to make a function return 2 values ?
Reply With Quote #1

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 }

Last edited by ConnorMcLeod; 10-24-2007 at 12:48.
ConnorMcLeod is offline
Alka
AMX Mod X Plugin Approver
Join Date: Dec 2006
Location: malloc(null)
Old 10-24-2007 , 12:49   Re: How to make a function return 2 values ?
Reply With Quote #2

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;
}
__________________
Still...lovin' . Connor noob! Hello
Alka is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 10-24-2007 , 12:53   Re: How to make a function return 2 values ?
Reply With Quote #3

Thanks, i tried something like this but it crashed my listenserver, but i may have made an error somewhere else...
ConnorMcLeod is offline
Alka
AMX Mod X Plugin Approver
Join Date: Dec 2006
Location: malloc(null)
Old 10-24-2007 , 13:04   Re: How to make a function return 2 values ?
Reply With Quote #4

Oh, np, if not working i will think at something else...
__________________
Still...lovin' . Connor noob! Hello
Alka is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 10-24-2007 , 13:27   Re: How to make a function return 2 values ?
Reply With Quote #5

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 ); }
__________________

Last edited by Arkshine; 10-24-2007 at 17:19.
Arkshine is offline
Alka
AMX Mod X Plugin Approver
Join Date: Dec 2006
Location: malloc(null)
Old 10-24-2007 , 13:30   Re: How to make a function return 2 values ?
Reply With Quote #6

Oh yeah lol, i think arkshine is right :s
__________________
Still...lovin' . Connor noob! Hello
Alka is offline
MaximusBrood
Veteran Member
Join Date: Sep 2005
Location: The Netherlands
Old 10-24-2007 , 16:49   Re: How to make a function return 2 values ?
Reply With Quote #7

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);
	
	//...
}
__________________
Released six formerly private plugins. Not active here since ages.
MaximusBrood is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 10-24-2007 , 17:13   Re: How to make a function return 2 values ?
Reply With Quote #8

Thanks, now it works like a charm.
ConnorMcLeod is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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