Jump to content

Snippets for Scripter


Unheardofski

Recommended Posts

EDIT 2017-03-17:

Added a slightly more advanced version (constructor obj + methods) at the bottom. Thanks to autoditactic for the inspiration to improve this.

 

 

I thought it might be a cool idea to share some of the code we use to make scripting more efficient. I have a bunch I use regularly and it makes writing scripts much less of a PITA.

 

I'll start us off with the one I use in every script that has parameters:

create_param

You save yourself a lot of typing/copy pasting with this. It also has a condition for checkbox so you don't get the annoying warnings in the console.

I often add an additional parameter to the pushed object: par_class: 'some string describing the type of parameter' to easily identify what type of parameter ParameterChanged() is dealing with when you have a lot of parameters. You could also add methods to the parameter object directly.

 

Requires the variable (array) PluginParameters to be declared.

function create_param(n, t, vs, miv, mav, def, nos, unit) {
t == 'checkbox' ? PluginParameters.push({
name: n,
type: t,
defaultValue: 0

}) :
PluginParameters.push({
name: n,
type: t,
valueStrings: vs,
minValue: miv,
maxValue: mav,
defaultValue: def,
numberOfSteps: nos,
unit: unit
});
}

////////////////////////////////////////

function LPXjs(){
  PluginParameters = [];
}

LPXjs.prototype.param = function(obj) {
  this.name = obj.n;
  this.type = obj.t || "menu";
  this.defaultValue = obj.dfv || 0;
  if(this.type != "checkbox"){
  this.valueStrings = obj.vs || false;
  this.minValue = obj.mv || 0;
  this.maxValue = obj.mav || this.valueStrings.length - 1 || 1;
  this.numberOfSteps = obj.nos || this.valueStrings.length - 1 || false;
  this.currentValue;
  this.callBack = obj.callBack || function(){};
  PluginParameters.push(this);
} ;

 

example of v2 in use:

var cc = new LPXjs();
var a =  new cc.param({n: "select something", t: "menu", vs: [0,1,3,6,8], callBack: function(v){this.currentValue = v }});


function ParameterChanged(p,v){
PluginParameters[p].callBack(v);
}

Edited by Unheardofski
Link to comment
Share on other sites

  • 1 year later...

Here's a new one I made that is ridic useful. If I had a dime for everytime I wrote Math.floor(Math.random() * min . . . . . . . .

 

Here's a function that accepts arrays, single numbers, min and max number and even strings:

 

function random(a, b){

 var i  = Object.prototype.toString.call(a).split(" ")[1].replace("]","");

  switch(i){

  case "Array":

   return a[Math.floor(Math.random() * a.length)];

   case "String":

  return a.charAt(Math.floor(Math.random() * a.length))

  case "Number":

    if(b){

      return Math.floor(Math.random() * (b - a + 1)) + a;

        } else {

          return Math.floor(Math.random() * (a - 0 + 1)) + 0;

        }

        break;

        default:

  }

  return false;

}

Link to comment
Share on other sites

  • 3 weeks later...

the random function slightly improved to also handle objects thrown at it (one dimension only for now)

 

function random(a, b){
 var i  = Object.prototype.toString.call(a).split(" ")[1].replace("]","");
  switch(i){
  case "Array":
   return a[Math.floor(Math.random() * a.length)];
   case "String":
  return a.charAt(Math.floor(Math.random() * a.length))
  case "Number":
    if(b){
      return Math.floor(Math.random() * (b - a + 1)) + a;
        } else {
          return Math.floor(Math.random() * (a - 0 + 1)) + 0;
        }
        break;
  case "Object":
          var tempArr = [];
          for(var p in a){
            tempArr.push(a[p]);
          }
          return this.random(tempArr);
          break;
        default:
  }
  return false;
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...