[Include] samp-observer.inc - Observe variables for changes
#1

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 Код:
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 Код:
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 Код:
#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
Reply


Messages In This Thread
samp-observer.inc - Observe variables for changes - by Slice - 11.07.2016, 18:14
Re: samp-observer.inc - Observe variables for changes - by Jay_ - 11.07.2016, 18:26
Re: samp-observer.inc - Observe variables for changes - by Slice - 11.07.2016, 18:36
Re: samp-observer.inc - Observe variables for changes - by Ritzy2K - 11.07.2016, 18:55
Re: samp-observer.inc - Observe variables for changes - by AliBro - 11.07.2016, 18:57
Re: samp-observer.inc - Observe variables for changes - by Marricio - 11.07.2016, 19:01
Re: samp-observer.inc - Observe variables for changes - by Slice - 11.07.2016, 19:08
Re: samp-observer.inc - Observe variables for changes - by Ivan_Ino - 11.07.2016, 19:08
Re: samp-observer.inc - Observe variables for changes - by Evocator - 11.07.2016, 19:48
Re: samp-observer.inc - Observe variables for changes - by JustMe.77 - 11.07.2016, 19:52
Re: samp-observer.inc - Observe variables for changes - by Markdrifter18 - 11.07.2016, 23:19
Re: samp-observer.inc - Observe variables for changes - by PT - 11.07.2016, 23:20
Re: samp-observer.inc - Observe variables for changes - by Slice - 12.07.2016, 02:23
Re: samp-observer.inc - Observe variables for changes - by SyS - 12.07.2016, 02:29
Re: samp-observer.inc - Observe variables for changes - by Kamper - 18.07.2016, 00:42
Re: samp-observer.inc - Observe variables for changes - by Crayder - 19.07.2016, 13:57
Re: samp-observer.inc - Observe variables for changes - by DRIFT_HUNTER - 19.07.2016, 16:49
Re: samp-observer.inc - Observe variables for changes - by Logic_ - 19.07.2016, 18:47
Re: samp-observer.inc - Observe variables for changes - by Admigo - 19.07.2016, 19:21

Forum Jump:


Users browsing this thread: 1 Guest(s)