plz help #2



  • Is this the optimal way to check wether a setting was changed while a module is enabled?
    This is my example script:

    /// api_version=2
    var script = registerScript({
    	name: "ExamplePfiff",
    	version: "0.0",
    	authors: ["KnechtRobert5"]
    });
    script.registerModule({
    	name: "Pfiff",
    	category: "Misc",
    	description: "Does some Pfiff here and there.",
    	settings: {
    		examplesetting: Setting.boolean({
    			name: "enable",
    			default: true
    		})
    	}
    }, function(module) {
    	var wasenabled = false;
    	module.on("enable", function() {
    		if (module.settings.examplesetting.get()) {
    			Chat.print("§aon")
    			wasenabled = true;
    			//module enabled
    		}
    	});
    	module.on("disable", function() {
    		if (module.settings.examplesetting.get()) {
    			Chat.print("§coff")
    			wasenabled = false;
    			//module disabled
    		}
    	});
    	module.on("update", function() {
    		if (module.settings.examplesetting.get() && !wasenabled) {
    			Chat.print("§aon")
    			wasenabled = true;
    			//setting enabled
    		}
    		if (!module.settings.examplesetting.get() && wasenabled) {
    			Chat.print("§coff")
    			wasenabled = false;
    			//setting disabled
    		}
    		
    	});
    });
    

    The main module here does nothing, only if the setting in it is true / false it loads / unloads code. this script works perfectly for what im trying to do, but is it the optimal way to check for that and does it impact the performance of the game if it checks every tick (while module is enabled) if the setting is true or false? (Sorry if this sounds dumb but im new to this kinda stuff)
    If asked for what this is needed, i want to make a clean module which doesent break that easily or in general doesnt break (user friendly)


  • Admin

    If you just want to check if the state of a setting has changed, the following code might be an simpler solution:

    // <your code>
    
    var lastState = null;
    
    module.on("enable", function() {
        lastState = module.settings.examplesetting.get();
    });
    
    module.on("update", function() {
        var currentState = module.settings.examplesetting.get();
    
        if (lastState !== currentState) {
            Chat.print("State has been changed! New state: " + currentState);
            lastState = currentState;
        }
    });
    
    // <your code>
    
    

    From a performance point of view you should not expect any problems here. Getting the state of a setting should require very little runtime.



  • @Senk-Ju thanks 🙂


Log in to reply