Sample: Monster 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 Monster Machine demonstrates how to spawn a random monster from a list, including custom monsters. It is recommended to load Male Spider, Kitsune, and Pig King before using the machine. Note that this sample requires build r25 or later to run.

Author: SkyCorp

Entity JSON Code

{
	"type": "ENTITY",
	"name":
	{
		"literalString": "Monster Machine"
	},
	"description":
	{
		"literalString": "A simple machine with a button that spits out a monster each time it is pressed."
	},
	"doUse":
	{
		"programBoolean": true
	},
	"isUseable":
	{
		"literalBoolean": true
	},
	"isPickupable":
	{
		"literalBoolean": true
	},
	"luac":  "G0x1YVEAAQQEBAgAFAAAAEBtb25zdGVyTWFjaGluZS5sdWEAAAAAAAAAAAAAAAICAwAAACQAAAAHAAAAHgCAAAEAAAAEBgAAAGRvVXNlAAEAAAAAAAAAAQAAACUAAAAAAAAFOQAAAAoAAAJBQAAAgYAAAMHAAAABAQEAIkAAAgcAAAAFgAEABkBBAEUAAAAcgAABB0ABAAVAAQAUAAAAB8ABAAVAAgAGgEIAQcACAIXAAQAcgIABBwACAAUAAABFAAIABkAAAAcAAwAFgAMABsBDAEUAAwAcgAABB0ADAAVAAwAXAEQAFsABgAVABAAGgEQAQcAEAIUAAwDBAAUAVcCAABxAAAEWAAOABUAEAAaARABBQAUAhUADAIaARQGcgIAAwcAFAAEBBgBVAIEAHEAAAQVAAwAGQEYAHECAAAIAgAAeAAABHgCAABoAAAAECQAAAG1vbnN0ZXJzAAQMAAAATWFsZSBTcGlkZXIABAgAAABLaXRzdW5lAAQJAAAAUGlnIEtpbmcABAwAAABGcm9nIE1pbmlvbgAEBQAAAGtleXMABAYAAAB0YWJsZQAEEQAAAG51bWJlck9mTW9uc3RlcnMABBMAAAByYW5kb21Nb25zdGVySW5kZXgABAUAAABtYXRoAAQHAAAAcmFuZG9tAAMAAAAAAADwPwQQAAAAcmFuZG9tTW9uc3RlcklEAAQIAAAAbW9uc3RlcgAEBwAAAEVudGl0eQAECQAAAGdlbmVyYXRlAAAECwAAAE1haW5TY3JlZW4ABAwAAABhZGRHYW1lVGV4dAAEJwAAAFlvdSBwcmVzcyB0aGUgYnV0dG9uLCBidXQgYmVjYXVzZSB0aGUgAAQlAAAAIG1vZCBpcyBub3QgbG9hZGVkLCBub3RoaW5nIGhhcHBlbnMuAAQDAAAAQSAABAgAAABnZXROYW1lAAQMAAAAIHN0ZXBzIG91dCAABCQAAABvZiB0aGUgbWFjaGluZSBhbmQgaW50byB0aGUgcm9vbS4KCgAEHQAAAG1vdmVFbnRpdHlUb1NhbWVSb29tQXNQbGF5ZXIAAAAAADkAAAAHAAAABwAAAAcAAAAHAAAABwAAAAcAAAAHAAAACgAAAAoAAAAKAAAACgAAAAoAAAALAAAACwAAAAsAAAANAAAADQAAAA0AAAANAAAADQAAAA0AAAAOAAAADgAAAA4AAAAOAAAAFQAAABUAAAAVAAAAFQAAABUAAAAXAAAAFwAAABcAAAAYAAAAGAAAABgAAAAZAAAAGQAAABkAAAAYAAAAGQAAABsAAAAbAAAAGwAAABsAAAAbAAAAGwAAABsAAAAcAAAAHAAAABsAAAAhAAAAIQAAACEAAAAkAAAAJAAAACUAAAAAAAAAAAAAAAMAAAAlAAAAAQAAACUAAAAAAAAAAAAAAA=="
}

LUA Source Code

function doUse()
	-- This function will randomly spawn one monster type.  Note that since
	-- this is just a sample, monsters will stack up, and the area around this
	-- object will become incredibly dangerous if pressed multiple times ;)
	
	-- List of monster IDs to spawn
	monsters = {"Male Spider", "Kitsune", "Pig King", "Frog Minion"};

	-- Choose a random monster ID
	keys = table.keys(monsters);
	numberOfMonsters = #keys;

	randomMonsterIndex = math.random(1, numberOfMonsters);
	randomMonsterID = monsters[randomMonsterIndex];

	-- Attempt to generate the monster
	-- This uses the 'id' parameter of the monster code.  If it does not exist
	-- yet, you will need to add it (as of Feb 2018, the SCLAB tool does not
	-- automatically generate the 'id' field.  It should usually be the same
	-- as the wiki name of your mod to prevent collisions with other mods).
	monster = Entity.generate(randomMonsterID);

	if monster == nil then
		MainScreen.addGameText("You press the button, but because the " .. 
			randomMonsterID .. " mod is not loaded, nothing happens.");
	else
		MainScreen.addGameText("A " .. monster.getName() .. " steps out " ..
			"of the machine and into the room.\n\n");

		-- The monster spawner may be in player inventory or in a room.  Either
		-- way, they are in the same room as the monster spawner, so this
		-- shortcut function can be used:
		monster.moveEntityToSameRoomAsPlayer();
	end

	return true; -- Automatic continue scene
end