Main Menu

Creating a table of Objects

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

Previous topic - Next topic

Beaver

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

(pseudocode) 
buttonTable {}

class buttonClass {
  name = "defaultName"
  stuff....
}

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

Is this correct?

Lupus590

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:
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.name] = button

Ripleigh

so... pardon the noob approach to this, but i like to figure out the basis behind how it works, and this bit:

Quote from: Lupus590 on Nov 04, 2020, 02:09 PM-- add button to buttonTable
buttonTable[button.name] = button

makes me question something.


little background since this thread is over 120 days old...

seeing as it has come to my attention while I was digging around for help with tables and such, as i am  looking to code a fair bit of this myself(this being both in game lua and MC mods, while i have a decent  grasp on
C#/C++... as you pointed out:  Lua is an odd one

learning through my own means because schooling is rediculous means i get to "enjoy" the bugs of learning for myself lol


anyway, the question.. well two actually:

how would one iterate through reading back these values as they seem to be assigned according to "button.name"'s value?

would a simple for loop be capable of going through this list, or would you also need a look-up table of names to forward to the table?


again.

i have a fair grasp of programming, so launch into this at full force ^.^

i'm just taking the noob approach because it helps me understand deeper, and it MIGHT be useful to someone else who finds this thread.

thanks in advance <3
~RipleighRose

Lupus590

#3
So lua has two ways of iterating through tables, pairs(table) and ipairs(table). They behave slightly differently, pairs will go though every entry in the table but doesn't guarantee that the order will be the same between two calls (although in my experience it often is the same order), were as ipairs does guarantee that the keys will be in order but only goes through the numeric keys.

local myTable = {"one", "two", "three", ["a"] = "alpha", ["b"] = "bravo", ["c"] = "charlie"}

for key, value in ipairs(myTable)
  print(key .. ":" .. value) -- 1:one 2:two 3:three
end

for key, value in pairs(myTable)
  print(key .. ":" .. value) -- a:alpha 2:two  1:one c:charlie 3:three b:bravo
end


Ripleigh

oh.

that just clarified SO MUCH in lua as to what "pairs and ipairs" are.... wow.


thank you so much!!


also: slight typo in the second section for using pairs as you typed Ipairs twice.

I got it though. I'm really bad for typos on my phone haha

thank you SO much for that bit of info ^.^