AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Return an array in stock (https://forums.alliedmods.net/showthread.php?t=302139)

Kowalsky 10-18-2017 04:45

Return an array in stock
 
Hey. First of all, thank you for explaining in details why should I use cstrike/fun/engine modules instead of converting them. I really did get the point and appreceate that you dealt with my narrow-mindness till the end :D

I wanted to ask some help regarding stocks. I want to create a stock that would do the following:

Code:
stock get_team_colours( id, rgb[ 3 ] ) {         if( cs_get_user_team( id ) == CS_TEAM_T ) {              rgb[ 0 ] = 255;              rgb[ 1 ] = 0;              rgb[ 2 ] = 75;         }         else //... return rgb; }

But this does not work. Could you please tell me how can I achieve the wanted result?

OciXCrom 10-18-2017 06:14

Re: Return an array in stock
 
The stock already does what you want it to do (trust me, try it). Remove the return rgb; line and you're good to go.

fysiks 10-18-2017 09:10

Re: Return an array in stock
 
The important thing to understand here is that arrays are passed by reference aka 'byref' in this language.

That means that when you manipulate an array variable inside your function, it actually changes the variable in the calling code which was provided as the argument.

siriusmd99 10-18-2017 09:14

Re: Return an array in stock
 
In amxx it is not possible to return an array or a string (basically in amxx string is an array of integers that actually can also be understood as chars).

So in this case you have just to use the function with an empty array as input. And then function will just save the rgb values directly in that array.

This will your stock look like:

PHP Code:

stock get_team_coloursidrgb] ) {
        if( 
cs_get_user_teamid ) == CS_TEAM_T ) {
             
rgb] = 255;
             
rgb] = 0;
             
rgb] = 75;
        }


And this way you call the function:

PHP Code:

new myRGB[3]

get_team_coloursidmyRGB 

After calling the function, myRGB array will have your desired values.

Kowalsky 10-18-2017 10:13

Re: Return an array in stock
 
Understood! Thank you

OciXCrom 10-18-2017 12:36

Re: Return an array in stock
 
Quote:

In amxx it is not possible to return an array or a string
You can return a string.

Natsheh 10-18-2017 12:46

Re: Return an array in stock
 
But returning a string is bad best is to copy it byref

klippy 10-18-2017 13:04

Re: Return an array in stock
 
Quote:

Originally Posted by Natsheh (Post 2555233)
But returning a string is bad best is to copy it byref

That's true, it's more efficient and flexible (doesn't have to be constant size). However, it's good that OcixCrom posted because we shouldn't spread false information.

KiLLeR. 10-18-2017 14:35

Re: Return an array in stock
 
Yeah, I know it's possible to return string, but still I can't understand why is not a good idea?! Can anyone explain please? :p

HamletEagle 10-18-2017 15:04

Re: Return an array in stock
 
Quote:

Originally Posted by KiLLeR. (Post 2555250)
Yeah, I know it's possible to return string, but still I can't understand why is not a good idea?! Can anyone explain please? :p

I'm pretty sure someone(Klippy? PRoSToTeM@?) posted a good explanations about that. You should try searching.


All times are GMT -4. The time now is 20:15.

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