Natives can return arrays in PAWN!
1 Attachment(s)
Hello there! :grrr:
I've got something really interesting for ya! One syntactic feature of PAWN, that I am sure almost nobody is aware of, is that natives can indeed return arrays! Try compiling the following code: PHP Code:
I knew this works for quite some time now, but I never really knew how to implement it. I've been searching on Google last year, and I really never found an answer to that. That's why today I decided to do research on my own, and I got some interesting results to show to you! So, let's get into it. We will test this functionality using the following plugin: PHP Code:
PHP Code:
Code:
0x90 PROC ; public cmdName(id) PHP Code:
If we analyze plugin's stack at the moment our native gets called, it should look similar this: Code:
<LOWER MEMORY ADDRESS> If we had a native that accepts variable number of arguments (like server_print() for example), the pointer would be located at Code:
params[(params[0] / sizeof(cell)) + 1] http://i.imgur.com/CHDZNj4.jpg Module source code attached. |
Re: Natives can return arrays in PAWN!
I don't think it is wise to introduce something like this into AMX Mod X. It will just make it more complicated for people to learn how to write plugins especially if they are not programmers by trade. Basically, it adds unneeded complexity to the syntax.
Also, you would need to make sure that you size "playerName" with NAME_LENGTH instead of hardcoding it to 32. |
Re: Natives can return arrays in PAWN!
Quote:
Quote:
|
Re: Natives can return arrays in PAWN!
Quote:
new playerName[32]; should be new playerName[NAME_LENGTH]; |
Re: Natives can return arrays in PAWN!
Quote:
|
Re: Natives can return arrays in PAWN!
So there's no risk of memory leak? I see the dynamic memory gets freed at end.
|
Re: Natives can return arrays in PAWN!
Quote:
|
Re: Natives can return arrays in PAWN!
Spoiler
|
Re: Natives can return arrays in PAWN!
This definitely is useful. I find it more useful when knowing that some default AMXX natives can indeed use this.
For example *_to_str natives SHOULD return array/string instead. Sure it might be "limited" (if that's the right term) but still there would be no need to declare a string and use them (which is a bit annoying). |
Re: Natives can return arrays in PAWN!
This is pretty cool.
|
All times are GMT -4. The time now is 02:38. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.