Sample: Radio

From SkyCorp Global
Jump to: navigation, search
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 sample demonstrates communication between entities, including custom user-defined LUA functions on those entities.

Author: SkyCorp

Radio

Entity JSON Code

{
	"type": "ENTITY",
	"name":
	{
		"literalString": "Radio"
	},
	"description":
	{
		"programString": true
	},
	"customPublicFunctions": ["volumeUp", "volumeDown", "getVolume", "getStation", "setStation", "askRemoteToAdd"],
	"luac": "G0x1YVEAAQQEBAgACwAAAEByYWRpby5sdWEAAAAAAAAAAAAAAAICEwAAAAFAAAAHAAAAAcAAAAeAAAAkAAAABwABACRAAAAHQAEAJIAAAAeAAQAkwAAAB8ABACQAAQAHAAIAJEABAAdAAgAkgAEAB4ACAB4AgAALAAAABAwAAAByYWRpb1ZvbHVtZQADAAAAAAAAAAAECAAAAHN0YXRpb24ABBMAAABNdWxiZXJyeSBDb29sIEphenoABAwAAABkZXNjcmlwdGlvbgAECQAAAHZvbHVtZVVwAAQLAAAAdm9sdW1lRG93bgAECgAAAGdldFZvbHVtZQAECwAAAHNldFN0YXRpb24ABAsAAABnZXRTdGF0aW9uAAQPAAAAYXNrUmVtb3RlVG9BZGQABwAAAAAAAAANAAAAGQAAAAAAAAUNAAAABQAAABdAQAAWQACAAYAAAB4AAAEBwAAARQABAIFAAQDBgAEABQEAABUAAQAeAAABHgCAAAcAAAAEDAAAAHJhZGlvVm9sdW1lAAMAAAAAAAAAAAQSAAAAVGhlIHJhZGlvIGlzIG9mZi4ABBcAAABUaGUgcmFkaW8gaXMgdHVuZWQgdG8gAAQIAAAAc3RhdGlvbgAEDgAAACBhbmQgcGxheWluZyAABBYAAABtdXNpYyBhdCBhIHZvbHVtZSBvZiAAAAAAAA0AAAASAAAAEgAAABIAAAATAAAAEwAAABYAAAAWAAAAFgAAABcAAAAXAAAAFwAAABcAAAAZAA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AAAAAAAAAA="
}

LUA Source Code

-- Radio volume is a number that determines how loud the radio is.
-- It cannot be modified by other scripts directly, but only by the 
-- exposed volume functions.
radioVolume = 0;

-- Radio station is the text name of the station the radio is tuned to.
-- It cannot be modified by other scripts directly, but only by the 
-- exposed set & get functions.
station = "Mulberry Cool Jazz";

-- Called by the game when the player looks at the radio to determine
-- radio 
function description()

	-- When you look at the radio, it will display its current
	-- volume level.

	if radioVolume == 0 then
		return "The radio is off.";
	end

	return "The radio is tuned to " .. station .. " and playing " ..
		"music at a volume of " .. radioVolume;
	
end

-- Increases the volume of the radio by 1, up to a max value of 5.
-- Custom LUA function.  By default not exposed, but it has been added
-- to the customPublicFunctions list in the JSON definition.
function volumeUp()
	if radioVolume < 5 then
		radioVolume = radioVolume + 1;
	end
end

-- Decreases the volume of the radio by 1, down to a min value of 0.
-- Custom LUA function.  By default not exposed, but it has been added
-- to the customPublicFunctions list in the JSON definition.
function volumeDown()
	if radioVolume > 0 then
		radioVolume = radioVolume - 1;
	end
end

-- Public function that allows other classes to find out how loud the
-- radio is.
function getVolume()
	return radioVolume;
end

-- Sets the current station.
function setStation(stationToTuneTo)	
	-- In a real script, you might want to perform some validation here 
	-- to ensure the station actually exists, or is a valid callsign, or 
	-- something like that.
	station = stationToTuneTo;
end

-- Gets the current station
function getStation()
	return station;
end

-- Asks the designated remote to add three numbers
-- (This is mostly to demonstrate that you can call back to the
-- original object if you want to, just be careful not to make
-- an infinite loop.)
function askRemoteToAdd(remote, number1, number2, number3)
	return remote.addThreeNumbers(number1, number2, number3);
end

Radio Remote

Entity JSON Code

{
	"type": "ENTITY",
	"name":
	{
		"literalString": "Radio Remote"
	},
	"description":
	{
		"literalString": "A remote control for a radio."		
	},
	"isUseable":
	{
		"literalBoolean": true
	},
	"doUse":
	{
		"programBoolean": true
	},
	"customPublicFunctions": ["addThreeNumbers"],
	"luac": "G0x1YVEAAQQEBAgAEQAAAEByYWRpb1JlbW90ZS5sdWEAAAAAAAAAAAAAAAICDQ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GhpcwAEDAAAAGdldExvY2F0aW9uAAAEBwAAAFBsYXllcgAEDAAAAGdldEluc3RhbmNlAAQGAAAAcmFkaW8ABAcAAABnZXRNYXAABBUAAABnZXROYW1lZEVudGl0eUluUm9vbQAEBgAAAFJhZGlvAAQHAAAAcm9vbUlEAAAAAAAZAAAAXgAAAF4AAABeAAAAXgAAAGAAAABgAAAAYAAAAGMAAABjAAAAYwAAAGMAAABjAAAAYwAAAGcAAABnAAAAZwAAAGcAAABoAAAAaQAAAGkAAABnAAAAaQAAAG4AAABuAAAAbwAAAAAAAAAAAAAAAAAAAHUAAAB3AAAAAAMABAQAAADMQAAAzICAAd4AAAEeAIAAAAAAAAAAAAAEAAAAdgAAAHYAAAB2AAAAdwAAAAMAAAAIAAAAbnVtYmVyMQAAAAAAAwAAAAgAAABudW1iZXIyAAAAAAADAAAACAAAAG51bWJlcjMAAAAAAAMAAAAAAAAADQAAAB4AAAACAAAAMQAAACIAAABFAAAANQAAAFkAAABMAAAAbwAAAFwAAAB3AAAAdQAAAHcAAAAAAAAAAAAAAA=="
}

LUA Source Code

-- Called by the game when the player uses the remote
function doUse()

	-- Get radio if its in the same room as the remote
	radio = getRadio();
	
	-- Was a radio found?
	if radio == nil then
		-- No radio found.
		MainScreen.addGameText("You press the button, but there's no " ..
				"radio here, so nothing happens.");
		return true;
	end

	-- Test volume functions
	MainScreen.addGameText("You fiddle with the volume settings:\n");
	testVolumeUp(radio);
	MainScreen.addGameText("\n");
	
	-- Test tuner functions
	MainScreen.addGameText("You fiddle with the tuner:\n");
	testStation(radio);
	MainScreen.addGameText("\n");
	
	-- Test multiple levels of function calls
	MainScreen.addGameText("You fiddle with the calculator function:\n");
	testCalculator(radio);
	
	return true;
end

-- Helper function to test volume feature
-- Pass in the radio to modify as a parameter
function testVolumeUp(radio)
	-- How loud is the radio?
	MainScreen.addGameText("The radio's volume is currently " ..
			radio.getVolume() .. "\n");
	
	-- Increase radio volume by one.
	MainScreen.addGameText("You press the volume up button on the radio remote.\n");
	radio.volumeUp();

	-- How loud is the radio? (It should be one louder than before,
	-- unless volume hit the maximum amount).
	MainScreen.addGameText("The radio's volume is currently " ..
			radio.getVolume() .. "\n");
	
	return true;
end

-- Helper function to test volume feature
-- Pass in the radio to modify as a parameter
function testStation(radio)
	-- What station is the radio?
	MainScreen.addGameText("The radio's station is currently " ..
			radio.getStation() .. "\n");
	
	-- Increase radio volume by one.
	newStation = "Rock & Roll";
	MainScreen.addGameText("Using the remote, you tune the radio " ..
			"to " .. newStation .. "\n");
	radio.setStation(newStation);

	-- What station is the radio? 
	MainScreen.addGameText("The radio's station is currently " ..
			radio.getStation() .. "\n");
	
	return true;
end

-- This example tests calling multiple levels down, eventually
-- back into this object.  
-- Ie: remote.testCalculator -> 
--		radio.radio.askRemoteToAdd(...) ->
--		 remote.addThreeNumbers(...)
function testCalculator(radio)
	number1 = 5;
	number2 = 10;
	number3 = 15;
	
	MainScreen.addGameText(
		number1 .. " + " ..
		number2 .. " + " ..
		number3 .. " = ");
		
	addAllThree = radio.askRemoteToAdd(this, number1, number2, number3);
	
	MainScreen.addGameText(addAllThree .. "\n");
end

-- Private helper function to get the radio object
function getRadio()
	-- First, get location of this remote
	location = this.getLocation();
	
	if location == nil then
		-- No location, so probably in player inventory.
		-- Use player's location instead.
		location = Player.getInstance().getLocation();
	end
	
	-- Now we know where to look for a radio, so try to find one.
	radio = location.getMap().getNamedEntityInRoom(
		"Radio", 
		location.roomID);
		
	-- Radio will be nil/null at this point if no radios are
	-- at the same location, but will be set to a radio if at
	-- least one radio is available.
	return radio;
end

-- Adds three numbers
-- (This is mostly to demonstrate that you can call back to the
-- original object if you want to, just be careful not to make
-- an infinite loop.)
function addThreeNumbers(number1, number2, number3)
	return number1 + number2 + number3;
end