How can I streamline this?

Im trying to streamline the way leveling is handled in my game. With the current system, there are 3 different items the player can trade to an NPC to level up 1 time per trade in. 4 x itemA, 2 x itemB, 1 x itemC. The issue is that trading multiple items for several levels at once becomes tedious.

I’d like to create an event that checks for all “level items” the player has and let them cash out at once. So, if the player has 5 of itemA and 2 itemB, it levels them twice and takes the items accordingly (while leaving the spare itemA).

I currently have a system in place where each item is tied to a variable. Does anyone know how to build an event that would work as described?
The event system has a Loop command. That will do things repeatedly until you Break Loop or Jump to Label.
The event system has a Loop command. That will do things repeatedly until you Break Loop or Jump to Label.
Wow, that’s a pretty quick and effecient solution. Why didn’t I think of that!
if you want to avoid nested conditionals, you can use conditionalBranch+ to
check serveral variables in 1 swoop.

it works really good for multi checks or shorter check.
if you want to avoid nested conditionals, you can use conditionalBranch+ to
check serveral variables in 1 swoop.

it works really good for multi checks or shorter check.
Thanks. I’ll look into this as well.
There's another way that uses neither loops nor conditional branches: just use maths.

You increase the player's level by itemA / 4 + itemB / 2 + itemC
Then you set:
itemA = itemA mod 4 (gives you the remainder of itemA / 4)
itemB = itemB mod 2 (gives you the remainder of itemB / 2)
itemC = 0 (the remainder of a division by 1 is always 0)

Let's say the player has following items:
itemA: 14
itemB: 5
itemC: 1

First you compute how many levels the player will get from these items:
14 / 4 = 3 (RPG Maker always rounds variables down)
5 / 2 = 2
1 / 1 = 1

This means the player gets 3 + 2 + 1 = 6 levels.

Then you compute how many of each item the player has left:
14 mod 4 = 2
5 mod 2 = 1
1 mod 1 = 0

So the player is left with these items:
itemA: 2
itemB: 1
itemC: 0

This is also the most efficient method, especially when the player has lots of items.
There's another way that uses neither loops nor conditional branches: just use maths.

You increase the player's level by itemA / 4 + itemB / 2 + itemC
Then you set:
itemA = itemA mod 4 (gives you the remainder of itemA / 4)
itemB = itemB mod 2 (gives you the remainder of itemB / 2)
itemC = 0 (the remainder of a division by 1 is always 0)

Let's say the player has following items:
itemA: 14
itemB: 5
itemC: 1

First you compute how many levels the player will get from these items:
14 / 4 = 3 (RPG Maker always rounds variables down)
5 / 2 = 2
1 / 1 = 1

This means the player gets 3 + 2 + 1 = 6 levels.

Then you compute how many of each item the player has left:
14 mod 4 = 2
5 mod 2 = 1
1 mod 1 = 0

So the player is left with these items:
itemA: 2
itemB: 1
itemC: 0

This is also the most efficient method, especially when the player has lots of items.
I get your logic, but how would you configure the actual event?
Before I describe the actual event, I think it's important that I explain why you would want to use this solution, and why you would want to avoid it instead.

The advantage of this solution:
  • It's fast.

    No matter how many itemA, itemB, or itemC the player is carrying, this event will always take the same amount of time to complete. Using a loop to add a level up and decrease the amount of itemA, itemB, and itemC will take more time the longer you have to stay in the loop.

    However, it's possible you won't ever really need that kind of speed. It's unlikely that you'll have to loop 98 times to go straight from level 1 to level 99 in one go. And even if that happened, looping 98 times won't take long.

    It's a different story if the player can (and is likely to) level up a million times in a single exchange, however. And even then, looping a million times might still not take too long. You'd have to profile that and then decide whether the performance is acceptable or not.

The disadvantages of this solution:
  • It's kinda long.

    Due to the limitations of RPG Maker, there's no easy way to make this solution shorter without using a plugin or JavaScript.

    Ideally you'd be able to access items and variables by a variable. For example: Set a variable's value to 25, then use it to access variable #0025 or item #0025. That way, you could use a loop to handle itemA, itemB, and itemC without having to copy-paste the same code all over.

    But RPG Maker doesn't support that, so you can't use a loop, and must instead copy-paste the code for each item you have. That's why this solution is long.


  • It's harder to extend.

    You'll probably want to add a condition to check if the player has reached the maximum level, so you can stop consuming the items. With the loop solution, it's as simple as adding a conditional branch (or three) to check if the player's level has maxed out, and then interrupt the loop.

    With this solution, you'd have to calculate how far the player is from maxing out, calculate the maximum amount of itemA you can consume before maxing out, ensure you don't consume more than that limit, then use a Jump To Label... to skip the remaining items (and do that two more times for itemB and itemC).

If you still wish to use this solution, then here's how I would've configured the event:
Spoiler
You will need at least 2 variables:
  • levelup_total: how many levels to give the player.
  • levelup_items: how many levels the current item will give the player (you can reuse this for itemA, itemB, and itemC)
The event would look like this:
  1. Set levelup_total to 0.
  2. Set levelup_items to the number of itemA you have.
  3. Divide levelup_items by 4. This gives you how many levels the player gets from itemA.
  4. Increase levelup_total by levelup_items.
  5. Depending on whether itemA is a real item or just a variable:
    • If a real item:
      1. Multiply levelup_items by 4. This gives you how many itemA have been consumed.
      2. Change Items...: decrease itemA by levelup_items.
      3. You said that each item is tied to a variable, so it might make sense to set the variable for itemA to the new number of itemA you have.
    • If just a variable:
      1. Change the itemA variable by mod 4. This gives you how many of itemA remain.
  6. Repeat points 2 to 5 for itemB and itemC. Make sure you divide/multiply/mod by 2 for itemB, and by 1 for itemC (you can skip the divide/multiply operations for itemC, and instead of mod 1 you can set itemC directly to 0).
  7. Change Level... increase by levelup_total.

If "repeating points 2 to 5" for each item is too much work, you could use a common event. In this case:

Use 3 variables:
  • levelup_total
  • levelup_items
  • items_per_levelup: how many items to consume for 1 level up (e.g. 4 for itemA, 2 for itemB, 1 for itemC)
The event would look like this:
  1. Set levelup_total to 0.
  2. Set levelup_items to the number of itemA you have.
  3. Set items_per_levelup to 4.
  4. Call Common Event...
  5. Depending on whether itemA is a real item or just a variable:
    • If a real item:
      1. Change Items...: decrease itemA by levelup_items.
      2. Consider setting the itemA variable to the new number of itemA you have.
    • If just a variable:
      1. Set the itemA variable to the value of levelup_items
  6. Repeat points 2 to 5 for itemB and itemC. Make sure to set items_per_levelup to 2 for itemB and 1 for itemC.
  7. Change Level... increase by levelup_total.
The common event would look like this:
  1. Divide levelup_items by items_per_levelup.
  2. Increase levelup_total by levelup_items.
  3. Depending on whether your items are real items or just variables:
    • If real items: multiply levelup_items by items_per_levelup.
    • If just variables: change levelup_items by mod items_per_levelup.

While it doesn't seem like you're saving much work from using a common event, you're less likely to make a mistake with this approach. With both approaches you'd have to copy-paste points 2 to 5, but with the common event, you'd only have to modify points 2 and 3 instead of modifying points 2, 3 and 5.1. The less things you have to modify, the less likely it is that you'll make a mistake.

Which approach you choose is up to you, of course.

Original thread: https://forums.rpgmakerweb.com/threads/how-can-i-streamline-this.153442/