Sample: Vending Machine
|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.|
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.
Entity JSON Code
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