Sample: Vending Machine

From SkyCorp Global
Note - this entity is a modification for The Underworld. Visit the new Mods Portal for more mods, mods on the wiki are no longer maintained. You can put it in-game by visiting the Debug Mod Control Panel in the debug rooms and browse to this mod or copy/paste the JSON below into the program window.

Entity Details

This Vending Machine demonstrates how an entity can spawn a different entity. You should also load the Soda entity before using the vending machine. Note that this sample requires build r25 or later to run.

Author: SkyCorp

Entity JSON Code

{
	"type": "ENTITY",
	"name":
	{
		"literalString": "Vending Machine"
	},
	"description":
	{
		"literalString": "The vending machine advertises $1 for one diet soda."
	},
	"doUse":
	{
		"programBoolean": true
	},
	"isUseable":
	{
		"literalBoolean": true
	},
	"luac":  "G0x1YVEAAQQEBAgAFAAAAEB2ZW5kaW5nTWFjaGluZS5sdWEAAAAAAAAAAAAAAAICAwAAACQAAAAHAAAAHgCAAAEAAAAEBgAAAGRvVXNlAAEAAAAAAAAAAQAAACYAAAAAAAADKAAAAAVAAAAGgEAAHICAAAcAAAAFAAAABsBAAEEAAQAcgAABGgAAABaABYAFgAEABsBBAEEAAgAcgAABB0ABAAVAAQAXQEIAFgABgAWAAgAGwEIAQQADABxAAAEWQAOABYACAAbAQgBBQAMAgYADAFWAgAAcQAABBUABAAbAQwAcQIAAFsAAgAWAAgAGwEIAQQAEABxAAAECAIAAHgAAAR4AgAARAAAABAcAAABwbGF5ZXIABAcAAABQbGF5ZXIABAwAAABnZXRJbnN0YW5jZQAECwAAAHJlbW92ZUNhc2gAAwAAAAAAAPA/BAUAAABzb2RhAAQHAAAARW50aXR5AAQJAAAAZ2VuZXJhdGUABAwAAABTYW1wbGUgU29kYQAABAsAAABNYWluU2NyZWVuAAQMAAAAYWRkR2FtZVRleHQABCYAAABQbGVhc2UgYWxzbyBsb2FkIHRoZSBTYW1wbGUgU29kYSBtb2QuAAQqAAAAWW91IGluc2VydCAkMSBpbnRvIHRoZSB2ZW5kaW5nIG1hY2hpbmUuICAABCEAAABJdCBzcGl0cyBvdXQgYSBjYW4gb2YgZGlldCBzb2RhLgAEHQAAAG1vdmVFbnRpdHlUb1NhbWVSb29tQXNQbGF5ZXIABCQAAABZb3UgbmVlZCBhdCBsZWFzdCAkMSB0byBidXkgYSBzb2RhLgAAAAAAKAAAAAcAAAAHAAAABwAAAAcAAAAJAAAACQAAAAkAAAAJAAAACQAAAAkAAAAQAAAAEAAAABAAAAAQAAAAEAAAABIAAAASAAAAEgAAABMAAAATAAAAEwAAABMAAAATAAAAFQAAABUAAAAVAAAAFgAAABYAAAAVAAAAHAAAABwAAAAcAAAAHQAAACIAAAAiAAAAIgAAACIAAAAlAAAAJQAAACYAAAAAAAAAAAAAAAMAAAAmAAAAAQAAACYAAAAAAAAAAAAAAA=="
}

LUA Source Code

function doUse()
	-- NOTE: If creating a full store, it would probably be better to use the
	-- existing store code, however as of Feb 2018 this is not yet exposed to
	-- LUA modding.  However, this is fine for a 'shop' that only sells one
	-- item.

	player = Player.getInstance();

	if player.removeCash(1) then
		-- Player has enough currency to cover the transaction, and it was
		-- deducted successfully.

		-- This matches the id field in entity json.  Generally best to use the
		-- same name as your mod wiki page to prevent conflicts between multiple
		-- mods.
		soda = Entity.generate("Sample Soda");

		if soda == nil then
			MainScreen.addGameText("Please also load the Sample Soda mod.");
		else
			MainScreen.addGameText('You insert $1 into the vending machine.  ' ..
				'It spits out a can of diet soda.');

			-- Set location to be the same location as vending machine.
			-- Since the player must be next to the vending machine to use it,
			-- this function works fine.  Another (longer) method would be to get
			-- the vending machine's location and set the soda's location to it.			
			soda.moveEntityToSameRoomAsPlayer();
		end
	else
		-- Player didn't have enough currency to cover the total cost of transaction.
		-- No currency was deducted.
		
		MainScreen.addGameText('You need at least $1 to buy a soda.');	
	end

	return true; -- Automatic continue scene
end