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, 06:20 PM   #731
Yashas
Gangsta
 
Join Date: Jun 2012
Location: India
Posts: 888
Reputation: 290
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 01:20 PM.
Yashas is offline   Reply With Quote
Old 13/01/2018, 10:21 PM   #732
Renan157
Little Clucker
 
Renan157's Avatar
 
Join Date: Dec 2017
Posts: 40
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, 10:38 PM   #733
RogueDrifter
Gangsta
 
RogueDrifter's Avatar
 
Join Date: Dec 2017
Location: SA-MP Drifting world.
Posts: 765
Reputation: 78
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.
RogueDrifter is offline   Reply With Quote
Old 13/01/2018, 11:24 PM   #734
Kaperstone
High-roller
 
Kaperstone's Avatar
 
Join Date: May 2011
Location: Vladivostok
Posts: 3,269
Reputation: 747
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 ( ͡° ͜ʖ ͡°)
__________________
Find me on GitHubSEF Community (Official NEF Successor) • SALobby Community • SA:MP Reviews • Find me on Steam
Kaperstone is offline   Reply With Quote
Old 14/01/2018, 12:00 AM   #735
Xeon™
Banned
 
Join Date: Jul 2017
Location: Earth
Posts: 536
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 14/01/2018, 12:05 AM   #736
Jeroen52
Gangsta
 
Jeroen52's Avatar
 
Join Date: Oct 2008
Location: The Netherlands
Posts: 828
Reputation: 66
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, 01:26 AM   #737
DRIFT_HUNTER
High-roller
 
Join Date: Oct 2009
Posts: 2,142
Reputation: 157
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, 01:54 AM   #738
Jeroen52
Gangsta
 
Jeroen52's Avatar
 
Join Date: Oct 2008
Location: The Netherlands
Posts: 828
Reputation: 66
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, 07:06 AM   #739
Mobtiesgangsa
Little Clucker
 
Mobtiesgangsa's Avatar
 
Join Date: Oct 2016
Location: Skopje, Macedonia
Posts: 38
Reputation: 3
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
__________________
As once told a programmer that in case your written code has a problem to not worry cuz one day you will figure a solution on how to overcome your problem
Mobtiesgangsa 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 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 04:58 PM.


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