Giter Club home page Giter Club logo

samp-observer's Introduction

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

new g_Test = 1234;
new g_TestString[32];
new g_Array[5];
new g_ArraySum = 0;

new g_Var1, g_Var2, g_Var3[10];

// The value of g_DynVar will update every time g_Var1/2/3 change
dynamic g_DynVar <- (g_Var1, g_Var2, g_Var3) {
	return g_Var1 + g_Var2 + g_Var3[1] + g_Var3[2];
}

main() {
	// Changing these automatically update g_DynVar
	g_Var1 = 1;
	g_Var2 = 10;
	g_Var3[1] = 100;
	g_Var3[2] = 1000;

	printf("g_DynVar = %d", g_DynVar);

	// 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:

#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.

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.

Limitations

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.