Raised This Month: $51 Target: $400
 12% 

Solved [ H3LP ] Can't return string on functions?


Post New Thread Reply   
 
Thread Tools Display Modes
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 06-23-2017 , 10:10   Re: [ H3LP ] Can't return string on functions?
Reply With Quote #11

Quote:
Originally Posted by PRoSToTeM@ View Post
But natives don't support returning arrays, it can be done only with workarounds.
https://forums.alliedmods.net/showthread.php?p=2379856
Unless you are referring to that as a workaround.

However, returning arrays should be avoided in most cases as it introduces copies of the whole array, of course affecting performance, especially for large arrays.

Last edited by klippy; 06-23-2017 at 10:10.
klippy is offline
Craxor
Veteran Member
Join Date: Jan 2016
Location: Romania
Old 06-23-2017 , 10:11   Re: [ H3LP ] Can't return string on functions?
Reply With Quote #12

Because is to lazy to use charsmax
__________________
Project: Among Us
Craxor is offline
Send a message via ICQ to Craxor
PRoSToTeM@
Veteran Member
Join Date: Jan 2010
Location: Russia, Ivanovo
Old 06-23-2017 , 11:22   Re: [ H3LP ] Can't return string on functions?
Reply With Quote #13

Quote:
Originally Posted by KliPPy View Post
https://forums.alliedmods.net/showthread.php?p=2379856
Unless you are referring to that as a workaround.
I mean plugin natives.
Quote:
Originally Posted by KliPPy View Post
However, returning arrays should be avoided in most cases as it introduces copies of the whole array, of course affecting performance, especially for large arrays.
It works without copying if it is inlined like this:
Code:
server_print("%s", GetPlayerName(player));
__________________
PRoSToTeM@ is offline
Send a message via ICQ to PRoSToTeM@ Send a message via Skype™ to PRoSToTeM@
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 06-23-2017 , 11:39   Re: [ H3LP ] Can't return string on functions?
Reply With Quote #14

Quote:
Originally Posted by PRoSToTeM@ View Post
It works without copying if it is inlined like this:
Code:
server_print("%s", GetPlayerName(player));
To me it looks like it still copies 1 time (out of the called function), just tried it. When assigning it copies 2 times. Try using amxxdump and check it out. It's always faster to just pass a reference.

Last edited by klippy; 06-23-2017 at 11:41.
klippy is offline
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 06-23-2017 , 11:43   Re: [ H3LP ] Can't return string on functions?
Reply With Quote #15

Quote:
Originally Posted by Craxor View Post
Because is to lazy to use charsmax
Ah haha
__________________








CrazY. is offline
Craxor
Veteran Member
Join Date: Jan 2016
Location: Romania
Old 06-23-2017 , 11:52   Re: [ H3LP ] Can't return string on functions?
Reply With Quote #16

He say 'it works' not that is the better way to do that.
__________________
Project: Among Us
Craxor is offline
Send a message via ICQ to Craxor
PRoSToTeM@
Veteran Member
Join Date: Jan 2010
Location: Russia, Ivanovo
Old 06-23-2017 , 12:03   Re: [ H3LP ] Can't return string on functions?
Reply With Quote #17

Quote:
Originally Posted by KliPPy View Post
To me it looks like it still copies 1 time (out of the called function)
Yes if it is a plugin function, but fmt writes directly to this array.
Quote:
Originally Posted by KliPPy View Post
When assigning it copies 2 times.
Yes, we need to introduce an optimization for this.

GetPlayerName without copying:
Code:
#include <amxmodx> #if !defined MAX_NAME const MAX_NAME = 32; #endif const MaxNameLength = MAX_NAME - 1; public client_connect(player) {     server_print("%s connecting", GetPlayerName(player)); } native get_user_name2(a, b, c) = get_user_name; GetPlayerName(player) {     static returnStringAddr; #emit LOAD.S.PRI 0x10 #emit STOR.PRI returnStringAddr         get_user_name2(player, returnStringAddr, MaxNameLength); #emit RETN         new name[MaxNameLength + 1];     return name; }
__________________

Last edited by PRoSToTeM@; 06-23-2017 at 12:27.
PRoSToTeM@ is offline
Send a message via ICQ to PRoSToTeM@ Send a message via Skype™ to PRoSToTeM@
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-24-2017 , 02:34   Re: [ H3LP ] Can't return string on functions?
Reply With Quote #18

Basically, if you are not an advanced programmer who know how the back end of the language works then you should never return an array or a string. So, in general, always do arrays by reference as arguments of the function.

There is a reason that ALL AMX Mod X functions that "return" a string use the string as an argument and not a return value.
__________________
fysiks is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 07-05-2017 , 05:14   Re: [ H3LP ] Can't return string on functions?
Reply With Quote #19

I will quote fysiks about returning arrays.
Quote:
Originally Posted by fysiks View Post
Basically, if you are not an advanced programmer who know how the back end of the language works then you should never return an array or a string. So, in general, always do arrays by reference as arguments of the function.

There is a reason that ALL AMX Mod X functions that "return" a string use the string as an argument and not a return value.
__________________

Last edited by HamletEagle; 07-05-2017 at 05:14.
HamletEagle 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 06:33.


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