SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Discussion

Reply
 
Thread Tools Display Modes
Old 23/01/2018, 07:20 PM   #741
Eoussama
High-roller
 
Eoussama's Avatar
 
Join Date: Jul 2016
Location: Kingdom of Morocco // Tangier
Posts: 1,188
Reputation: 174
Default Re: Today I learned - Share your newly found knowledge!

Quote:
Originally Posted by GaByM View Post
TIL this:
This isn't quite good because sizeof() doesn't work on this strings but it is still interesting.
sizeof() counts characters and stops when it reaches the null terminator (0, '\0', EOS)
__________________
Eoussama is online now   Reply With Quote
Old 23/01/2018, 08:25 PM   #742
GaByM
Big Clucker
 
GaByM's Avatar
 
Join Date: Jan 2014
Location: Romania
Posts: 99
Reputation: 7
Default Re: Today I learned - Share your newly found knowledge!

Quote:
Originally Posted by Eoussama View Post
sizeof() counts characters and stops when it reaches the null terminator (0, '\0', EOS)
No, strlen returns the amount of characters inside a string and stops at '\0'. sizeof() returns the size of the array, so
Code:
str[128] = "Hello"; printf("%i", sizeof(str)); //will print 128
GaByM is offline   Reply With Quote
Old 23/01/2018, 09:21 PM   #743
Spmn
Gangsta
 
Join Date: Jun 2015
Location: Romania
Posts: 531
Reputation: 126
Default Re: Today I learned - Share your newly found knowledge!

You should never append to (or even edit) string literals. You will go out of bounds and most likely rewrite other literals.
Spmn is offline   Reply With Quote
Old 23/01/2018, 09:58 PM   #744
Dayrion
High-roller
 
Dayrion's Avatar
 
Join Date: Jan 2016
Location: France
Posts: 1,637
Reputation: 202
Default Re: Today I learned - Share your newly found knowledge!

Quote:
Originally Posted by GaByM View Post
TIL this:

You now can use strcat with a constant string (idk if i'm correct for this name) passed as parameter.

A better example:

This isn't quite good because sizeof() doesn't work on this strings but it is still interesting.
It can be easily bypassed.


PHP Code:
FunctionGetActorTextLabel(text[], length sizeof(text))
{
    
strcat("YEA ->"textlength);
    print(
text);

__________________
actor_plus - v4.1.2
I'm a goner, somebody catch my breath,
I'm a goner, somebody catch my breath
Dayrion is online now   Reply With Quote
Old 06/02/2018, 07:23 PM   #745
Freaksken
Gangsta
 
Freaksken's Avatar
 
Join Date: Mar 2009
Location: Belgium
Posts: 551
Reputation: 230
Default Re: Today I learned - Share your newly found knowledge!

TIL you can ignore parameters passed by reference (out parameters) by using a default value, just like you can with parameters passed by value (in parameters):

Code:
stock Float:DoSomething(playerid, &Float:a = 4.0) {
	a = 5.0;
	return 9.0;
}
The default value can be anything, since it will be ignored anyway.

Use:
Code:
DoSomething(0); //Returns 9.0

new Float:a;
DoSomething(0, a); //Returns 9.0 and variable 'a' contains 5.0
__________________
Always keep in mind that a lot of people are active on this forum in their spare time.
They are sacrificing time they could easily spend on things they would rather do, to help you instead.


[Include] FCNPC A.I. - [Tutorial] Speedometer - [WIP] Single-player-like Co-op Mission - [Other] List of all my work
Freaksken is offline   Reply With Quote
Old 06/02/2018, 08:49 PM   #746
ISmokezU
Gangsta
 
Join Date: Aug 2016
Posts: 542
Reputation: 70
Default Re: Today I learned - Share your newly found knowledge!

Quote:
Originally Posted by Freaksken View Post
TIL you can ignore parameters passed by reference (out parameters) by using a default value, just like you can with parameters passed by value (in parameters):

Code:
stock Float:DoSomething(playerid, &Float:a = 4.0) {
	a = 5.0;
	return 9.0;
}
The default value can be anything, since it will be ignored anyway.

Use:
Code:
DoSomething(0); //Returns 9.0

new Float:a;
DoSomething(0, a); //Returns 9.0 and variable 'a' contains 5.0
Great find!

Would expect a tag mismatch with this, but this is great.
ISmokezU is offline   Reply With Quote
Old 12/02/2018, 03:30 PM   #747
PepsiCola23
Gangsta
 
Join Date: May 2015
Posts: 513
Reputation: 2
Default Re: Today I learned - Share your newly found knowledge!

today i learned that there is not necessary to use
PHP Code:
stock 
in your gamemode.

for example,instead of
PHP Code:
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;

you can simply write
PHP Code:
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;

PepsiCola23 is online now   Reply With Quote
Old 12/02/2018, 05:19 PM   #748
Dayrion
High-roller
 
Dayrion's Avatar
 
Join Date: Jan 2016
Location: France
Posts: 1,637
Reputation: 202
Default Re: Today I learned - Share your newly found knowledge!

Quote:
Originally Posted by PepsiCola23 View Post
...
you can simply write
PHP Code:
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;

There is quick tutorial to explain when you can use the keyword stock: http://forum.sa-mp.com/showpost.php?...16&postcount=1
Now, you can learn something new. It's called "switch case" statement ( tutorial here!).
PHP Code:
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;

Become (with some tiny optimisations)
PHP Code:
GetTruckNeedTracks(playerid)
{
    switch(
GetPlayerTruckSkill(playerid))
    {
        case 
1: return 35;
        case 
2: return 100;
        case 
3: return 220;
        case 
4: return 450
    }
    return 
0;

__________________
actor_plus - v4.1.2
I'm a goner, somebody catch my breath,
I'm a goner, somebody catch my breath
Dayrion is online now   Reply With Quote
Old 12/02/2018, 09:26 PM   #749
PepsiCola23
Gangsta
 
Join Date: May 2015
Posts: 513
Reputation: 2
Default Re: Today I learned - Share your newly found knowledge!

Quote:
Originally Posted by Dayrion View Post
There is quick tutorial to explain when you can use the keyword stock: http://forum.sa-mp.com/showpost.php?...16&postcount=1
Now, you can learn something new. It's called "switch case" statement ( tutorial here!).
PHP Code:
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;

Become (with some tiny optimisations)
PHP Code:
GetTruckNeedTracks(playerid)
{
    switch(
GetPlayerTruckSkill(playerid))
    {
        case 
1: return 35;
        case 
2: return 100;
        case 
3: return 220;
        case 
4: return 450
    }
    return 
0;

Thanks you,i ll try using this method
PepsiCola23 is online now   Reply With Quote
Old 13/02/2018, 12:14 AM   #750
Eoussama
High-roller
 
Eoussama's Avatar
 
Join Date: Jul 2016
Location: Kingdom of Morocco // Tangier
Posts: 1,188
Reputation: 174
Default Re: Today I learned - Share your newly found knowledge!

Today I learned about a simple BATCH instruction that serves as a cool hack when testing/debugging.

1- First, open any text editor, and paste the following instructions
PHP Code:
@echo off

:s
samp
-server.exe
goto 
2- Save the file as something.bat
3- Run the .bat file

Now, whenever you change your code source and compile, no need to exit and re-execute your samp-server.exe, just type the exit command in the console, and it will automatically close and re-open it for you.
__________________

Last edited by Eoussama; 15/02/2018 at 12:01 AM.
Eoussama is online now   Reply With Quote
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Things you learned from GTA SA Toni Everything and Nothing 212 20/08/2011 08:07 AM
How you learned to script Bam23 Help Archive 9 11/04/2011 11:21 AM
What did u learned today ? rafay Everything and Nothing 170 30/03/2010 05:06 PM


All times are GMT. The time now is 08:49 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.