So Many Pieces – Challenge Solution

A few weeks ago I issued a challenge: populate your game with three different-colored chests, and to open then, require a player to have 50 fragments of the right color of key. We solved a single key problem all within the chest event, but here I asked you to do it with a common event that runs each time a chest is opened.

Why? Well, what if you find that 50 key fragments are just way too many to ask a player to get, and want to change it to 10 for your entire game. If you have any more than a dozen chests, you’re going to curse the day you decided to have all of that information contained in each chest event.

And we’re going to solve the problem with Ruby. Don’t be afraid! This one is easy. So let’s get to it! I’ve created three items in my database, one for each key fragment, and they are items 4, 5, and 6 (you can check for your item IDs in the game Database, it’s the number to the left of the item name). Red, blue, and green, respectively. I also need a variable, a switch, and a common event.

  • I call my variable “Key to Check” – this is different than before, when we wanted to know how many of an item the player had in their inventory. Instead, we’re storing in this variable the item ID for the chest. 4 for red, 5 for blue, you get the idea.
  • My switch is named “Chest Can Open” – if it’s on, the chest will open. If it’s off, the chest will remind us we need more key fragments.
  • My common event is called “Open Chest Check” – it’s the event that will process all chests and decide whether to open them.

I’ll set up my chest with a conditional branch, just as we did before. It’s just that the things we do in advance are a little different. We’ll need to set our “Key to Check” variable based on the color of our chest, and then we’ll need to call our common event. Finally, the conditional branch is based on our switch. Like so!

To think only a few weeks ago we were pickle hunting in this forest.

To think only a few weeks ago we were pickle hunting in this forest.

Note that because it’s a red chest, the Key To Use is set to 4 – the item ID of my Red Key Fragment. Now we build our common event. Choose “script” from the trigger editor to open a scripting window, and add the following.

Just a few lines of code will process all of our chests in the game.

Just a few lines of code will process all of our chests in the game.

Ok, let’s go through that line by line:

  • id = $game_variables[5] : here, we’re just setting whatever the value is in our “Key To Use” to an easy name, “id.” The script editor inside a common event doesn’t play nicely when you don’t name your variables.
  • key = $data_items[id] : now that we have our id, it’s easy to actually match the id up to an item. We’ll call our item “key” (even though it’s really a fragment, we don’t win points for being literal).
  • itemamt = $game_party.item_number(key) : a built-in RPG-Maker function, this allows you to find out how many of a given item the party has. In this case, we’re “giving” it key, which we defined in the line before.
  • if itemamt >= 50 : this is just like a conditional branch in the trigger editor. We’re asking, if the player has 50 or more of whatever item we’ve chosen.
  • $game_switches[1] = true : set our “Chest Can Open” switch to ON
  • $game_party.lose_item(key, 50) : another awesome built-in, we tell RPG Maker which item to lose (here, it’s “key”) and how many to lose (here, “50”)
  • else : just like the else in conditional branches in the trigger editor
  • $game_switches[1] = false : set our “Chest Can Open” switch to OFF
  • end : make sure to end your if/else statement

And that’s it! Doing it this way has two VERY nice features:

  • If you want to change the amount of key fragments from 50 to 10, you only have to change it in two places. You can probably tell where they are in the script!
  • If you want to add a yellow chest, no problem! No need to reprogram anything, just make a yellow key fragment and you’re off to the races – this common event doesn’t care what chest you’re opening.

Happy scripting!




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s