Creating a table of Objects

Started by Beaver, Nov 04, 2020, 11:49 am

Previous topic - Next topic


Hi guys, I want to know how do I create a class and assign the object created to a table using the name of the object as the index. I'm trying to make a button API, I know that there are some good ones on pastebin but I'm just practicing. I am also using cc version 1.75 on Minecraft 1.7.10. Here is what I am thinking

Code Select
buttonTable {}

class buttonClass {
  name = "defaultName"

function newButton(instanceName, stuff....) { 
  new buttonInstance = InstanceName
  setmetatable (InstanceName, {__index=buttonClass}) 
  buttonClass.setName (instanceName) 
  buttonTable[instanceName] = buttonInstance  //is this correct? 

Is this correct?


You are probably better off using someone else's button API if your actual end goal is to use the API to make buttons for a program that you are wanting to write. Touchpoint should work on CC 1.75

Also, Lua doesn't have classes or objects for that matter, you can fake objects with tables but they really are not objects. You will likely have to use a prototyping method of creating those objects, making Lua support a class like syntax usually has a lot of hacks.

As for setting the key of a table to a property of another table which table becomes the value of that key:
Code Select
local button = {name = "myButton"} -- exerciseForUser: implement button
-- if you don't know the local keyword then you might want to look up scope

local buttonTable = {} -- a better name might be buttons, putting the type in the name of a variable is seen as bad practice

-- add button to buttonTable
buttonTable[] = button