AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   [TUT] The Use of Stocks (https://forums.alliedmods.net/showthread.php?t=40341)

Hawk552 06-26-2006 09:17

[TUT] The Use of Stocks
 
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.

nightscreem 06-26-2006 09:26

Re: The Use of Stocks
 
nice tut but so there is no diffrents with
Code:
fnDoFunc()
or
Code:
stock fnDoFunc()

Hawk552 06-26-2006 09:29

Re: The Use of Stocks
 
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.

Greenberet 06-26-2006 10:15

Re: The Use of Stocks
 
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

Hawk552 06-26-2006 10:26

Re: The Use of Stocks
 
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.

Greenberet 06-26-2006 10:43

Re: The Use of Stocks
 
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 ;)

BAILOPAN 06-26-2006 12:55

Re: The Use of Stocks
 
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).

Hawk552 06-26-2006 14:14

Re: The Use of Stocks
 
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).

drekes 04-18-2010 20:01

Re: [TUT] The Use of Stocks
 
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
}


AntiBots 04-18-2010 20:43

Re: [TUT] The Use of Stocks
 
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.


All times are GMT -4. The time now is 05:41.

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