Raised This Month: $ Target: $400
 0% 

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


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 06-22-2017 , 21:08   [ H3LP ] Can't return string on functions?
Reply With Quote #1

Hello, why I can't return string on functions?

Example:

Code:
get_player_name(id) {     new szName[32]     get_user_name(id, szName, charsmax(szName))         return szName; }

Other little question, it's really needed add " ; " after all code if applicable?
__________________









Last edited by CrazY.; 06-23-2017 at 00:41.
CrazY. is offline
EFFx
Veteran Member
Join Date: Feb 2016
Location: São Paulo, Brasil
Old 06-22-2017 , 22:49   Re: [ H3LP ] Can't return string on functions?
Reply With Quote #2

About the ';', it aint needed. Only pawner's style.
__________________
• Ranking System • AutoMix 5vs5 System
• Web Ban System • Plugins for free

____________________________________________
For private works:
• Discord: EFFEXo#8850 • Steam: EFFEXo
EFFx is offline
Craxor
Veteran Member
Join Date: Jan 2016
Location: Romania
Old 06-22-2017 , 23:04   Re: [ H3LP ] Can't return string on functions?
Reply With Quote #3

Because it will return each char in part( like %s = (szName[0] && szName[1] && szName[2] && szName[3] ) it is very poor method to return arrays with natives, you should format or copy the output in a string variable.
PHP Code:
get_user_name2id, const Name[], maxchars=)
{
    
get_user_nameidNamemaxchars );

But is basicly the same, so only if you want to format the name with something special or maybe in other cases, i've answer to you why is not good returning arrays with natives.
__________________
Project: Among Us
Craxor is offline
Send a message via ICQ to Craxor
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 06-23-2017 , 00:40   Re: [ H3LP ] Can't return string on functions?
Reply With Quote #4

@EFFx, @Craxor, thanks!
__________________








CrazY. is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 06-23-2017 , 01:22   Re: [ H3LP ] Can't return string on functions?
Reply With Quote #5

Search about returning a string by reference...

Semicolons are just a coding style suppose to mean end of line...
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 06-23-2017 , 01:27   Re: [ H3LP ] Can't return string on functions?
Reply With Quote #6

Natsheh, I know about by-reference natives. Which is the fast? style=0 or style=1?
__________________








CrazY. is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 06-23-2017 , 03:16   Re: [ H3LP ] Can't return string on functions?
Reply With Quote #7

You should always use style 0.
__________________
HamletEagle is offline
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 06-23-2017 , 09:06   Re: [ H3LP ] Can't return string on functions?
Reply With Quote #8

Ok Hamlet, thank you.
__________________








CrazY. is offline
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 #9

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

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
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:11.


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