Sample: OOP Test

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 entity demonstrates some object oriented programming techniques, allowing objects to be created, copied, and modified. Unfortunately, it does not demonstrated inheritance due to the LUA Limitations.

Author: SkyCorp

Entity JSON Code

{
	"type": "ENTITY",
	"name":
	{
		"literalString" : "OOP Test"
	},
	"description":
	{
		"programString": true
	},
	"luac": "G0x1YVEAAQQEBAgAEgAAAEBvb3BOb0luaGVyaXQubHVhAAAAAAAAAAAAAAACAhEAAAAKQAAACYDAgAcAAAAFAAAAZAAAAAlAgIEFAAAAZEAAAAlAAIIFAAAAZIAAAAlAgIIkwAAAB4ABACQAAQAHwAEAHgCAAAgAAAAECAAAAEFjY291bnQABAgAAABiYWxhbmNlAAMAAAAAAAAAAAQJAAAAd2l0aGRyYXcABAQAAABuZXcABAgAAABkZXBvc2l0AAQGAAAAY2xvbmUABAwAAABkZXNjcmlwdGlvbgAFAAAAAAAAAAcAAAAJAAAAAAEAAwYAAABFAAAAhQAAAIZAQAGNAAABSYCAgB4AgAACAAAABAUAAABzZWxmAAQIAAAAYmFsYW5jZQAAAAAABgAAAAgAAAAIAAAACAAAAAgAAAAIAAAACQAAAAEAAAACAAAAdgAAAAAABQAAAAAAAAAAAAAACwAAABAAAAAAAgAFCwAAAFpAAAAWQACAigAAAEAAAAGFAAAAwACAAAABAACcQIABCQCAgF4AAAEeAIAAAgAAAAQNAAAAc2V0bWV0YXRhYmxlAAQIAAAAX19pbmRleAAAAAAACwAAAAwAAAAMAAAADAAAAAwAAAANAAAADQAAAA0AAAANAAAADgAAAA8AAAAQAAAAAgAAAAUAAABzZWxmAAAAAAAKAAAAAgAAAG8AAAAAAAoAAAAAAAAAAAAAABIAAAAUAAAAAAIAAwQAAACGAEAAjEAAAQmAAIAeAIAAAQAAAAQIAAAAYmFsYW5jZQAA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AATAAAAAIAAAAJAAAAYWNjb3VudDEAAwAAADEAAAAJAAAAYWNjb3VudDIAEgAAADEAAAAAAAAAEQAAAAUAAAAFAAAABQAAAAcAAAAJAAAABwAAAAsAAAAQAAAACwAAABIAAAAUAAAAEgAAADMAAAAZAAAATAAAADYAAABMAAAAAAAAAAAAAAA="	
}

LUA Source Code

-- Account Class

Account = {balance = 0}

function Account.withdraw (v)
   self.balance = self.balance - v
end

function Account:new (o)
   o = o or {}   -- create object if user does not provide one
   setmetatable(o, self)
   self.__index = self
   return o
end

function Account:deposit (v)
  self.balance = self.balance + v
end

-- End Account Class

-- UTIL --
function clone (t) -- deep-copy a table
   if type(t) ~= "table" then return t end
   local meta = getmetatable(t)
   local target = {}

   local keys = table.keys(t);

   io.write("Keys array is: " .. type(keys) .. "\n");
   io.write("Keys length is: " .. #keys .. "\n");

   for i=1, #keys do
      local key = keys[i];
      local value = t[key];
      io.write("Key: " .. key .. "\n");

      if key == "__metatable" then
         -- do nothing, metatable is handled separately
      elseif type(value) == "table" then
         target[key] = clone(value)
      else
         target[key] = value
      end
   end
   setmetatable(target, meta)

   return target
end

-- END UTIL

function description()

   local account1 = Account:new();
   account1:deposit(100.00);
   outText = "This account has: " .. account1.balance .. "\n";

   io.write("Copy Start\n");

   local account2 = clone(account1);

   io.write("Copy Done\n");

   outText = outText .. "Account 2 has: " .. account2.balance .. "\n";

   outText = outText .. "<Increasing Account 2>\n";

   account2:deposit(50);

   outText = outText .. "Account 1 has: " .. account1.balance .. "\n";
   outText = outText .. "Account 2 has: " .. account2.balance .. "\n";

   return outText
end