SA-MP Forums Archive
stock vs function - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: stock vs function (/showthread.php?tid=649579)



stock vs function - PepsiCola23 - 11.02.2018

whats the difference between

PHP код:
stock GetTruckNeedTracks(playerid)
{
    new 
truckneedtracks;
    new 
truckskill GetPlayerTruckSkill(playerid);
    if(
truckskill == 1truckneedtracks 35;
    else if(
truckskill == 2truckneedtracks 100;
    else if(
truckskill == 3truckneedtracks 220;
    else if(
truckskill == 4truckneedtracks 450;
    return 
truckneedtracks;

and

PHP код:
function GetTruckNeedTracks(playerid)
{
    new 
truckneedtracks;
    new 
truckskill GetPlayerTruckSkill(playerid);
    if(
truckskill == 1truckneedtracks 35;
    else if(
truckskill == 2truckneedtracks 100;
    else if(
truckskill == 3truckneedtracks 220;
    else if(
truckskill == 4truckneedtracks 450;
    return 
truckneedtracks;

which one should i use?


Re: stock vs function - Nero_3D - 11.02.2018

the keyword function doesn't exists, read this

https://sampwiki.blast.hk/wiki/Stock#stock


Re: stock vs function - Eoussama - 11.02.2018

You may also consider taking a look at this tutorial by Vince.


Re: stock vs function - PepsiCola23 - 11.02.2018

by function i meant
PHP код:
#define function%0(%1) forward %0(%1); public %0(%1) // Shortcut 
ni this case i should use function instead of stock?


Re: stock vs function - Misiur - 11.02.2018

There are three (simplified) scenarios:
1. You need a callback, or the function is called by a timer: use forward + public
2. You write a function in your gamemode. Do not use anything! Just write:
pawn Код:
GetTruckNeedTracks(playerid)
{
    new truckneedtracks;
    new truckskill = GetPlayerTruckSkill(playerid);
    if(truckskill == 1) truckneedtracks = 35;
    else if(truckskill == 2) truckneedtracks = 100;
    else if(truckskill == 3) truckneedtracks = 220;
    else if(truckskill == 4) truckneedtracks = 450;
    return truckneedtracks;
}
3. You are writing an include. Use stock.


Re: stock vs function - PepsiCola23 - 11.02.2018

and whats the difference?they both work,is it more optimized,less memory used or..what?


Re: stock vs function - Nero_3D - 11.02.2018

stock simple removes the variable / function from your code if not used at compilation, it could lead to useless code fragments in your code because the unused warning doesn't appear
a public variable / function instead is always included in your code even if not directly used, it also needs space in the header (it stores the address and the name)