1
Hello, I’m creating a mini rpg in javascript and I’m having a question in the character creation part, I would like to know how to limit the number of uses of a function.
var personagem = new Object;
personagem.carisma = 1;
personagem.força = 1;
personagem.inteligencia = 1;
window.onload = init;
function init () {
var button1 = document.getElementById('char');
var button2 = document.getElementById('for');
var button3 = document.getElementById('int');
button1.onclick = buttonClickCarisma;
button2.onclick = buttonClickForce;
button3.onclick = buttonClickInt;
}
function buttonClickCarisma () {
if( personagem.carisma < 3 ) {
personagem.carisma = personagem.carisma + 1;
console.log(personagem.carisma);
}
else {
console.log('Seu carisma está no máximo!');
}
}
function buttonClickForce () {
if( personagem.força < 3 ) {
personagem.força = personagem.força + 1;
console.log(personagem.força);
}
else {
console.log('Sua força está no máximo!');
}
}
function buttonClickInt () {
if( personagem.inteligencia < 3 ) {
personagem.inteligencia = personagem.inteligencia + 1;
console.log(personagem.inteligencia);
}
else {
console.log('Sua inteligência está no máximo!');
}
}
This piece of code demonstrates the creation of characters, where I would like to limit the number of use of the functions buttonClickCarisma, buttonClickForce and buttonClickInt to only 3 uses ( if I use a 3 times I can not any other ), something similar to creating characters in games in the Fallout saga. Thank you in advance.
could use a variable to count the times the function was used, and each time it executes the function increment or decrement that variable!
– anderson seibert
It is even better to limit the maximum values of each character of the player, which is what the code already does. Limiting the number of times a function performs is something you don’t usually see in code because it’s usually the wrong way to approach the problem.
– Isac