SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Filterscripts > Includes

Reply
 
Thread Tools Display Modes
Old 11/07/2016, 06:14 PM   #1
Slice
High-roller
 
Join Date: Mar 2008
Posts: 1,831
Reputation: 1607
Spray samp-observer.inc - Observe variables for changes

samp-observer

Easily observe variables for changes. At the moment the variable is modified, the callback will be invoked.

There is no need for custom syntax, and this include does not use timers or loops to check for changes!

Example usage

Dynamic variables:
pawn Code:
new Var1, Var2;

// The value of DynVar will update every time Var1 or Var2 change
dynamic DynVar <- (Var1, Var2) {
    return Var1 * Var2;
}

main() {
    Var1 = 5;
    Var2 = 10;

    // Output: DynVar is 50
    printf("DynVar is %d", DynVar);
}

More detailed example:
pawn Code:
new g_Test = 1234;
new g_TestString[32];
new g_Array[5];
new g_ArraySum = 0;

main() {
    // Now OnTestChange will be executed
    g_Test = 789;

    print(g_TestString); // Test is 789

    // Every time this happens, OnArrayChange will be executed
    for (new i = 0; i < sizeof(g_Array); i++)
        g_Array[i] = (i + 1) * 10;

    // g_ArraySum will be automatically updated in OnArrayChange
    printf("The sum of g_Array is %d", g_ArraySum);

    for (new i = 0; i < sizeof(g_Array); i++)
        g_Array[i] *= 10;

    printf("The sum of g_Array is %d", g_ArraySum);
}

// Executed every time g_Test is changed
observer OnTestChange(previous, current) {
    printf("Test changed from %d to %d", previous, current);

    // Update a string when this value changes
    format(g_TestString, sizeof(g_TestString), "Test is %d", current);
}

// Executed every time g_Array is changed
observer OnArrayChange(prev, curr, idx) {
    printf("g_Array[%d] %d -> %d", idx, prev, curr);

    // Keep track of the array's sum without ever looping through it
    g_ArraySum += curr - prev;
}

public InstallObservers() {
    ObserveVar(g_Test, OnTestChange);
    ObserveArray(g_Array, OnArrayChange);
}

Usage and requirements

You need the amx_assembly lib. Simply place it with your include files, along with samp-observer, then include it like this:

pawn Code:
#include <samp-observer/observe>

How it's done

The include will modify the AMX code to add the function call every time an observed var/array is modified.

When you change an observed variable, the observer callback will be executed before the code continues.

Compatibility

This include is compatible with and without both crashdetect and the JIT plugin.

Limitations

You can only observe global variables and 1-dimensional arrays. I hope to bring 2-dimensional arrays to this include also!

Credits

This would not be possible without Zeex's amx_assembly lib and Y-Less's contributions to it. Most importantly, the latest codescanner addition.

GitHub
https://github.com/oscar-broman/samp-observer

Last edited by Slice; 12/07/2016 at 02:23 AM.
Slice is offline   Reply With Quote
Old 11/07/2016, 06:26 PM   #2
Jay_
Gangsta
 
Jay_'s Avatar
 
Join Date: Jul 2009
Posts: 654
Reputation: 204
Default Re: samp-observer.inc - Observe variables for changes

Nice work Slice as always, this is a game changer. This has given me an idea on how I can improve my debugging techniques!
Jay_ is offline   Reply With Quote
Old 11/07/2016, 06:36 PM   #3
Slice
High-roller
 
Join Date: Mar 2008
Posts: 1,831
Reputation: 1607
Default Re: samp-observer.inc - Observe variables for changes

Good idea to use for debugging!

That actually got me thinking about using PrintAmxBacktrace from crashdetect within the observer. Right now it will not print the correct line number, but I think that can be fixed.

You could for example use this to keep track of TextDraws in some more complicated systems.
Slice is offline   Reply With Quote
Old 11/07/2016, 06:55 PM   #4
Ritzy2K
High-roller
 
Ritzy2K's Avatar
 
Join Date: Jun 2014
Location: India
Posts: 1,547
Reputation: 170
Default Re: samp-observer.inc - Observe variables for changes

Amazing.
__________________
Ritzy2K is offline   Reply With Quote
Old 11/07/2016, 06:57 PM   #5
AliBro
Big Clucker
 
Join Date: Aug 2015
Location: San Fierro
Posts: 57
Reputation: 13
Default Re: samp-observer.inc - Observe variables for changes

Great Work as always +repped
__________________
AliBro is offline   Reply With Quote
Old 11/07/2016, 07:01 PM   #6
Marricio
Gangsta
 
Join Date: Dec 2010
Posts: 718
Reputation: 96
Default Re: samp-observer.inc - Observe variables for changes

Neat! Is there a way to detect automatically when a variable is created and assign a observer just after? (In the amx assembly) Perhaps a macro, like observer var; instead of new var;. Maybe not too possible right now since it doesnt support multi dimensional arrays.
Marricio is offline   Reply With Quote
Old 11/07/2016, 07:08 PM   #7
Slice
High-roller
 
Join Date: Mar 2008
Posts: 1,831
Reputation: 1607
Default Re: samp-observer.inc - Observe variables for changes

Quote:
Originally Posted by Marricio View Post
Neat! Is there a way to detect automatically when a variable is created and assign a observer just after? (In the amx assembly) Perhaps a macro, like observer var; instead of new var;. Maybe not too possible right now since it doesnt support multi dimensional arrays.
You can only observe global variables, but I could make a macro such as this:
pawn Code:
// Global var declared somewhere
new g_Test;

observe g_Test(prev, curr) {
    // code
}

I'll look into it.


I'm also thinking about a more advanced concept, similar to that in some react frameworks:
pawn Code:
new g_Var1, g_Var2;
new g_DynamicVar = {
    return g_Var1 * g_Var2;
};

The idea here is that g_DynamicVar will automatically update when either g_Var1 or g_Var2 is modified.

This is all possible with the current version, but you need to write a bit more code. It would be nice to have a more compact way to express these things.
Slice is offline   Reply With Quote
Old 11/07/2016, 07:08 PM   #8
Ivan_Ino
Big Clucker
 
Join Date: Oct 2012
Location: Croatia
Posts: 101
Reputation: 120
Default Re: samp-observer.inc - Observe variables for changes

Good work Slice dudek
Ivan_Ino is offline   Reply With Quote
Old 11/07/2016, 07:48 PM   #9
Evocator
Huge Clucker
 
Join Date: Nov 2013
Posts: 464
Reputation: 158
Default Re: samp-observer.inc - Observe variables for changes

This is brilliant! I will definitely be using this for debugging.
Thanks slice, perfect as always.
Evocator is offline   Reply With Quote
Old 11/07/2016, 07:52 PM   #10
JustMe.77
Huge Clucker
 
JustMe.77's Avatar
 
Join Date: Jun 2013
Location: Germany
Posts: 215
Reputation: 65
Default Re: samp-observer.inc - Observe variables for changes

Great release my student !
JustMe.77 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
[Tutorial] Player Variables vs Normal Variables. Carlton Tutorials 24 09/09/2016 07:19 PM
How to make Y_INI loop through all variables, without knowing the name of the variables? Prostilov Scripting Help 3 31/08/2014 10:24 PM
Global variables or Local variables armyoftwo Help Archive 3 02/10/2010 04:45 PM
New rcon variables in SAMP 0.3. What do they do? KKashimia Server Support 2 10/11/2009 09:03 PM


All times are GMT. The time now is 05:43 PM.


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