Hello everyone, I’m writing on a code that calculates how much money the player has. At the moment, I have a problem: I want other game objects to send a message (SendMessage (“Money”, 50.0) to the money script.
Here’s the part of the code that receives the messages and takes the sum of the received money and that one the player already had:
private var gold = float;
private var number = float;
function Money (number : float) {
inventoryBoolean = true;
audio.PlayOneShot(moneySound, 0.2);
if (number > 0) {
number + gold;
}
}
Interesting… If I interpret what you want to do correctly, you have the gold variable to hold your player’s current money and the Money method is suppose to increment it by number. I would write this as:
private var gold : float;
function Money (number : float) {
inventoryBoolean = true;
audio.PlayOneShot(moneySound, 0.2);
if (number > 0) {
gold += number;
// OR gold = number + gold
}
}
Your code raises a question for me though. Does anyone know if the original code would have two “number” variables in Javascript? IE. I would expect that
private var number : Float;
Would be a class variable and:
function Money (number : float) {
Would be a separate method variable.
Anyone know how this is implemented in the language?
Whoops, I missed that one when I wrote my first response. I believe that within that function, all references to ‘number’ would use the local variable named ‘number’ rather than the class variable.
So galent’s version of the function will actually work better, with:
gold = number + gold;
and ‘gold’ being the variable in which the player’s total money is tracked.