AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   [TUT] SourcePawn Scripting - Tips, Basics to Advanced (https://forums.alliedmods.net/showthread.php?t=321089)

Dragokas 03-24-2020 10:29

Re: [TUT] SourcePawn Scripting - Tips, Basics to Advanced
 
It seems doesn't required, according to this article: https://support.steampowered.com/kb_...8711&l=english

Dragokas 03-27-2020 12:42

Re: [TUT] SourcePawn Scripting - Tips, Basics to Advanced
 
1 Attachment(s)
Minimal sample "How to stop (delete) global timer"

I see a really lot of questions about:
Quote:

- Native "KillTimer" reported: Invalid timer handle XXX (error 3)
- Handle XXX is invalid (error 1)
- TIMER_FLAG_NO_MAPCHANGE doesn't stop timer when round ends/lost etc...
when somebody wants to start single or repeatable timer and stop it on round_end, manually or using TIMER_FLAG_NO_MAPCHANGE.

To do it without errors, you have to create timer correcly and nullify global variable in correct events.

Here are minimal correct samples with comments:

1. Global single timer (timer_single.sp)
Spoiler


2. Global repeatable timer (timer_repeat.sp)
Spoiler


3. Global single per-client timer (timer_single_per-client.sp)
Spoiler


4. Global repeatable per-client timer (timer_repeat_per-client.sp)
Spoiler


5. (No global var.) Pause repeatable timer
- without the need to manage handle in global variable (timer_repeat_pause.sp)
Spoiler


-------------------

General tips:
  • Do not use TIMER_FLAG_NO_MAPCHANGE flag if you assign timer's handle to a global variable to control the timer via this variable later.
    - TIMER_FLAG_NO_MAPCHANGE flag can be safely used if you create timer without assigning its handle to a variable.

  • When you kill timer outside the timer's callback, use 'delete' keyword.

  • When you kill repeatable timer within its own callback, use "return Plugin_Stop" only!
    - Do not use "delete" or "KillTimer()" there, otherwise you might receive error:
    Quote:

    Plugin "XXX.smx" encountered error 23: Native detected error
    [SM] Invalid timer handle XXX (error 3) during timer end, displayed function is timer callback, not the stack trace
  • You should always assign zero to timer's global variable in the very last line of timer's callback (before return) if timer's handle is about to destruct, that happen:
    - for repeatable timer, when you pass "return Plugin_Stop"
    - for single time, automatically when callback finishes.

    When your global variable is array-based and you don't know how to find it within timer's callback:
    - with per-client timers, use OnClientDisconnect() forward to do it.
    - with incremental based global timers:
    Spoiler

  • Delete timer before creating it to prevent it from firing twice, since some events like "round_start" could be fire multiple times.

  • To stop timer on round end, use both "round_end" event and "OnMapEnd()" forward, because "round_end" doesn't fire when map is forcibly changed.

Good luck!

P.S. If you found some grammar mistakes, please PM me.

Alienmario 03-29-2020 09:46

Re: [TUT] SourcePawn Scripting - Tips, Basics to Advanced
 
Some information on string tables would be useful, perhaps include how to dump info from here https://forums.alliedmods.net/showthread.php?t=250804

Dragokas 03-29-2020 10:45

Re: [TUT] SourcePawn Scripting - Tips, Basics to Advanced
 
Alienmario,

Released more user-friendly version: [DEV] String Tables Dumper

aleeexxx 04-21-2020 11:47

Re: [TUT] SourcePawn Scripting - Tips, Basics to Advanced
 
you could add the multithread functions recommendation for SQL

I until recently knew that the old functions could block the main thread, the new syntax already does it in multithread

correct me if I'm wrong!

Dragokas 05-03-2020 08:24

Re: [TUT] SourcePawn Scripting - Tips, Basics to Advanced
 
Quote:

Originally Posted by aleeexxx (Post 2695091)
you could add the multithread functions recommendation for SQL

There are actually no so-called multi-threaded functions.

There are only:
  • non-threaded (executed in the same thread as sm plugins, so each query to db cause waiting the response and pausing other plugin's operations during this time).
  • threaded (this one executes db queries in other second thread; when you execute one more query in parallel, no more threads created, it queues to the same second thread).

Quote:

Originally Posted by aleeexxx (Post 2695091)
I until recently knew that the old functions could block the main thread, the new syntax already does it in multithread

correct me if I'm wrong!

Not all of them.
Some of Database methodmap functions still point to the old non-threded functions at the moment.

Example: Database.SetCharset() - it is just an alias to SQL_SetCharset, see source code.

There are also some exclusions.
Example: Database.Escape() - even if according to source code, it access the db handle, no external query is processed. Instead, client makes escaping itself based on pre-cached codepage of connection.

Most of above info is provided by Kruzya (CrazyHackGUT).
Thanks to him.

Additional info can be obtained in this article (in Russian) by R1KO.
Reading comments would be also useful.

Dragokas 07-15-2020 14:49

Re: [TUT] SourcePawn Scripting - Tips, Basics to Advanced
 
Some updates I prepared long time ago:

Code:

AddAmbientSoundHook(AmbientHoo));
=>
AddAmbientSoundHook(AmbientHook);

+
Code:

#include <sourcemod>
#include <sdktools>
#include <sdkhooks>

bool g_bBlockSound;

in header, since tutorial is intended for novice as well.

Updates for descriptions of some errors:

Addition for: "Native XXX is not bound"
- to prevent calling a native of unloaded plugin, implement OnLibraryAdded() and OnLibraryRemoved() natives.
Do not use LibraryExists() constantly because it's a very CPU demanding operation.

Unable to load plugin (bad header) - error similar to "Illegal disk size". (maybe just append it in same sentence)

Warning "Array-based enum structs will be removed in 1.11" - examples of fixing that:
1) by adding MAX_ITEM to enum is here: https://forums.alliedmods.net/showpo...32&postcount=8
2) by converting to enum struct is here: https://forums.alliedmods.net/showpo...0&postcount=45

Silvers 07-15-2020 16:17

Re: [TUT] SourcePawn Scripting - Tips, Basics to Advanced
 
Quote:

Originally Posted by Dragokas (Post 2710226)
Some updates I prepared long time ago:

Thanks!

Had already noticed and fixed AddAmbientSoundHook.

Will add a clarification for this: OnLibraryAdded() and OnLibraryRemoved() can be used instead of ever calling LibraryExists() since they both trigger when late loading a plugin that's using it.

Nice details for 1.11 enum structs change, many have been asking how to fix.

BHaType 08-01-2020 07:03

Re: [TUT] SourcePawn Scripting - Tips, Basics to Advanced
 
I'll just leave it as information maybe it will be useful for someone

INT & BOOL & FLOAT


PROPERTY


command


CHAR


OTHER


Code

Lux 08-01-2020 23:55

Re: [TUT] SourcePawn Scripting - Tips, Basics to Advanced
 
Quote:

Originally Posted by BHaType (Post 2712600)
I'll just leave it as information maybe it will be useful for someone

INT & BOOL & FLOAT


PROPERTY


command


CHAR


OTHER


Code

You should do these bench-tests without C-States skewing the results causing high variance between the VM.
Example below way too much variance.
PHP Code:

BenchMin 0.000075Avg 0.001785Max 0.034192 SetEntProp(0Prop_Send"m_bColdWorld"1


For abit of context anyone who will choose the faster function.
ClientCommand and FakeClientCommand are not comparable they do different jobs FakeClientCommandEx would be equivalent.
PHP Code:

BenchMin 0.001201Avg 0.001476Max 0.002383 FakeClientCommand(client"sm_somecommand");
BenchMin 0.000213Avg 0.000286Max 0.000483 ClientCommand(client"sm_somecommand"); 



All times are GMT -4. The time now is 22:27.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.