As most of you know, ModPE Script is based on a programming language called JavaScript. (Not to be confused with Java, or a potato).
JavaScript, like many programming languages, includes some very handy features that will help you make some spectacular modscripts. I'll show a few ones below, and link to places where you can learn more about them.
1. Variables
Variables are places you can store data. For example, if you want to keep track of a player's score, you can add
var score = 0
and the computer will remember that you have a variable
called "score" that has a value of 0.
If I wanted to change it (say the player won 4 points), just do
score = score + 4;
You can substitute variables for numbers (just like equations), for example, if I wanted to award 2 TNT blocks for every point in their score, I can just type
addToInventory(46, 2 * score);
.
Variables can hold all sorts of things, not just numbers. For example, my PigFlight script uses a variable to hold the data representing the animal that the player is currently sitting on.
2. Loops
There's an old joke in programmer circles: "I used to write millions of lines of code every hour! Then I discovered loops."
Loops are ways to tell a computer to run some code repeatedly. A common one is the For Loop.
For example (pun intended), say if I wanted to check whether the player has any TNT above their head, instead of writing
if (getTile(x, y, z) == 46 || getTile(x, y + 1, z) == 46...
all the way to
...getTile(x, y + 128, z) == 46)
I can let the computer run my checking code multiple times, changing Y everytime, until we find a match.
for (var myY = y; myY < 128; myY++) { //remember variables?
//The for loop changes one so you can loop through blocks.
//By the way, always double check that you used the right variable name
//I made that mistake multiple times, including in the draft of this post
if (getTile(x, myY, z) == 46)) {
print("There's TNT above your head. Might want to move away");
break; //exit the loop - we found the tnt
}
}
3. Functions
Functions, also called methods, are bits of code you can use again and again. For example, say if you had a script that builds multicoloured smiley faces around the player. You can write a method for building smiley faces:
function buildSmileyFace(x, y, z, material) {
setTile(x, y, z, material); //nose
setTile(x - 1, y + 1, z, material); //left eye
setTile(x + 1, y + 1, z, material); //right eye
}
Then, you can call that code like this:
buildSmileyFace(0, 128, 0, 46);
to get a TNT smiley face.
And then, later on in your code, if you need to build a smiley face out of dirt, just call it with different parameters!
buildSmileyFace(22, 33, 33, 4);
To write a function, you just declare your function with the inputs it's going to take (in the previous example, I declared x, y, z, and material), and they will be available as variables that you can read.
If you have followed this far, you might have noticed that ModPE scripts's callbacks themselves are functions.
Some sample scripts: https://github.com/zhuowei/ModPEScripts
This is my script repository; I keep my scripts as well as some contributed by other forum members there so others can study from them.
Could anyone help me with loop? I want to know how to loop a var infinite. I have this code:
function useItem(x,y,z,itemId,blockId)
{
var pY = getPlayerY()-1.6;//feet position
var pX = getPlayerX();
var pZ = getPlayerZ();
var activated=0;
if(blockId==12);
{
activated ==1;
setTile(pX,pY,pZ,51);
print("SuperFart activated!");
}
}
I want to make the "setTile(pX,pY,pZ,51);" loop infite. Any help please? :3
Could anyone help me with loop? I want to know how to loop a var infinite. I have this code:
function useItem(x,y,z,itemId,blockId)
{
var pY = getPlayerY()-1.6;//feet position
var pX = getPlayerX();
var pZ = getPlayerZ();
var activated=0;
if(blockId==12);
{
activated ==1;
setTile(pX,pY,pZ,51);
print("SuperFart activated!");
}
}
I want to make the "setTile(pX,pY,pZ,51);" loop infite. Any help please? :3
If you loop it in an infinite loop, Minecraft would freeze, since you're looping without giving Minecraft a chance to run as well.
Instead, wait for ModPE Script 0.3 - it will have a tick hook, which is a hook called on every Minecraft tick(50 milliseconds), you can repeatedly set block there.
500 ISE Would there be any way to make a ModMP Java Script that would have teleporters that you wold link and if you would stand on it, it would teleport you to the other linked teleporter? If so could you give me the script?
As most of you know, ModPE Script is based on a programming language called JavaScript. (Not to be confused with Java, or a potato).
JavaScript, like many programming languages, includes some very handy features that will help you make some spectacular modscripts. I'll show a few ones below, and link to places where you can learn more about them.
1. Variables
Variables are places you can store data. For example, if you want to keep track of a player's score, you can add
var score = 0
and the computer will remember that you have a variable
called "score" that has a value of 0.
If I wanted to change it (say the player won 4 points), just do
score = score + 4;
You can substitute variables for numbers (just like equations), for example, if I wanted to award 2 TNT blocks for every point in their score, I can just type
addToInventory(46, 2 * score);
.
Variables can hold all sorts of things, not just numbers. For example, my PigFlight script uses a variable to hold the data representing the animal that the player is currently sitting on.
2. Loops
There's an old joke in programmer circles: "I used to write millions of lines of code every hour! Then I discovered loops."
Loops are ways to tell a computer to run some code repeatedly. A common one is the For Loop.
For example (pun intended), say if I wanted to check whether the player has any TNT above their head, instead of writing
if (getTile(x, y, z) == 46 || getTile(x, y + 1, z) == 46...
all the way to
...getTile(x, y + 128, z) == 46)
I can let the computer run my checking code multiple times, changing Y everytime, until we find a match.
for (var myY = y; myY < 128; myY++) { //remember variables?
//The for loop changes one so you can loop through blocks.
//By the way, always double check that you used the right variable name
//I made that mistake multiple times, including in the draft of this post
if (getTile(x, myY, z) == 46)) {
print("There's TNT above your head. Might want to move away");
break; //exit the loop - we found the tnt
}
}
3. Functions
Functions, also called methods, are bits of code you can use again and again. For example, say if you had a script that builds multicoloured smiley faces around the player. You can write a method for building smiley faces:
function buildSmileyFace(x, y, z, material)
Then, you can call that code like this:
buildSmileyFace(0, 128, 0, 46);
to get a TNT smiley face.
And then, later on in your code, if you need to build a smiley face out of dirt, just call it with different parameters!
buildSmileyFace(22, 33, 33, 4);
To write a function, you just declare your function with the inputs it's going to take (in the previous example, I declared x, y, z, and material), and they will be available as variables that you can read.
If you have followed this far, you might have noticed that ModPE scripts's callbacks themselves are functions.
Some sample scripts: https://github.com/z...ei/ModPEScripts
This is my script repository; I keep my scripts as well as some contributed by other forum members there so others can study from them.
The example you give to build a smiley face using the function doesn't work.
You were supposed to write the method! updated for clarification.
Thanks. What I meant to say was that for the example you can't create a custom function to build a block unless you use the setTile(); function. I wish there was a setItem(); function so that I could position an item relative to the player. I want to make a back tools mod similar to that of pc.
I will almost always use "metatables" (Metatables are a Lua concept, but JavaScript has a similar concept) to create new class-like features. While I've never made or used a ModPE script before, I made a quick script that probably won't work (the syntax is right, but I don't know if ModPE will like the script very much), but if it does, have fun with learning how to make class-like functions. For some of you more intermediate or advanced users, be a bit adventurous and learn new concepts in JavaScript:
//Yes, you can set a function as a variable!
var House = function(){
var width = 20;
var height = 5;
var length = 15;
//Accepts an integer representing a block ID
var setWallMat = function(material){
if (arguments.length) {
that.wallMaterial = material;
}else{
that.wallMaterial = 5; //Wood
}
};
//Accepts an integer representing a block ID
var setFloorMat = function(material){
if (arguments.length) {
that.floorMaterial = material;
}else{
that.floorMaterial = 4; //CobbleStone
}
};
//Needs width,length and height
var size = function(w,l,h){
width = w;
length = l;
height = h;
};
//Needs an x,y,z position, the position is of the front-bottom-left corner
var pos = function(x,y,z){
that.x = x;
that.y = y;
that.z = z;
};
//This is just a simple table, nothing too complex
var that = {
setWallMaterial: setWallMat, //Function from above
setFloorMaterial: setFloorMat,
setSize: size,
setPos: pos,
wallMaterial: 5,
floorMaterial: 4,
windows: false,
x: Math.floor(Player.getX()+1), //Default uses player position
y: Math.floor(Player.getY()),
z: Math.floor(Player.getZ()),
//The function that creates the house
make: function(window) {
if (arguments.length) {
that.windows = arguments[0];
}
//floor
for(var i=0;i<width;i++){
for(var j=0;j<length;j++){
setTile(x+i,y-1,z+j,floorMaterial);
}
}
//ceiling
for(var i=0;i<width;i++){
for(var j=0;j<length;j++){
setTile(x+i,y+height-1,z+j,wallMaterial);
}
}
//front wall
for(var i=0;i<width;i++){
for(var j=0;j<height;j++){
setTile(x+i,y+j,z,wallMaterial);
}
}
setTile(x+Math.floor(width/2),y,z,0);
setTile(x+Math.floor(width/2),y+1,z,0);
//back wall
for(var i=0;i<width;i++){
for(var j=0;j<height;j++){
setTile(x+i,y+j,z+length-1,wallMaterial);
}
}
//left wall
for(var i=0;i<lenth;i++){
for(var j=0;j<height;j++){
if(windows && i<z+length-2 && i>z+1 && j<height-1 && j>y+1){
setTile(x,y+j,z+i,102); //Glass Pane
}else{
setTile(x,y+j,z+i,wallMaterial);
}
}
}
//right wall
for(var i=0;i<lenth;i++){
for(var j=0;j<height;j++){
if(windows && i<z+length-2 && i>z+1 && j<height-1 && j>y+1){
setTile(x+width-1,y+j,z+i,102); //Glass Pane
}else{
setTile(x+width-1,y+j,z+i,wallMaterial);
}
}
}
}
};
return that;
}();
House.setWallMaterial(1); //Stone
House.setFloorMaterial(5); //Wood
House.setSize(15,4,10); //width is 15, height is 4, length is 10
House.make(true); //(Hopefully) Make a house with windows!
But yeah, if I have a large, complex task I want to complete, I make seperate "classes" that can handle different parts of the task and organize the code better.
That's not possible with ModScript. :$ You need a texture pack at least...
Yeah I've already retextured the leather chestplate to be a diamond sword on my back, but I want the real item on my back because it's bigger and 3 dimensional
I will almost always use "metatables" (Metatables are a Lua concept, but JavaScript has a similar concept) to create new class-like features. While I've never made or used a ModPE script before, I made a quick script that probably won't work (the syntax is right, but I don't know if ModPE will like the script very much), but if it does, have fun with learning how to make class-like functions. For some of you more intermediate or advanced users, be a bit adventurous and learn new concepts in JavaScript:
//snip
But yeah, if I have a large, complex task I want to complete, I make seperate "classes" that can handle different parts of the task and organize the code better.
That's the most overkill house script I've seen.
In Java, many classes like this uses a builder concept, where each setter returns the current object, so you'll write
-
View User Profile
-
View Posts
-
Send Message
Curse PremiumJavaScript, like many programming languages, includes some very handy features that will help you make some spectacular modscripts. I'll show a few ones below, and link to places where you can learn more about them.
1. Variables
Variables are places you can store data. For example, if you want to keep track of a player's score, you can add
and the computer will remember that you have a variable
called "score" that has a value of 0.
If I wanted to change it (say the player won 4 points), just do
You can substitute variables for numbers (just like equations), for example, if I wanted to award 2 TNT blocks for every point in their score, I can just type
.
Variables can hold all sorts of things, not just numbers. For example, my PigFlight script uses a variable to hold the data representing the animal that the player is currently sitting on.
http://www.codecademy.com/courses/getting-started-v2?curriculum_id=506324b3a7dffd00020bf661
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals
2. Loops
There's an old joke in programmer circles: "I used to write millions of lines of code every hour! Then I discovered loops."
Loops are ways to tell a computer to run some code repeatedly. A common one is the For Loop.
For example (pun intended), say if I wanted to check whether the player has any TNT above their head, instead of writing
all the way to
I can let the computer run my checking code multiple times, changing Y everytime, until we find a match.
for (var myY = y; myY < 128; myY++) { //remember variables? //The for loop changes one so you can loop through blocks. //By the way, always double check that you used the right variable name //I made that mistake multiple times, including in the draft of this post if (getTile(x, myY, z) == 46)) { print("There's TNT above your head. Might want to move away"); break; //exit the loop - we found the tnt } }http://www.codecademy.com/courses/javascript-beginner-en-NhsaT?curriculum_id=506324b3a7dffd00020bf661
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Statements
3. Functions
Functions, also called methods, are bits of code you can use again and again. For example, say if you had a script that builds multicoloured smiley faces around the player. You can write a method for building smiley faces:
function buildSmileyFace(x, y, z, material) { setTile(x, y, z, material); //nose setTile(x - 1, y + 1, z, material); //left eye setTile(x + 1, y + 1, z, material); //right eye }Then, you can call that code like this:
to get a TNT smiley face.
And then, later on in your code, if you need to build a smiley face out of dirt, just call it with different parameters!
To write a function, you just declare your function with the inputs it's going to take (in the previous example, I declared x, y, z, and material), and they will be available as variables that you can read.
If you have followed this far, you might have noticed that ModPE scripts's callbacks themselves are functions.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions
http://www.codecademy.com/courses/javascript-beginner-en-6LzGd?curriculum_id=506324b3a7dffd00020bf661
I hope this has inspired you to learn a bit more about programming. Good luck and have fun!
Tutorials for JavaScript:
Codecademy: http://www.codecademy.com/tracks/javascript
Very good interactive tutorial.
Mozilla Developers Wiki: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide
A little more academic; brought to you by the people who developed Firefox.
JavaScript Kit: http://www.javascriptkit.com/javatutors/
A bit old, and too focused on scripting webpages, but helped me when I was learning.
Some sample scripts:
https://github.com/zhuowei/ModPEScripts
This is my script repository; I keep my scripts as well as some contributed by other forum members there so others can study from them.
-
View User Profile
-
View Posts
-
Send Message
Curse PremiumCheck out my game! It's an open-world, sandbox text adventure.
Follow @hexdro_
Hexdro © 2012-2015
function useItem(x,y,z,itemId,blockId) { var pY = getPlayerY()-1.6;//feet position var pX = getPlayerX(); var pZ = getPlayerZ(); var activated=0; if(blockId==12); { activated ==1; setTile(pX,pY,pZ,51); print("SuperFart activated!"); } }I want to make the "setTile(pX,pY,pZ,51);" loop infite. Any help please? :3-
View User Profile
-
View Posts
-
Send Message
Curse PremiumIf you loop it in an infinite loop, Minecraft would freeze, since you're looping without giving Minecraft a chance to run as well.
Instead, wait for ModPE Script 0.3 - it will have a tick hook, which is a hook called on every Minecraft tick(50 milliseconds), you can repeatedly set block there.
Thx,
Ryan
Lead developer of Dragonet!
Check out my game, Adventuria!
Dev of (IMO the best server ever) TwilightGamez!
-
View User Profile
-
View Posts
-
Send Message
Curse PremiumYou were supposed to write the method! updated for clarification.
//Yes, you can set a function as a variable! var House = function(){ var width = 20; var height = 5; var length = 15; //Accepts an integer representing a block ID var setWallMat = function(material){ if (arguments.length) { that.wallMaterial = material; }else{ that.wallMaterial = 5; //Wood } }; //Accepts an integer representing a block ID var setFloorMat = function(material){ if (arguments.length) { that.floorMaterial = material; }else{ that.floorMaterial = 4; //CobbleStone } }; //Needs width,length and height var size = function(w,l,h){ width = w; length = l; height = h; }; //Needs an x,y,z position, the position is of the front-bottom-left corner var pos = function(x,y,z){ that.x = x; that.y = y; that.z = z; }; //This is just a simple table, nothing too complex var that = { setWallMaterial: setWallMat, //Function from above setFloorMaterial: setFloorMat, setSize: size, setPos: pos, wallMaterial: 5, floorMaterial: 4, windows: false, x: Math.floor(Player.getX()+1), //Default uses player position y: Math.floor(Player.getY()), z: Math.floor(Player.getZ()), //The function that creates the house make: function(window) { if (arguments.length) { that.windows = arguments[0]; } //floor for(var i=0;i<width;i++){ for(var j=0;j<length;j++){ setTile(x+i,y-1,z+j,floorMaterial); } } //ceiling for(var i=0;i<width;i++){ for(var j=0;j<length;j++){ setTile(x+i,y+height-1,z+j,wallMaterial); } } //front wall for(var i=0;i<width;i++){ for(var j=0;j<height;j++){ setTile(x+i,y+j,z,wallMaterial); } } setTile(x+Math.floor(width/2),y,z,0); setTile(x+Math.floor(width/2),y+1,z,0); //back wall for(var i=0;i<width;i++){ for(var j=0;j<height;j++){ setTile(x+i,y+j,z+length-1,wallMaterial); } } //left wall for(var i=0;i<lenth;i++){ for(var j=0;j<height;j++){ if(windows && i<z+length-2 && i>z+1 && j<height-1 && j>y+1){ setTile(x,y+j,z+i,102); //Glass Pane }else{ setTile(x,y+j,z+i,wallMaterial); } } } //right wall for(var i=0;i<lenth;i++){ for(var j=0;j<height;j++){ if(windows && i<z+length-2 && i>z+1 && j<height-1 && j>y+1){ setTile(x+width-1,y+j,z+i,102); //Glass Pane }else{ setTile(x+width-1,y+j,z+i,wallMaterial); } } } } }; return that; }(); House.setWallMaterial(1); //Stone House.setFloorMaterial(5); //Wood House.setSize(15,4,10); //width is 15, height is 4, length is 10 House.make(true); //(Hopefully) Make a house with windows!But yeah, if I have a large, complex task I want to complete, I make seperate "classes" that can handle different parts of the task and organize the code better.
So how would you do it? xD
I am secretly a modder, who works only for himself, seldom publishes.
-
View User Profile
-
View Posts
-
Send Message
Curse PremiumThat's the most overkill house script I've seen.
In Java, many classes like this uses a builder concept, where each setter returns the current object, so you'll write
I'm pretty sure this can be done here as well.
Edit: script fails with a "ReferenceError: "x" is not defined" on line 60.
for(var i=0;i<width;i++){ for(var j=0;j<length;j++){ setTile(x+i,y-1,z+j,floorMaterial); } }I am pretty sure you need to use this.x instead: JavaScript does not look for variables in the current object context.