I want to create a function that returns a result. I want to run the function from the update. Below is an example of what I have in mind, but I don’t know how to make this exactly. Who can help me with this?
// update
Example.prototype.update = function(dt) {
if (this.randomFunction[result] === true) {
// code that need a true result from the randomFunction
}
};
// function
Example.prototype.randomFunction = function() {
if (this.randomCheck > 0) {
return true;
}
else {
return false;
}
};
I have no idea. It was just something I thought it should be. How about to get a value?
var Example = pc.createScript('Example');
// initialize
Example.prototype.initialize = function() {
this.value = 2;
};
// update
Example.prototype.update = function(dt) {
if (this.randomFunction(> 1)) {
// code that need a value more then 1 from the randomFunction
}
};
// function
Example.prototype.randomFunction = function() {
return this.value;
};
If you’re always returning a value, you can effectively treat your function as a variable in itself. It would look like (using your example):
var Example = pc.createScript('Example');
// initialize
Example.prototype.initialize = function() {
this.value = 2;
};
// update
Example.prototype.update = function(dt) {
if (this.randomFunction() > 1) {
// code that need a value more then 1 from the randomFunction
}
};
// function
Example.prototype.randomFunction = function() {
return this.value;
};
In order to evaulate the function and get the result, you have to use brackets () after the function name. If you are trying to output the result to console, then:
console.log(this.randomFunction())
A more readable way is to save the result into a variable and use that instead:
var result = this.randomFunction();
console.log(result);
If you can show how you are trying to print the result to console, I can advice. Otherwise, you must check that you are executing the function and that it actually returns stuff.
I can’t look back anymore, but it looks like I accidentally did the last one in the console.log(). At least that was indeed the result of my console.log().