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

Solved stock vs functions


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
shavit
AlliedModders Donor
Join Date: Dec 2011
Location: Israel
Old 10-04-2016 , 01:07   stock vs functions
Reply With Quote #1

In what cases would the use of stock benefit me over the use of a function, if both do the exact same thing?

Sorry for the stupid question, I'm trying to optimize a few plugins and figured this might be a problem in OnGameFrame or OnPlayerRunCmd if functions will be more expensive in terms of resource hogging
__________________
retired

Last edited by shavit; 10-04-2016 at 03:26.
shavit is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 10-04-2016 , 01:08   Re: stock vs functions
Reply With Quote #2

stock functions are functions.

Can you give more detail as to what exactly you mean?
__________________

Last edited by ddhoward; 10-04-2016 at 01:10.
ddhoward is offline
shavit
AlliedModders Donor
Join Date: Dec 2011
Location: Israel
Old 10-04-2016 , 01:11   Re: stock vs functions
Reply With Quote #3

Quote:
Originally Posted by ddhoward View Post
stock functions are functions.

Can you give more detail as to exactly you mean?
I'm probably wrong, but what I remember is stock essentially 'pasting' the lines of code inside the stock to where ever I use it, so I assume that calling a function will take more time to process on the server side
__________________
retired
shavit is offline
Impact123
Veteran Member
Join Date: Oct 2011
Location: Germany
Old 10-04-2016 , 01:12   Re: stock vs functions
Reply With Quote #4

This has recently been discussed in this thread.
__________________
Impact123 is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 10-04-2016 , 01:14   Re: stock vs functions
Reply With Quote #5

You are confusing stock and #define.

Labeling a function as "stock" merely tells the compiler to NOT compile the function into the plugin if the function isn't called anywhere. This is useful especially in include files where the plugin author may not have a need for every single function within the include.

#define'd functions, also called macro-functions, involve the compiler copy/pasting code. This functionality is slated for removal in future versions of Sourcemod, and should not be used.
__________________
ddhoward is offline
shavit
AlliedModders Donor
Join Date: Dec 2011
Location: Israel
Old 10-04-2016 , 01:18   Re: stock vs functions
Reply With Quote #6

Quote:
Originally Posted by Impact123 View Post
This has recently been discussed in this thread.
Quote:
Originally Posted by ddhoward View Post
You are confusing stock and #define.

Labeling a function as "stock" merely tells the compiler to NOT compile the function into the plugin if the function isn't called anywhere. This is useful especially in include files where the plugin author may not have a need for every single function within the include.

#define'd functions, also called macro-functions, involve the compiler copy/pasting code. This functionality is slated for removal in future versions of Sourcemod, and should not be used.
Thanks for explaining!

Another thing, in SourcePawn, if I don't add the public prefix, what does it actually change? We don't have object-orientation yet so I'm confused
__________________
retired
shavit is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 10-04-2016 , 01:21   Re: stock vs functions
Reply With Quote #7

A function that is not marked as "public" cannot be directly called by Sourcemod; only other functions within the plugin can call the function.

Timers, forwards like OnGameFrame()/OnClientConnect()/OnPluginStart(), or players using commands, are all things that require Sourcemod to be able to run a function at the given moment. Hence, these functions should all be marked as public by the plugin author.

https://wiki.alliedmods.net/Introduc...wn#Functions_2
__________________

Last edited by ddhoward; 10-04-2016 at 01:26.
ddhoward is offline
shavit
AlliedModders Donor
Join Date: Dec 2011
Location: Israel
Old 10-04-2016 , 01:27   Re: stock vs functions
Reply With Quote #8

Quote:
Originally Posted by ddhoward View Post
A function that is not marked as "public" cannot be directly called by Sourcemod; only other functions within the plugin can call the function.

Timers, forwards like OnGameFrame()/OnClientConnect()/OnPluginStart(), or players using commands, are all things that require Sourcemod to be able to run a function at the given moment. Hence, these functions should all be marked as public by the plugin author.

https://wiki.alliedmods.net/Introduc...wn#Functions_2
So unless it's a global forward I don't need to add public, correct?
__________________
retired
shavit is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 10-04-2016 , 01:43   Re: stock vs functions
Reply With Quote #9

There is no reason to add public to a function's header unless the function needs to be called by something outside of the plugin. If the function only ever runs because it's being called by other functions within the same plugin, then leave it out.
__________________

Last edited by ddhoward; 10-04-2016 at 01:46.
ddhoward is offline
shavit
AlliedModders Donor
Join Date: Dec 2011
Location: Israel
Old 10-04-2016 , 02:57   Re: stock vs functions
Reply With Quote #10

Quote:
Originally Posted by ddhoward View Post
There is no reason to add public to a function's header unless the function needs to be called by something outside of the plugin. If the function only ever runs because it's being called by other functions within the same plugin, then leave it out.
Thanks!
__________________
retired
shavit 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:43.


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