I am currently trying to find a way to allow a function within an object to run once the object is called.
For example, take the following Object...
const textNodes = [
{
id: 1,
faction: "Greaser",
text: "Do you want to help this person?",
options: [
{
text: "Yes",
nextText: 2
},
{
text: "No",
nextText: 2
},
]
},
{
id: 2,
faction: "greaser",
text: "It was a trap, do you run or kill them?",
options: [
{
text: "Run",
nextText: 3,
damage: 0,
},
{
text: "Kill",
nextText: 5,
damage: 8,
moneyDrop: 20,
radiationDamage: 2,
logic: function update() {
console.log("update function worked")
if(character.greaser == true && this.faction == "greaser"){
console.log("greaser check worked");
}
character.health -= this.damage;
character.cash += this.moneyDrop;
character.radiationLevel += this.radiationDamage;
character.maxHP -= this.radiationDamage;
if(this.radiationDamage >= 1 ){
addRadiatedStatus();
}
console.log("character HP" + " " + character.health);
console.log("character maxHP" + " " + character.maxHP);
console.log("moneyDrop " + " " + this.moneyDrop);
console.log("character cash" + " " + character.cash);
console.log("radiation level" + " " + character.radiationLevel);
console.log("character Raditated?" + " " + character.radiated);
},
},
]
}
]
As you can see , I have a function update()
that is going to change a character object once the function is called.
I am currently calling the function like... textNodes[1].options[1].logic();
Of course, I do not want to do this, because I am building a rpg decision game. The user will be given a question and some choices, and allowed to create their own path. If the user gets 3 options, and the user picks option 2, I will run option 2's logic()
/ update()
function.
Each game choice will have a function attached to it that does something different, and each time that I choose the option, I want the function to run automatically. I am not sure if this is even possible, but it seems like the most logical option.
The card decision game I am building is similar to https://www.youtube.com/watch?v=ByBUsBxPrZ4.
as you can see in this video, each card that is chosen ( tinder app style ) will update the game and the characters stats.
I want those update function to run as soon as an object option is called. I am not sure if this is possible, and a peer told me my hint to you is to use classes
, but I do not often use classes unless I am coding in java, which I do not like frankly.
Sorry if this is hard to follow and thanks in advance.
Here is what I have tried.
const textNodes = [
{
id: 1,
faction: "Greaser",
text: "Do you want to help this person?",
options: [
{
text: "Yes",
nextText: 2
},
{
text: "No",
nextText: 2
},
]
},
{
id: 2,
faction: "greaser",
text: "It was a trap, do you run or kill them?",
options: [
{
text: "Run",
nextText: 3,
damage: 0,
},
{
text: "Kill",
nextText: 5,
damage: 8,
moneyDrop: 20,
radiationDamage: 2,
logic: function update(){
console.log("update function worked")
if(character.greaser == true && this.faction == "greaser"){
console.log("greaser check worked");
}
character.health -= this.damage;
character.cash += this.moneyDrop;
character.radiationLevel += this.radiationDamage;
character.maxHP -= this.radiationDamage;
if(this.radiationDamage >= 1 ){
addRadiatedStatus();
}
console.log("character HP" + " " + character.health);
console.log("character maxHP" + " " + character.maxHP);
console.log("moneyDrop " + " " + this.moneyDrop);
console.log("character cash" + " " + character.cash);
console.log("radiation level" + " " + character.radiationLevel);
console.log("character Raditated?" + " " + character.radiated);
},
run2: () =>{
this.logic();
}
},
]
}
]