Page 1 of 6

Shop Example Script

Posted: June 14th, 2009, 7:48 pm
by GoBologna120
I made a script that could help people set up a basic shop and inventory system. I based a lot of the code off of the village map, but I only used it for reference. You can buy 3 things at the shop. A pie, a sword, and a treasure. Level trigger 1 is a coin pickup, and level trigger 2 is the shopkeeper.

Code: Select all

"on_start" = [
	money = 0
	pie = 0
	sword = 0
	treasure = 0
	]

"level_trigger_1" = [ money = ( + $money 5 ) 
	echo "You got 5 moneys."
	]

"level_trigger_2" = "showgui Shopkeeper"

newgui Shopkeeper [
	guitext "What're ya buyin, stranger?" chat
	guibar
	guilist [
		guibutton "Pie: 10 moneys" [
			if ( > $money 9 ) [
				money = ( - $money 10 )
				pie = ( + $pie 1 )
			]
		]
		guibar
		guibutton "Sword: 20 moneys" [
			if ( > $money 19 ) [
				money = ( - $money 20 )
				sword = ( + $sword 1 )
				]
			]
		guibar
		guibutton "Treasure: 40 moneys" [
			if ( > $money 39 ) [
				money = ( - $money 40 )
				treasure = ( + $treasure 1 )
				]
			]
	]
]

newgui Inventory [
guibutton "Back" "cleargui 1"
guibar
guitext ( format "You have %1 moneys in your wallet." $money )
guitext ( format "You have %1 pies.  Yum." $pie )
guitext ( format "You have %1 swords.  Not that you can use them..." $sword )
guitext ( format "You have %1 treasures.  Lucky you!" $treasure )
]

newgui main [
	guilist [
		guilist [
			guibutton "Inventory" "showgui Inventory"
		]
	]
	guibar
	@main
]
I tested it out and it works perfectly. Have fun. ;)

Re: Shop Example Script

Posted: June 14th, 2009, 7:57 pm
by Mike
Thanks for posting this!! Maybe we should add some of this stuff to the wiki, like a Scripting help template page. Thanks again! Take care.
-mike

Re: Shop Example Script

Posted: June 14th, 2009, 7:59 pm
by GoBologna120
It's good to know that I'm contributing something. :mrgreen:
The only thing I'm not sure of at the moment is how to have the shopkeeper tell you if you don't have enough money.

Re: Shop Example Script

Posted: June 14th, 2009, 11:29 pm
by Dannyboi
Once again you have outdone yourself my friend! Good job!!

Thanks
Danny

Re: Shop Example Script

Posted: June 14th, 2009, 11:31 pm
by Mike
Have you looked at the village.cfg ? Very interesting code Hirato has in there :) Let me know if you have any questions. Take care.
-mike

Re: Shop Example Script

Posted: June 14th, 2009, 11:33 pm
by Dannyboi
Ill do that i've heard Hirato has some good scripts/codes i look forward to seeing them.

Thanks
Danny

Re: Shop Example Script

Posted: June 15th, 2009, 5:52 am
by Hirato
run sandbox recently? :P the menus are all my scripts, so is some of the interface (namely the green text in the edit hud)

I've a shop script lying around here somewhere... didn't work too well, poorly optimised and I've never gotten around to redoing it.. I'll take a look around for it (but really, it''s bad :P)

Re: Shop Example Script

Posted: June 15th, 2009, 2:49 pm
by GoBologna120
Mike wrote:Have you looked at the village.cfg ? Very interesting code Hirato has in there :) Let me know if you have any questions. Take care.
-mike
I believe in the first post I said that I based a lot of code off the village cfg. :lol: But just for reference. I typed all of it up myself. Looking at the village cfg really helped me figure out a lot. It was a great learning tool.

On an unrelated note, the smilies here look really odd. Most smilies don't have chins.

Re: Shop Example Script

Posted: June 15th, 2009, 3:06 pm
by Dannyboi
Well I looked at the code, but it looks a little complicated haha...mind helping me out?

Re: Shop Example Script

Posted: June 15th, 2009, 4:50 pm
by GoBologna120
Alright. Let me break it down for you. Let's start with the best place to start.

Code: Select all

"on_start" = [
   money = 0
   pie = 0
   sword = 0
   treasure = 0
   ]
I crack myself up sometimes. :lol: Anyway, what this is doing is setting all inventory items to 0 at the start of the map. Next we have:

Code: Select all

"level_trigger_1" = [ money = ( + $money 5 ) 
   echo "You got 5 moneys."
   ]
This makes it so that every time you hit an object assigned to level_trigger_1, you get 5 "moneys". Next:

Code: Select all

"level_trigger_2" = "showgui Shopkeeper"
This makes level_trigger_2, AKA the shopkeeper, talk. Now we've gotta make him say something, right?

Code: Select all

newgui Shopkeeper [
   guitext "What're ya buyin, stranger?" chat
   guibar
   guilist [
      guibutton "Pie: 10 moneys" [
         if ( > $money 9 ) [
            money = ( - $money 10 )
            pie = ( + $pie 1 )
         ]
      ]
      guibar
      guibutton "Sword: 20 moneys" [
         if ( > $money 19 ) [
            money = ( - $money 20 )
            sword = ( + $sword 1 )
            ]
         ]
      guibar
      guibutton "Treasure: 40 moneys" [
         if ( > $money 39 ) [
            money = ( - $money 40 )
            treasure = ( + $treasure 1 )
            ]
         ]
   ]
]
This part gets a little tricky. First, it makes the shopkeeper say "What're ya buyin, stranger?". Then it shows a menu underneath. The menu has 3 items. Pie, sword, and treasure. Therefore, we must make buttons that tell the item and how much it costs. After we tell what the button says, you have to make it do something when it's pressed. For this, you have to make sure the player has enough money first. If so, your money decreases by the amount of the price and your item increases by 1. Now how do we view the inventory?

Code: Select all

newgui Inventory [
guibutton "Back" "cleargui 1"
guibar
guitext ( format "You have %1 moneys in your wallet." $money )
guitext ( format "You have %1 pies.  Yum." $pie )
guitext ( format "You have %1 swords.  Not that you can use them..." $sword )
guitext ( format "You have %1 treasures.  Lucky you!" $treasure )
]

newgui main [
   guilist [
      guilist [
         guibutton "Inventory" "showgui Inventory"
      ]
   ]
   guibar
   @main
]
First we need to define what the inventory will have in it. So first we put in a Back button for obvious purposes. :lol: Next we have to tell the gui to put in some text telling what you have. So we use guitext to do that. Now hold it. You have to type format before your text for this to work. That way, it'll read the %1 as the item you specified right after the text. The last part of the code just puts the inventory button in the main menu.

Whew. That took a while.