How to programmatically update a plugin's settings? Is there a way to programatically update a plugin's settings? I'm experimenting with setSettings() but not really getting anywhere - I'm still quite new to plugin development so there's a good chance i'm completely off target hehe.

For some context: I'm trying to update the plugin settings when an event is fired and picked up by a listener as follows:

public function init()
{

    // add event listeners
    craft()-&gt;on('assets.onReplaceFile', function(Event $event) {

        //Load Asset
        $asset = $event-&gt;params['asset'];

        //Check if asset is the svg sprite
        if($asset-&gt;getUrl() == $this-&gt;getSettings()-&gt;spritePath){
            $url = craft()-&gt;getSiteUrl().$this-&gt;getSettings()-&gt;spritePath;
            $this-&gt;setSettings(array('spriteDefs'=&gt;$this-&gt;getSpriteDefs($url)));
        }

    });
}


getSpriteDefs returns an array.

Thanks in advance!
It's not 100% clear from your question, but I'm guessing that by "update the settings" you mean actually storing the new settings in the database, which the setSettings() method will not do; it will only update your plugin's settings model. This means that any following calls to e.g. getSettingsHtml() should return the updated data, but it will not persist. 

To store the new settings in the database, you'll need to use Craft's PluginService:

craft()-&gt;plugins-&gt;savePluginSettings( $this, array( 'spriteDefs' =&gt; $this-&gt;getSpriteDefs( $url ) ) );


The $this variable in the call above refers to an instance of BasePlugin – meaning that the above code will only work if called from your plugins main class. I notice that your code is contained within your plugin's init() method, so you should be fine, however if you ever want to update a plugin's settings from outside the main class (e.g. through a service), you would first need to fetch an instance of the plugin:

$myPlugin = craft()-&gt;plugins-&gt;getPlugin( 'myPluginHandle' );
craft()-&gt;plugins-&gt;savePluginSettings( $myPlugin, $settings );