AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved [ H3LP ] Can't return string on functions? (https://forums.alliedmods.net/showthread.php?t=298796)

CrazY. 06-22-2017 21:08

[ H3LP ] Can't return string on functions?
 
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?

EFFx 06-22-2017 22:49

Re: [ H3LP ] Can't return string on functions?
 
About the ';', it aint needed. Only pawner's style.

Craxor 06-22-2017 23:04

Re: [ H3LP ] Can't return string on functions?
 
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.

CrazY. 06-23-2017 00:40

Re: [ H3LP ] Can't return string on functions?
 
@EFFx, @Craxor, thanks!

Natsheh 06-23-2017 01:22

Re: [ H3LP ] Can't return string on functions?
 
Search about returning a string by reference...

Semicolons are just a coding style suppose to mean end of line...

CrazY. 06-23-2017 01:27

Re: [ H3LP ] Can't return string on functions?
 
Natsheh, I know about by-reference natives. Which is the fast? style=0 or style=1?

HamletEagle 06-23-2017 03:16

Re: [ H3LP ] Can't return string on functions?
 
You should always use style 0.

CrazY. 06-23-2017 09:06

Re: [ H3LP ] Can't return string on functions?
 
Ok Hamlet, thank you.

PRoSToTeM@ 06-23-2017 09:54

Re: [ H3LP ] Can't return string on functions?
 
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.

CrazY. 06-23-2017 10:04

Re: [ H3LP ] Can't return string on functions?
 
Code:
const MaxNameLength = MAX_NAME - 1;

Code:
static name[MaxNameLength + 1];

Why you decrease -1 and then add +1 again?


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

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