Hi! Welcome to the forum for Platinum Arts Sandbox Free 3D Game Maker. I currently have the forums locked as I am attempting to properly update them.

In the meantime please join the new Discord Server!

If you have any questions please e-mail me through the Platinum Arts website.

Shop Example Script

Learn more on how to use Sandbox, or submit your own tutorials or resources.
User avatar
GoBologna120
Member
Member
Posts: 167
Joined: June 10th, 2009, 3:07 pm
Name: Tim
IRC Username: GoBologna
Location: Nowhere in particular.

Shop Example Script

Post 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. ;)
Image
Yay. ^_^
User avatar
Mike
Administrator
Administrator
Posts: 871
Joined: May 24th, 2009, 12:52 pm

Re: Shop Example Script

Post 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
Sign up for our Newsletter to keep up to date with the Sandbox news!
We also have a facebook page, facebook group, myspace page and a twitter page[/b][/color]!
User avatar
GoBologna120
Member
Member
Posts: 167
Joined: June 10th, 2009, 3:07 pm
Name: Tim
IRC Username: GoBologna
Location: Nowhere in particular.

Re: Shop Example Script

Post 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.
Image
Yay. ^_^
User avatar
Dannyboi
Member
Member
Posts: 98
Joined: June 14th, 2009, 5:00 pm
Name: Danny
Location: Temple TX

Re: Shop Example Script

Post by Dannyboi »

Once again you have outdone yourself my friend! Good job!!

Thanks
Danny
I'm BAAAACK!!! >:D
User avatar
Mike
Administrator
Administrator
Posts: 871
Joined: May 24th, 2009, 12:52 pm

Re: Shop Example Script

Post 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
Sign up for our Newsletter to keep up to date with the Sandbox news!
We also have a facebook page, facebook group, myspace page and a twitter page[/b][/color]!
User avatar
Dannyboi
Member
Member
Posts: 98
Joined: June 14th, 2009, 5:00 pm
Name: Danny
Location: Temple TX

Re: Shop Example Script

Post by Dannyboi »

Ill do that i've heard Hirato has some good scripts/codes i look forward to seeing them.

Thanks
Danny
I'm BAAAACK!!! >:D
Hirato
Developer
Developer
Posts: 689
Joined: May 30th, 2009, 1:23 pm
IRC Username: hirato

Re: Shop Example Script

Post 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)
This is not a url, clicking it is pointless
User avatar
GoBologna120
Member
Member
Posts: 167
Joined: June 10th, 2009, 3:07 pm
Name: Tim
IRC Username: GoBologna
Location: Nowhere in particular.

Re: Shop Example Script

Post 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.
Image
Yay. ^_^
User avatar
Dannyboi
Member
Member
Posts: 98
Joined: June 14th, 2009, 5:00 pm
Name: Danny
Location: Temple TX

Re: Shop Example Script

Post by Dannyboi »

Well I looked at the code, but it looks a little complicated haha...mind helping me out?
I'm BAAAACK!!! >:D
User avatar
GoBologna120
Member
Member
Posts: 167
Joined: June 10th, 2009, 3:07 pm
Name: Tim
IRC Username: GoBologna
Location: Nowhere in particular.

Re: Shop Example Script

Post 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.
Image
Yay. ^_^
Post Reply