Become a Script Master Tutorial

I have learned some new code that is really helpful! This tutorial is to introduce this code to you, and give you new capabilities with coding (unless if you already knew this).

Anyways, let me start with removing a script.


1 - Removing a Script

You probably wanna create a clone of an object, or stop running the update script of an object for some reason. Maybe you don’t want the unload script to work for an object? Well, here’s your solution:

Clip.removeScript("scriptname"); //  Remove a script from "Clip"

Replace “scriptname” with the name of the script, for example, you can use “default,” “update,” “unload,” “keydown,” “mouseclick,” and etc. but keep the quotation marks to have it work! As a side note, the script will still be there once you stop the project, this code shouldn’t erase a script from the editor, but rather disable it while running the project.


2 - Adding/ Replacing a Script

I have found this code really helpful when creating clones, you just create a clone, and give its very own script! It’s a form of eval, yes, and here’s how it looks like:

Clip.addScript("scriptname","Code"); 
/* Replace Clips default script with Code */

This code replaces or adds to the default script of “clip” the code that you type in where it says “Code.” If there’s an error, the project will take you to the clip, and the console will give you an error, but the script you added during the project WILL NOT be in the clip, everything resets once the project pauses.


3 - Choose What Script to Run

Hmmmm… ever wondered if there was a way to tell the project to run a script whenever you want it to? Will, here’s how:

this.scheduleScript("scriptName"); // Change scriptName with name of script

Change “scriptName” with the name of the script that you want to run, and it’ll run that specific script for that specific clip. Also, you can have it run a script related to key, and define the key that being used in that script!

For example, let’s say that I wanna run the keydown script for the “a” key and the a key isn’t even pressed down, I can do this:

this.scheduleScript("keydown", {key: "a"} );

This is a really good way to have wasd and the arrow keys act the same way in a project without going through all of your code and adding them in!


(Also, in case anyone’s curious, I found this code while reading through wicks sources)

6 Likes

I didn’t know that. But I would still rather prefer using variables to determine whether the script should run.

if (canRun) {

// code
// code
// code

}

But, it’d be cool if I could have the equivalent of “return” for a script so I could instead do this:

if (!canRun) stop;

// code
// code
// code

which looks nicer. (And I probably wouldn’t have to battle with the auto-formatter. I’m pretty sure the auto-formatter would be angry at that, because I would not count for the if-statement when indenting my code)

But I can see how this.scheduleScript would be useful.

1 Like

Thank you for sharing. I didn’t know about it. Well, @pumpkinhead, Do you think that we could use the addScript one to add code from the local machine? I think is possible…

Why?
Well, sometimes we have better software to write code like vs code or so, and maybe we could develop and debug an entire game using the editor of preference, and then, at the end just embed the code…

I forgot to add these guys to the tutorial:

object.hasScript('update') // true if object has update script
object.runScript('default'); // run default script of object
Also, here's how the editor checks for and calls certain events
// mouseenter {
if (this._lastMouseState === 'out' && this._mouseState!== 'out') {
  this.scheduleScript('mouseenter');
} 
// }

// mousedown {
if (this._mouseState=== 'down') {
  this.scheduleScript('mousedown');
} 
// }

// mousepressed {
if (this._lastMouseState === 'over' && this._mouseState=== 'down') {
  this._isClickTarget = true;
  this.scheduleScript('mousepressed');
} 
// }

// mouseclick {
if (this._lastMouseState === 'down' && this._mouseState=== 'over' && this._isClickTarget) {
  this.scheduleScript('mouseclick');
}
// }

// Mousereleased {
if (this._lastMouseState === 'down' && this._mouseState=== 'over') {
  this._isClickTarget = false;
  this.scheduleScript('mousereleased');
}
// }

// mouseleave {
if (this._lastMouseState !== 'out' && this._mouseState=== 'out') { 
  this.scheduleScript('mouseleave');
} 
// }

// Mousehover {
if (this._mouseState=== 'over') {
  this.scheduleScript('mousehover');
}
// }

// mousedrag {
if (this._lastMouseState === 'down' && this._mouseState=== 'down') {
  this.scheduleScript('mousedrag');
}
// }

// keydown {
this.project.keysDown.forEach(key => {
  this.project.currentKey = key;
  this.scheduleScript('keydown', {
    key: key
  });
});
// }

// Key press {
this.project.keysJustPressed.forEach(key => {
  this.project.currentKey = key;
  this.scheduleScript('keypressed', {
    key: key
  });
});
// }

// Key released {
this.project.keysJustReleased.forEach(key => {
  this.project.currentKey = key;
  this.scheduleScript('keyreleased', {
    key: key
  });
});
// }

// I didn't write this code
2 Likes

I’m playing with this… What is Clip… ?

Edit: I figured already…
Edit2: I’m getting somewhere… I’ll share it soon…

I’m trying to use this for one of my games (more specifically runScript()). However the default script I’m trying to run is in a frame, not an object. Not sure how to get the frame to run the script.

I just tested it with a clip instead of a frame in a dummy project and runScript() does work, so all I need to do is talk to a frame instead of a clip.

1 Like

This worked for me… at the first frame default script I added a console.log(“Hi”);… then at that button I called frame.runScript(‘deafult’); and it executed and printed “Hi” every time that I press that button.

Scripts6-4-2022_12-48-13.wick (2.1 KB)

2 Likes