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

[TUT] The Use of Stocks


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 06-26-2006 , 09:17   [TUT] The Use of Stocks
Reply With Quote #1

What is a stock? Many people don't understand at all, and that's why I'm writing this.

A stock is basically a function or variable that, if not used anywhere in the script, is simply ignored by the compiler.

Many people have the idea of doing this:

Code:
#include <amxmodx> #include <amxmisc> public plugin_init() {     register_plugin("The","Matrix","Has You")         fnDoFunc() } stock fnDoFunc()     log_amx("the world has imploded upon itself")

When in fact you could achieve the same functionality through using this:

Code:
#include <amxmodx> #include <amxmisc> public plugin_init() {     register_plugin("The","Matrix","Has You")         fnDoFunc() } fnDoFunc()     log_amx("the world has imploded upon itself")

So why are stocks used? Mostly for include files, where there are many functions and variables that are not used.

Taken straight from engine_stocks.inc:

Code:
stock fakedamage(idvictim,szClassname[],Float:takedmgdamage,damagetype) {     new entity = create_entity("trigger_hurt")     if (entity)     {         DispatchKeyValue(entity,"classname","trigger_hurt")         new szDamage[16]         // Takedamages only do half damage per attack (damage is damage per second, and it's triggered in 0.5 second intervals).         // Compensate for that.         format(szDamage,15,"%f",takedmgdamage * 2)         DispatchKeyValue(entity,"dmg",szDamage)         format(szDamage,15,"%i",damagetype)         DispatchKeyValue(entity,"damagetype",szDamage)         DispatchKeyValue(entity,"origin","8192 8192 8192")         DispatchSpawn(entity)         entity_set_string(entity, EV_SZ_classname, szClassname)         fake_touch(entity,idvictim)         remove_entity(entity)         return 1     }     return 0 } //wrapper for find_ent_by_class stock find_ent(iStart, szClassname[]) {     return find_ent_by_class(iStart, szClassname) }

So, let's say I use find_ent somewhere in my script (which I am not condoning, it is a deprecated function for backward compatibility with AMX). The find_ent function will be included in the overhead and execution of my script, but fakedamage will not be because it was not used anywhere.

Stocks, as said twice above, can also be used on variables as such:

Code:
stock const mystring[] = "hello pm"

Again, including this in your script is useless, but it is useful for an include file where a variable may or may not be used, without using a #define due to issues with pushing it onto the stack each time it is used.

That's basically what stocks are and why they are useful.
__________________

Last edited by Brad; 06-26-2006 at 09:21. Reason: snipped immature and useless adjective
Hawk552 is offline
Send a message via AIM to Hawk552
nightscreem
Veteran Member
Join Date: Jul 2004
Location: Belgium
Old 06-26-2006 , 09:26   Re: The Use of Stocks
Reply With Quote #2

nice tut but so there is no diffrents with
Code:
fnDoFunc()
or
Code:
stock fnDoFunc()
__________________
- Bye bye!
nightscreem is offline
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 06-26-2006 , 09:29   Re: The Use of Stocks
Reply With Quote #3

Quote:
Originally Posted by nightscreem
nice tut but so there is no diffrents with
Code:
fnDoFunc()

or Code:
stock fnDoFunc()
I can't confirm any of what I say below here, but based on my experience it is as follows:

Not quite, I should clarify that the stock is public but the private function is not.

If you use the stock outside and inside of the plugin, then make it public (ex. if you use it in set_task or query_client_cvar)

However, if you just use it inside the plugin, you should always avoid using public or stock and instead just make it a private function.

EDIT: What I said here is wrong, stocks are private.
__________________

Last edited by Hawk552; 06-26-2006 at 14:15.
Hawk552 is offline
Send a message via AIM to Hawk552
Greenberet
AMX Mod X Beta Tester
Join Date: Apr 2004
Location: Vienna
Old 06-26-2006 , 10:15   Re: The Use of Stocks
Reply With Quote #4

Quote:
Originally Posted by Hawk552
Code:
stock const mystring[] = "hello pm"

Again, including this in your script is useless
WRONG!!!!
stock variables are usefull!

When you create big plugins, you declare most of the global variables at the begin.
So you have many gobal variables but you aren't using all of them from begin.
So when you compile it, you will get many warnings( "warning 203: symbol is never used" )
But with stock variables you will not get this warning, because the variables will not be created
Greenberet is offline
Send a message via ICQ to Greenberet Send a message via MSN to Greenberet
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 06-26-2006 , 10:26   Re: The Use of Stocks
Reply With Quote #5

Quote:
Originally Posted by Greenberet
WRONG!!!!
stock variables are usefull!

When you create big plugins, you declare most of the global variables at the begin.
So you have many gobal variables but you aren't using all of them from begin.
So when you compile it, you will get many warnings( "warning 203: symbol is never used" )
But with stock variables you will not get this warning, because the variables will not be created
That's very arguable as I don't do that with large plugins I make (I make variables as I go along). I guess you could say that, but I disagree. If I need this functionality, I simply comment the declaration of the variable.
__________________
Hawk552 is offline
Send a message via AIM to Hawk552
Greenberet
AMX Mod X Beta Tester
Join Date: Apr 2004
Location: Vienna
Old 06-26-2006 , 10:43   Re: The Use of Stocks
Reply With Quote #6

then you use the variable and you get the error "error 017: undefined symbol".
But with a stock variable you will not have this problem ;)
Greenberet is offline
Send a message via ICQ to Greenberet Send a message via MSN to Greenberet
BAILOPAN
Join Date: Jan 2004
Old 06-26-2006 , 12:55   Re: The Use of Stocks
Reply With Quote #7

Stocks are not public. Furthermore, they are always parsed by the compiler ('stock' doesn't mean the compiler simply ignores it). The only difference between a stock and a normal function is that if it is unreferenced at assembly time, it is not included into the final code.

This is ideal for include files where users might not be using everything, and variables that might not be used (like Greenberet noted).
__________________
egg
BAILOPAN is offline
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 06-26-2006 , 14:14   Re: The Use of Stocks
Reply With Quote #8

Quote:
Originally Posted by BAILOPAN
Stocks are not public. Furthermore, they are always parsed by the compiler ('stock' doesn't mean the compiler simply ignores it). The only difference between a stock and a normal function is that if it is unreferenced at assembly time, it is not included into the final code.

This is ideal for include files where users might not be using everything, and variables that might not be used (like Greenberet noted).
Thanks, I wasn't sure about that. I saw someone use a set_task on a stock a while ago and the person who used it claimed that it worked.

I don't see anything else wrong that I said other than that, and nothing wrong in the main post, so I'm not going to go through the hassle of changing anything (since editing code is still broken).
__________________
Hawk552 is offline
Send a message via AIM to Hawk552
drekes
Veteran Member
Join Date: Jul 2009
Location: Vault 11
Old 04-18-2010 , 20:01   Re: [TUT] The Use of Stocks
Reply With Quote #9

sorry for reviving a 4 year old thread, but what is the best to use? cause i don't understand it.

this
Code:
stock something()
{
    more something
}
or this
Code:
something()
{
    more something
}
__________________

Quote:
Originally Posted by nikhilgupta345 View Post
You're retarded.
drekes is offline
Send a message via MSN to drekes
AntiBots
Veteran Member
Join Date: May 2008
Location: Brazil
Old 04-18-2010 , 20:43   Re: [TUT] The Use of Stocks
Reply With Quote #10

The only difference between a stock and a normal function is that if it is unreferenced at assembly time, it is not included into the final code.
__________________
AntiBots is offline
Send a message via ICQ to AntiBots Send a message via MSN to AntiBots Send a message via Skype™ to AntiBots
Reply


Thread Tools
Display Modes

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 04:38.


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