The RPG Maker Resource Kit

RMRK RPG Maker Creation => General Tutorials and Eventing => RPG Maker General => Tutorials Database => Topic started by: firerain on July 01, 2007, 01:45:12 PM

Title: Windows, Conditional Branches, and Scripting Tutorial
Post by: firerain on July 01, 2007, 01:45:12 PM
Okay, so, uh..lets get started. (TYPO IN TUTORIAL, I noticed I said to use armor, but I used sword by accident) (Demo at bottom).

Lets say you were making a blacksmith, you set a choice for an item to be created, but you want a window showing what is needed to make the item or whatever.Code: [spoiler]class Window_Popup < Window_Base
  def initialize
    super(0, 0, 640, 64)
    self.contents = Bitmap.new(width = 600, height = 32)
    refresh  end
  def refresh
    self.contents.clear
#----------------------------------------------
#--Edit where it has purple text, if you want--
#--to be able to have more than just the same--
#--popup window, then copy this and rename it--
#--to something unique, you'll have to rename--
#--it where ever you see where I've placed my--
#--names, like to the left, and at the top of--
#--this, and don't forget to check the event --
#--that runs the script, you'll have to      --
#--rename that aswell.  When making the event--
#--put @gold_window.dispose AFTER the text,  --
#--it's important otherwise nothing pops up. --
#--        Have Fun!                --
#----------------------------------------------
        cx = contents.text_size("Creation Items: 10 Iron Bars,50 gold").width
    self.contents.font.color = normal_color
    self.contents.draw_text(0, 0, cx, 32, "Creation Items: 10 Iron Bars,50 gold", 2)
  end
end
[/spoiler]

Step 1:Make a new event, name it Blacksmith01 or whatever you want.
[spoiler](https://rmrk.net/proxy.php?request=http%3A%2F%2Fimg524.imageshack.us%2Fimg524%2F4049%2Ftutscreen1vk8.png&hash=e9a00c079fc3dce972ea81a3981142b48d7c16d9) (http://imageshack.us)
Shot at 2007-06-30[/spoiler]
Now, put a Show Choice command and type "Make Iron Armor". Once your done that, under the "Make Iron Sword" option insert a "Script" command. Type "@gold_window = Window_Popup.new"(Explained at end of tutorial).
[spoiler](https://rmrk.net/proxy.php?request=http%3A%2F%2Fimg100.imageshack.us%2Fimg100%2F7852%2Ftutscreen2ho0.png&hash=a9de5026963b510f0ff917feda3397f2ff32d589) (http://imageshack.us)
Shot at 2007-06-30[/spoiler]
After that, put in another Show Choice option asking "Continue?" Then under the "Continue?" choice put another Show Choice command asking "Create Item".
[spoiler](https://rmrk.net/proxy.php?request=http%3A%2F%2Fimg252.imageshack.us%2Fimg252%2F9759%2Ftutscreen3db0.png&hash=881e316e5e273ea437cee7189fa070a6a8cb484d) (http://imageshack.us)
Shot at 2007-06-30[/spoiler]
Now the fun stuff, under the "Create Item" choice put a conditional branch, navigate to settings page 4 in the Conditional Branch options, check Gold or G or whatever your currency is and put 50.
[spoiler](https://rmrk.net/proxy.php?request=http%3A%2F%2Fimg523.imageshack.us%2Fimg523%2F8786%2Ftutscreen4hd9.png&hash=9d28ac59c7e28020f456f0f502f876f05a55ddb7) (http://imageshack.us)
Shot at 2007-06-30[/spoiler]
After that,in the conditional branch, put a text command saying "Here you are." Then, a Change Gold command with settings of "Operation = Decrease" "Constant = 50" Then after that, put a Change Weapons "Iron sword Increase 1".
[spoiler](https://rmrk.net/proxy.php?request=http%3A%2F%2Fimg512.imageshack.us%2Fimg512%2F9272%2Ftutscreen5qf5.png&hash=45cb69f1489a6938146c140e5c88cce0541601b8) (http://imageshack.us)
Shot at 2007-06-30[/spoiler]
Under "Else" put a "Jump To Label" command and put whatever label name you want. (Everything explained at the end of tutorial)
[spoiler](https://rmrk.net/proxy.php?request=http%3A%2F%2Fimg339.imageshack.us%2Fimg339%2F3352%2Ftutscreen6oe1.png&hash=b624338e4e57805f4af4dbd05c17288509e28a00) (http://imageshack.us)
Shot at 2007-06-30[/spoiler]
Then after the "Jump to Label" command insert a script command with "@gold_window.dispose" (Also put this wherever necessary).
[spoiler](https://rmrk.net/proxy.php?request=http%3A%2F%2Fimg253.imageshack.us%2Fimg253%2F7600%2Ftutscreen7do1.png&hash=912ccf990fbf3a909589eb65c311e9e5ad38a9fd) (http://imageshack.us)
Shot at 2007-06-30[/spoiler]
Then, at the bottom of everything, put an "Exit Event Processing" command.  Then under that, put a Label command. Put the same name as you did the Jump to Label command. Then insert text saying "Not enough gold."
[spoiler](https://rmrk.net/proxy.php?request=http%3A%2F%2Fimg141.imageshack.us%2Fimg141%2F2011%2Ftutscreen8un5.png&hash=9f06b0489d6b17b64d1913db200a2c8fdbadc60c) (http://imageshack.us)
Shot at 2007-06-30[/spoiler]
Now if you go ingame, talk to the event and you have 50 gold, select the Create Iron Armor option and voilla a new Armor.
The Result: [spoiler](https://rmrk.net/proxy.php?request=http%3A%2F%2Fimg510.imageshack.us%2Fimg510%2F8264%2Ftutscreen9jy0.png&hash=2a700ff333830dc0119cb963b36f07c3c8680341) (http://imageshack.us)
Shot at 2007-06-30[/spoiler]

EVERYTHING EXPLAINED!!

Starting with the conditional branch, I like to make my custom "shops" actually cost something :P. Which is why I used the Conditional Branch. For the Jump to Label, I used that to "jump" to the label below everything if the player did not have enough gold.
Now for the script explanation, the script is fairly simple to use, read the comments I put in it and you'll figure it out.(Put this script above "Main").[spoiler]class Window_Popup < Window_Base
  def initialize
    super(0, 0, 640, 64)
    self.contents = Bitmap.new(width = 600, height = 32)
    refresh
  end
  def refresh
    self.contents.clear
#----------------------------------------------
#--Edit where it has purple text, if you want--
#--to be able to have more than just the same--
#--popup window, then copy this and rename it--
#--to something unique, you'll have to rename--
#--it where ever you see where I've placed my--
#--names, like to the left, and at the top of--
#--this, and don't forget to check the event --
#--that runs the script, you'll have to      --
#--rename that aswell.  When making the event--
#--put @gold_window.dispose AFTER the text,  --
#--it's important otherwise nothing pops up. --
#--        Have Fun!                --
#----------------------------------------------
        cx = contents.text_size("Creation Items: 10 Iron Bars,50 gold").width
    self.contents.font.color = normal_color
    self.contents.draw_text(0, 0, cx, 32, "Creation Items: 10 Iron Bars,50 gold", 2)
  end
end
[/spoiler]
-=DEMO=-
http://s23.quicksharing.com/v/6927608/WindowDemo.rar.html (http://s23.quicksharing.com/v/6927608/WindowDemo.rar.html)
Credit to Reddawg for the script.
Title: Re: Windows, Conditional Branches, and Scripting Tutorial
Post by: Zeriab on July 01, 2007, 09:33:15 PM
Very nice tutorial ^_^

I edited the script a little bit. I feel this version is a little bit nicer to work with:
[spoiler]##
# Original script by Reddawg
# Editted by Zeriab
#
# Displayes a window popup
#
# @gold_window = Window_Popup.new
# When making the event put
# @gold_window.dispose AFTER the text,
# it's important otherwise the popup will stay.
#
class Window_Popup < Window_Base
  def initialize(text = "Creation Items: 10 Iron Bars,50 gold",
                 x = 0, y = 0, width = 640, height = 64)
    super(x, y, width, height)
    self.contents = Bitmap.new(width - 32, height - 32)
    @text = text
    refresh
  end
  def refresh
    self.contents.clear
    cx = contents.text_size(@text).width + 2
    self.contents.font.color = normal_color
    self.contents.draw_text(0, 0, cx, 32, @text, 2)
  end
end
[/spoiler]

You can use it like you do in the example.
If you want another text to be shown simply do this: @popup_window = Window_Popup.new("This is a popup window")
Remember to dispose the window when you don't want it shown anymore. (@popup_window.dispose)

~ Zeriab
Title: Re: Windows, Conditional Branches, and Scripting Tutorial
Post by: firerain on July 01, 2007, 10:05:40 PM
Oh, cool. Thanks  :)
Title: Re: Windows, Conditional Branches, and Scripting Tutorial
Post by: Kokowam on July 01, 2007, 10:11:36 PM
Nice. That's a lot of screenshots. I'd get tired of doing that. XD
Title: Re: Windows, Conditional Branches, and Scripting Tutorial
Post by: Rune on July 10, 2007, 07:00:54 PM
Nice :P You learn something new everyday :P

Now I know how to call windows on the map :D
Title: Re: Windows, Conditional Branches, and Scripting Tutorial
Post by: firerain on July 10, 2007, 10:42:51 PM
Quote from: Rune on July 10, 2007, 07:00:54 PM
Nice :P You learn something new everyday :P

Now I know how to call windows on the map :D
Your welcome :)
Title: Re: Windows, Conditional Branches, and Scripting Tutorial
Post by: shaz on July 10, 2007, 11:37:12 PM
I don't think your dispose is in the right spot.  If the player says No when given the Continue, No options, the window has been created but will not be disposed.  If the player chooses Create Item, the window will not be disposed.  If the player chooses Create Item but doesn't have enough gold, the Jump To NoGold will be executed before the window is disposed.  So I don't think your @gold_window.dispose command will ever be executed where it currently is.  You create the window before the Continue/No choice, so you should dispose the window after that whole block is completed.

Also, (I think) unless you've got extra processing in there, you don't even need the jump to when there's not enough gold - you could just pop your message in there right where the condition evaluates to false, and the if/else branching will take it straight to the bottom of the events anyway.  If you kept in the Jump, you'd need a second @gold_window.dispose to execute right before the jump, or right at the spot you're jumping to.

Here's a slightly modded version showing the above changes
[spoiler](https://rmrk.net/proxy.php?request=http%3A%2F%2Fwww.users.on.net%2F%7Emszhanlon%2Fsh%2Fforum%2FRMXP%2Frmrk18670.JPG&hash=76d8af6fd168cbf8bbcf479bbb97de4591f28fa7)[/spoiler]
If you think I've got something wrong please let me know - I still have a lot to learn.
Title: Re: Windows, Conditional Branches, and Scripting Tutorial
Post by: firerain on July 11, 2007, 01:33:52 AM
If you read the whole thing, you would've noticed I told you to put the dispose wherever else necessary. 

And the  JumpToLabel, I know about that already.
Title: Re: Windows, Conditional Branches, and Scripting Tutorial
Post by: Rune on July 11, 2007, 06:40:00 PM
Quote from: Lac on July 10, 2007, 10:42:51 PM
Quote from: Rune on July 10, 2007, 07:00:54 PM
Nice :P You learn something new everyday :P

Now I know how to call windows on the map :D
Your welcome :)

Oh crap :P

Thanks a lot :P This'll help my games a bit :P