Raised This Month: $ Target: $400
 0% 

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


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
PRoSToTeM@
Veteran Member
Join Date: Jan 2010
Location: Russia, Ivanovo
Old 06-23-2017 , 09:54   Re: [ H3LP ] Can't return string on functions?
Reply With Quote #1

It works:
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)); } GetPlayerName(player) {     static name[MaxNameLength + 1];     get_user_name(player, name, charsmax(name));         return name; }
But natives don't support returning arrays, it can be done only with workarounds.
__________________

Last edited by PRoSToTeM@; 06-23-2017 at 09:54.
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 , 10:10   Re: [ H3LP ] Can't return string on functions?
Reply With Quote #2

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
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 #3

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 #4

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
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 #5

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@
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 23:12.


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