SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 13/01/2018, 05:20 PM   #731
Yashas
Gangsta
 
Join Date: Jun 2012
Location: India
Posts: 868
Reputation: 295
Default Re: Today I learned - Share your newly found knowledge!

Destructors in PAWN

This is not documented in the PAWN Language Guide which is why this feature was unknown for quite a long time (11 years?). The implementer guide hints about destructors at few places though.

Declaration of destructors:
Code:
operator~(MyTag:arr[], size) { }
At the first glance, it feels like the destructor works only with arrays. However, this is not true. The above function signature is applicable to both arrays and variables.

The first parameter is essentially behaves as a reference variable in case of variables being destroyed and as a reference array in case of arrays. This means that you can know where the object was stored in memory. The tag of this parameter participates in overload resolution.

The second parameter provides the size of the first parameter. The tag of this parameter is ignored during overload resolution. By that, I mean to say that: if you have `new arr[tag1:10], arr2[tag2:20];", the same destructor will be called for both. In fact, you cannot create two destructors that differ only in the tag of the this parameter.

The destructor cannot have a return type. The compilation will fail if all the dimensions of an array (which has a destructor) is not explicitly given during declaration (even if all the sizes can be determined at compile-time).

Examples:
Code:
#include <a_samp>
operator~(Tag:arr[], size) {
	new address;
	#emit LOAD.S.pri arr
	#emit STOR.S.pri address
	printf("Destructor Called: address:%d, first-value:%d, size:%d", address, _:arr[0], size);
}

main ()
{
     new Tag:arr[] = {Tag:1, Tag:2, Tag:3};
     {
          new Tag:var;
     }
}
will result in:

Code:
Destructor Called: address:16584, first-value:0, size:1
Destructor Called: address:16588, first-value:1, size:3
Code:
#include <a_samp>
operator~(Tag:arr[], size) {
	new address;
	#emit LOAD.S.pri arr
	#emit STOR.S.pri address
	printf("Destructor Called: address:%d, first-value:%d, size:%d", address, _:arr[0], size);
}

func(&Tag:a, Tag:b = Tag:5) {
     return;
}
main ()
{
     new Tag:x = Tag:10;
     func(x);
     printf("end of main");
}
will give:

Code:
Destructor Called: address:16628, first-value:5, size:1
end of main
Destructor Called: address:16632, first-value:10, size:1
Multi-dimensional arrays
The destructor is not called for multi-dimensional arrays in the default PAWN compiler.

The Zeex compiler calls the destructor for the data of the array. The first parameter of the destructor will point to the start of the data of the array and the second parameter will provide the total number of elements in the array. The entire multi-dimensional array has to be accessed as if it were a single dimensional array.

[FIXES PENDING]

Order of destruction:
The destructors are called in the reverse order of declaration (most recently declared will be destroyed first).

Code:
new Tag:a = 2, Tag2:b = 5;
{
	   new Tag3:c;
}
"c" will be destroyed first, "b" will be destroyed next and "a" finally.

Notes:
  1. destructors are not called for global variables
  2. destructors are called for variables and arrays only (they are not called for references or array references)

Bugs:
  1. destructors are called for local static variables every time the function is called (it shouldn't be called at all given that destructors are not called for globals) Issue at Zeex's compiler Repo
  2. destructors are called for arguments only at explicit returns, i.e: destructors for arguments are not called unless you add a return statement at the end of the function
    Issue at Zeex's compiler Repo
  3. destructors are not called for multi-dimensional arrays in the default compiler and are called wrongly in the Zeex compiler

[PR for Zeex's Compiler aiming to resolve all of them]

Last edited by Yashas; 15/01/2018 at 12:20 PM.
Yashas is offline   Reply With Quote
Old 13/01/2018, 09:21 PM   #732
Renan157
Little Clucker
 
Renan157's Avatar
 
Join Date: Dec 2017
Posts: 37
Reputation: 0
Default Re: Today I learned - Share your newly found knowledge!

Mt Grande deu até Preguiça de ler
__________________
Novo No Scripiter
Membro Iniciante
Renan157 is offline   Reply With Quote
Old 13/01/2018, 09:38 PM   #733
RogueDrifter
High-roller
 
RogueDrifter's Avatar
 
Join Date: Dec 2017
Location: SA-MP Drifting world.
Posts: 1,144
Reputation: 345
Default Re: Today I learned - Share your newly found knowledge!

here's what i learned today, don't base any of your scripts on a samp update until its out of RC.
__________________
Quote:
I'm not using the forum to release stuff anymore, if you want updates for my releases or to check my new releases check the links below >
Quote:
Originally Posted by Escobar ⚠
Life sucked me in and shat me on the cruel world.

[Github]:Link [Gists]:Link [Forum]:Link [Server]:Link [Discord]:Link


RogueDrifter is offline   Reply With Quote
Old 13/01/2018, 10:24 PM   #734
Kaperstone
Banned
 
Join Date: May 2011
Location: Russia
Posts: 3,346
Reputation: 824
Default Re: Today I learned - Share your newly found knowledge!

Quote:
Originally Posted by RogueDrifter View Post
here's what i learned today, don't base any of your scripts on a samp update until its out of RC.
real knowledge ( ͡° ͜ʖ ͡°)
Kaperstone is offline   Reply With Quote
Old 13/01/2018, 11:00 PM   #735
Xeon™
Banned
 
Join Date: Jul 2017
Location: Earth
Posts: 498
Reputation: 96
Default Re: Today I learned - Share your newly found knowledge!

Quote:
Originally Posted by RogueDrifter View Post
here's what i learned today, don't base any of your scripts on a samp update until its out of RC.
so much knowledge, such a genius.
even Albert Einstein didn't think about this.
Xeon™ is offline   Reply With Quote
Old 13/01/2018, 11:05 PM   #736
Jeroen52
Gangsta
 
Jeroen52's Avatar
 
Join Date: Oct 2008
Location: The Netherlands
Posts: 866
Reputation: 71
Default Re: Today I learned - Share your newly found knowledge!

Quote:
Originally Posted by RogueDrifter View Post
here's what i learned today, don't base any of your scripts on a samp update until its out of RC.
Just make a branch, nerd-o.
__________________
Jeroen52 is offline   Reply With Quote
Old 14/01/2018, 12:26 AM   #737
DRIFT_HUNTER
High-roller
 
Join Date: Oct 2009
Posts: 2,143
Reputation: 172
Default Re: Today I learned - Share your newly found knowledge!

Quote:
Originally Posted by Jeroen52 View Post
Just make a branch, nerd-o.
Only few people here use versioning, and even less that use it the right way.
__________________
Путин here,
Путин there,
Путин просто everywhere.


Any PM's that include question about any kind of help will be ignored.
Use appropriate boards for that
DRIFT_HUNTER is offline   Reply With Quote
Old 14/01/2018, 12:54 AM   #738
Jeroen52
Gangsta
 
Jeroen52's Avatar
 
Join Date: Oct 2008
Location: The Netherlands
Posts: 866
Reputation: 71
Default Re: Today I learned - Share your newly found knowledge!

Quote:
Originally Posted by DRIFT_HUNTER View Post
Only few people here use versioning, and even less that use it the right way.
Looks like right now is the wrong moment to learn, however saying that it is right to learn it anytime.


For those wondering: DOWNLOAD GIT AND LEARN IT.
__________________
Jeroen52 is offline   Reply With Quote
Old 16/01/2018, 06:06 AM   #739
Mobtiesgangsa
Big Clucker
 
Mobtiesgangsa's Avatar
 
Join Date: Oct 2016
Location: Skopje, Macedonia
Posts: 118
Reputation: 10
Default Re: Today I learned - Share your newly found knowledge!

Today i learn'd
this - here code and logs....

Code:
if(mysql_errno(g_handle) != 0) // either connected or failed to connect
	{
		print("[MySQL] Failed the connection to \"phpMyAdmin\".\n");
		SendRconCommand("exit");
	}
	else
	{
	    print("[MySQL] Successfully made a connection to \"phpMyAdmin\".\n");
	}
using the latest MySQL version i was checking when the MySQL was connected or failed to make connection

R41-4

log when failed to connect to mysql phpmyadmin

HTML Code:
----------
Loaded log file: "server_log.txt".
----------

SA-MP Dedicated Server
----------------------
v0.3.7-R2, (C)2005-2015 SA-MP Team

[07:58:30] filterscripts = ""  (string)
[07:58:30] 
[07:58:30] Server Plugins
[07:58:30] --------------
[07:58:30]  Loading plugin: mysql
[07:58:30]  >> plugin.mysql: R41-4 successfully loaded.
[07:58:30]   Loaded.
[07:58:30]  Loaded 1 plugins.

[07:58:31] 
[07:58:31] Filterscripts
[07:58:31] ---------------
[07:58:31]   Loaded 0 filterscripts.

[07:58:35] [MySQL] Failed the connection to "phpMyAdmin".

[07:58:35] Number of vehicle models: 0
[07:58:35] --- Server Shutting Down.
[07:58:35] plugin.mysql: Unloading plugin...
[07:58:35] plugin.mysql: Plugin unloaded.
logs that successfully connected to mysql phpmyadmin

HTML Code:
----------
Loaded log file: "server_log.txt".
----------

SA-MP Dedicated Server
----------------------
v0.3.7-R2, (C)2005-2015 SA-MP Team

[07:59:05] filterscripts = ""  (string)
[07:59:05] 
[07:59:05] Server Plugins
[07:59:05] --------------
[07:59:05]  Loading plugin: mysql
[07:59:06]  >> plugin.mysql: R41-4 successfully loaded.
[07:59:06]   Loaded.
[07:59:06]  Loaded 1 plugins.

[07:59:06] 
[07:59:06] Filterscripts
[07:59:06] ---------------
[07:59:06]   Loaded 0 filterscripts.

[07:59:06] [MySQL] Successfully made a connection to "phpMyAdmin".

[07:59:06] Number of vehicle models: 0
__________________


So can i earn money
Mobtiesgangsa is offline   Reply With Quote
Old 23/01/2018, 04:31 PM   #740
GaByM
Big Clucker
 
GaByM's Avatar
 
Join Date: Jan 2014
Location: Romania
Posts: 112
Reputation: 10
Default Re: Today I learned - Share your newly found knowledge!

TIL this:

Code:
main()
{
	func("Hello\0        ");
	return 1;
}
func(str[])
{
	strcat(str, " world!", 15);
	print(str);
	return 1;
}
You now can use strcat with a constant string (idk if i'm correct for this name) passed as parameter.

A better example:

Code:
CMD:sellcar(playerid, params[])
{
	ShowPlayerSafeDialog(playerid,
		"Are you sure you want to sell your vehicle?\0 Everything here will be ignored, this is just to create a longer string");
	return 1;
}
ShowPlayerSafeDialog(playerid, info[])
{
	strcat(info, "\n\nType your password to continue:", 117);
	ShowPlayerDialog(playerid, 1, DIALOG_STYLE_INPUT, " ", info, "Continue", "Cancel");
	return 1;
}
This isn't quite good because sizeof() doesn't work on this strings but it is still interesting.
GaByM is offline   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 07:07 AM
How you learned to script Bam23 Help Archive 9 11/04/2011 10:21 AM
What did u learned today ? rafay Everything and Nothing 170 30/03/2010 04:06 PM


All times are GMT. The time now is 11:11 PM.


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