How do I make an affection system in MZ?

Hey guys, how are you all? So, my current problem is that I want to create an affection system to my game where, when you met the requisites, the affection with x character levels up. I have been using variables to determine each character's affection and conditional branches to raise them.

Now, for the problem: I want to set a system where, if you raise this variable to a certain point, you receive a reward. You would collect these rewards by speaking to them.

The issue is, I don't knoww how to do that... I thought of using conditional branches to give the player the reward but, this would cause the event to give infinite rewards. To circuvent this, I thought of the following method: Create a second variable and increase its value each time the player collects the reward and, create two conditional branches to give the reward. Example:

Player receive affection level 1 -> collect reward -> second variable goes to value 1
Player has level 1 affection and tries to receive the same reward again -> conditional branch that only activates when the value of the second variable is 0 forces the event to exit without giving another item.
Second level reward needs the second variable to be value 1 or otherwise, it won't activate.

(to be honest, I just thought of this method while writing this)

Is this the most efficient way? If I have 20 different characters, this wwill be 40 variables just for this system. So, will this suffice or do you guys have another suggestion to help?
Before I can begin to offer any good advice, I need to know some details. Namely, how do you have this system set up to start? I know you're using variables, but how many are you using and how high do affection levels go? Do you get a reward at every level or only certain ones?
Before I can begin to offer any good advice, I need to know some details. Namely, how do you have this system set up to start? I know you're using variables, but how many are you using and how high do affection levels go? Do you get a reward at every level or only certain ones?
The system starts after you complete the character's quest and this completion unlocks other requests (side quests) that raise this affection. Affection will reach level 10 and you get a reward in varying levels. In one character, the reward is given at level 2,4,6,8,10 and in another could be: 1,3,5,7,10.

As for how many I'm using, I'm planning to use 2 per character, one that is the affection level and the other is the reward granted variable (used to deactivate to avoid infinite items and giving access to the folloing reward)
You could use a data structure, such as an array or a dictionary, to track the rewards that the player has received. This way, you can store all of the information about the rewards in a single data structure, rather than using separate variables for each character and reward level.

For example, you could create an array with one element for each character, and each element could be a dictionary that maps reward levels to Booleans indicating whether the player has received that reward. Then, you could use a single loop to check whether the player has received each reward, and you could use a single conditional branch to give the reward if the player has not received it yet.

Code:
# Set up the array and dictionaries
$game_party.affection_rewards = []
for i in 0...$game_actors.size
  $game_party.affection_rewards[i] = {}
end

# Check whether the player has received a reward
actor = $game_actors[x]  # Replace x with the actor's ID
level = y  # Replace y with the reward level
if !$game_party.affection_rewards[actor][level]
  # Give the reward
  $game_party.affection_rewards[actor][level] = true
  # Add code here to give the reward to the player
end

This approach reduces the number of variables that you need to track the rewards from 40 (20 characters * 2 variables per character) to just 2 (one array and one dictionary). It also makes it easier to add or remove characters or reward levels, since you don't need to create or delete variables manually.
I'm not sure if the plugin would meet your need but @lordvalinar has a Friendship Romance System plugin that might have the functionality you want and saves you re-inventing the wheel (or at least provide inspiration.)
The system starts after you complete the character's quest and this completion unlocks other requests (side quests) that raise this affection. Affection will reach level 10 and you get a reward in varying levels. In one character, the reward is given at level 2,4,6,8,10 and in another could be: 1,3,5,7,10.

As for how many I'm using, I'm planning to use 2 per character, one that is the affection level and the other is the reward granted variable (used to deactivate to avoid infinite items and giving access to the folloing reward)
If you're already using two variables, this is a simple matter. Rather than employing a conditional branch, just use different event pages that activate based on your variables. That way you only ever receive a reward once, then the new event page ensures a copy won't be given because that old page is no longer applicable.
I'm not sure if the plugin would meet your need but @lordvalinar has a Friendship Romance System plugin that might have the functionality you want and saves you re-inventing the wheel (or at least provide inspiration.)
I will give it a look, thanks!

If you're already using two variables, this is a simple matter. Rather than employing a conditional branch, just use different event pages that activate based on your variables. That way you only ever receive a reward once, then the new event page ensures a copy won't be given because that old page is no longer applicable.
Really? I thought it would activate since the value the pages use is equal or higher. This way, if I have 10 affection, he would go through the whole list unless I forced the exit of the process.

Like this:

1672110830151.png
You could use a data structure, such as an array or a dictionary, to track the rewards that the player has received. This way, you can store all of the information about the rewards in a single data structure, rather than using separate variables for each character and reward level.

For example, you could create an array with one element for each character, and each element could be a dictionary that maps reward levels to Booleans indicating whether the player has received that reward. Then, you could use a single loop to check whether the player has received each reward, and you could use a single conditional branch to give the reward if the player has not received it yet.

Code:
# Set up the array and dictionaries
$game_party.affection_rewards = []
for i in 0...$game_actors.size
  $game_party.affection_rewards[i] = {}
end

# Check whether the player has received a reward
actor = $game_actors[x]  # Replace x with the actor's ID
level = y  # Replace y with the reward level
if !$game_party.affection_rewards[actor][level]
  # Give the reward
  $game_party.affection_rewards[actor][level] = true
  # Add code here to give the reward to the player
end

This approach reduces the number of variables that you need to track the rewards from 40 (20 characters * 2 variables per character) to just 2 (one array and one dictionary). It also makes it easier to add or remove characters or reward levels, since you don't need to create or delete variables manually.
Hey buddy, thank you for the response.

Sorry but, I am a complete noob at js and scripts so, you kind of lost me there. My first doubt is the "$game_party.affection_rewards".

I didn't quite understante what you meant with "for i in 0...$game_actors.size" what does this mean and what should I put in place of the ""?

The rest I think I understood. If the reward is an item, in the final part of the code I should use something like: "$gameParty.gainItem(item, amount);" right? Or is this code wrong?
I will give it a look, thanks!


Really? I thought it would activate since the value the pages use is equal or higher. This way, if I have 10 affection, he would go through the whole list unless I forced the exit of the process.

Like this:

View attachment 249097
Oops, I forgot to account for this in my post. I meant to add that you will have to arrange the event pages in reverse order. By that I mean instead of having page 1 be the default state of the NPC, have page 10 be the default state instead. I'm reasonably certain that will work.
Oops, I forgot to account for this in my post. I meant to add that you will have to arrange the event pages in reverse order. By that I mean instead of having page 1 be the default state of the NPC, have page 10 be the default state instead. I'm reasonably certain that will work.
Yeah, that way he would begin at the highest level and it would go descending until it finds the current level of affection. To avoid repeat, I would use a "exit event processing".

I may be mistaken but, I think if I did it this way, it would end up skipping levels. For example, if I have level 4 and didn't talk with the NPC until then, I would earn the reward for this level and lose the past ones.

A way to work around that would be to force an interaction as soon as the quest is completed... Or, I could try to mess a bit with self-switches and conditional branches to make the player receive all the rewards once he talks with the NPC (I prefer this method because it's more exciting to earn a lot of things at once than earning one-by-one. But, that's just my opinion)
Apparently some of the fellows have forgotten the first principle in programming. If it works, do not mess with it. Otherwise I have no idea why people suggest code or plugins in a place where there already is a running engine.
Is this the most efficient way? If I have 20 different characters, this wwill be 40 variables just for this system. So, will this suffice or do you guys have another suggestion to help?
This is not the most efficient, but is more than sufficient. It uses 2 variables per character, but we aren't in times when we need to conserve every byte anymore.
If you wanted to use a single variable, there would be a couple ways to do that. My favourite one is to begin the levels from 100 instead of 1 and use numbers 0-99 to track the level of received rewards. But this already complicates things in an unnecessary fashion.
Apparently some of the fellows have forgotten the first principle in programming. If it works, do not mess with it. Otherwise I have no idea why people suggest code or plugins in a place where there already is a running engine.
If it speeds up the process or makes things simpler, you'll find a lot of people will recommend a plugin. Nothing new or strange about that.

Original thread: https://forums.rpgmakerweb.com/threads/how-do-i-make-an-affection-system-in-mz.153530/