Unique tables and possibly a table to store tables.. how to achieve?

  1. What do you want to achieve?
    I wanted to make a script so, as an example, every time a lightbulb is wired to a button, would store the wires, lightbulb and button used in a new ‘Wire Connection’ table, and possibly use a ‘Wire Families’ table to store each ‘Wire Connection’ table for easier access.
    (names for the tables because im bad at explaining and it gets a bit complicated)

  2. What is the issue?
    i have no clue how i would start as for variables you can make a new part and assign a new unique variable to that, but the same thing doesnt apply for tables atleast im pretty sure

edit:
added diagram to explain in more depth

It really depends what you want to do with it. If you wanted to stick to tables you can easily create a new table/dictionary and then append that to an existing table/dictionary.

e.g

new_table = {wire, bulb, button}
existing_table[location] = new_table

However, it sounds like what you are searching for is OOP, where you could create an object which would be more dynamic. Again, it all depends on the functionality you require.

the problem begins when i need a unique new table every time for example someone wires a lightbulb to a button, and then across the map someone else wires a door to a lever, etc etc.

for the second part about ‘OOP’, can you describe what it is? the link you sent just goes from pretty vague to overly complex for me to understand the part i need :grimacing:

i also tried searching if there was an answer on youtube for my query, and apparently metatables also came up, but after watching most of one of the videos it just seems like giving tables events, or am i wrong?

1 Like

See if this helps, not sure if its what you are asking

ConnectionList = {}

function FindConnection(src,dst)
	for n = 1,#ConnectionList do
		local entry = ConnectionList[n]
		if entry.Source == src and entry.Destination == dst then
			return n
		end
		if entry.Source == dst and entry.Destination == src then
			return n
		end
	end
end

function FindConnectionsBySource(src)
	local ret = {}
	for n = 1,#ConnectionList do
		local entry = ConnectionList[n]
		if entry.Source == src then
			table.insert(ret,n)
		end
	end
	return ret
end

function AddConnection(src,dst)
	if not FindConnection(src,dst) then	
		local entry = {
			Source = src,
			Destination = dst,
		}
		table.insert(ConnectionList,entry)
		return entry
	end
end

function RemoveConnection(src,dst)
	local index = FindConnection(src,dst)
	if index then
		table.remove(ConnectionList,index)
	end
end


--Example PSEUDOCODE
AddConnection(Button,Light)
AddConnection(Lever,Door)

if Button:Pressed then
	for _,i in pairs(FindConnectionsBySource(Button)) do
		local entry = ConnectionList[i]
		entry.Destination:Activate()
	end	
end

i dont really get what the script will do due to the acronyms but i have a feeling you misunderstood because there’s only 1 unique table, but honestly i dont blame you because i explained it pretty badly, i’ll edit the post and add a diagram and try see if people get it more but at this point it probably wont get reccommended to many people

So you want something like this?

wiregroups = {}

group = {light, wire, wire, wire, switch}
table.insert(wiregroups, group)

print(wiregroups[1]) --> table {light, wire, wire, wire, switch}
print(wiregroups[1][5]) --> switch
1 Like

yes! do you know how i would do that?

How do you mean, if not with the code I gave as an example? You need to loop over the tables?

for index, wiregroup in pairs(wiregroups) do
    local object = wiregroup[1] --> light
    local control = wiregroup[#wiregroup] --> switch
    local wires = {}
    for i = 2, #wiregroup-1 do
        table.insert(wires, wiregroup[i])
    end
    -- do stuff
end

not necessarily, i just want to find a way so i can make all these seperate tables for the connected parts and possibly organise them into 1 table

also getting off to sleep, sorry

I’m still having a hard time grasping the details of exactly what you’re trying to do here and what is core/fundamental to the design you’re looking for. Describe the system you want from the ground up. The specifics determine whether or not an object oriented approach is most efficient. i.e what are you using it for, how does it work, how are you retrieving the data etc.

Object oriented programming is a paradigm based on the creation of methods and classes. So for example you could have classes named Lever and Door which inherit from Mechanism. You could then define a method in Mechanism which connects the two via a ‘wire’. Alternatively, you could make a WireFamily class which constructs WireGroups. However, this may not be the most efficient route if really all you want is a table of tables since you can just do that with table.insert, or WireFamily[location] - as has already been suggested.

I’ll try explaining it here hopefully as detailed as I can.

  1. Player1 currently is standing next to a generator and a lightbulb. WireFamilies is currently empty, as this is in a fresh new server.

  2. Player1 uses a wirepart and wires said generator and lightbulb together. WireFamilies, to account for this, now has a new table inside. This table is named TableA, and currently is holding the generator, wirepart and lightbulb.

  3. Nearby, Player2 has a door and a lever.

  4. Player2 now wires the door and lever together, but uses 3 wireparts in the process. WireFamilies now is given another table, named TableB, as there are no connections between both players builds. TableB currently contains 3 wireparts, a door and a lever.

  5. Now, whenever a wire is made, if it is attached to a component of TableA or TableB, it will be added to the table instead of making a new one.

sorry for the delayed response as i said i had to sleep for the day lol

also,

this is my problem though, how do i insert a table into another? let alone make a new unique one everytime someone meets the conditions to?

How are you retrieving the data?

I still think OOP is probably the best way of achieving what you actually want, but tables is probably simpler to understand.

You’ve already been given a decent solution for this. Realistically, anything beyond constructing the table depends on how you’re storing/retrieving data. e.g

function createTab(num_wireparts, start, finish)
	local tab = {start}
	
	for i=1,num_wireparts,1 do
		table.insert(tab,"wire")
	end
	
	table.insert(tab,finish)
	
	return tab
end

print(createTab(3,"door","lever"))

-- prints {"door","wire","wire","wire","lever"}

You could then do something like:

local wireFamily = {}

table.insert(wireFamily, createTab(3,"door","lever"))
-- or
wireFamily[name_of_location] = createTab(3,"door","lever")

However, as I said, I’m certain that when you actually try to use this system for anything useful you’ll realise objects are actually what you’re looking for.

sorry, i knew i was missing something when i wrote the detailed explanation, im using ChildAdded but specifically to detect when wireparts are made but also InputPlugs and OutputPlugs.

This actually seems like a viable method for signals, but it isnt exactly what i really wanted for generators and a power system, (sorry D:) is there a way i could do something similar that held all the connected inputs, outputs and wires? im basically trying to make an electrical system similar to rust (i think) where you can connect 2 generators together and so the power limit for everything connected will be 1 generator times 2

What’s this? If you wanted to get a specific table of wireparts how would you do it.

Explain. From the information you’ve provided it seems like that method does give an input, an output and the wires connecting them. I somehow feel like I know less about what you’re describing that when I started.

Do you want something like this?

-- for some class mechanism
function Mechanism.new (self, name_, type__)
  local object = {}
  
  object.name = _name
  object.type_ = type__
  object.connections = {}

  setmetatable(object, self)
  return object
end

function Mechanism:connection (output_)
  table.insert(self.connections, output_)
end

A basic implementation of a possible OOP approach. In this example there are no ‘wires’ just the connections they represent.

They are how I would detect a button from a motor etc. I’m finding the wireparts by using a script in serverscriptstorage which detects when a new part is made, and then if its a wirepart.

exactly, but there are multiple inputs/outputs, my example accidentally left that part out

just my incredible explaining skills, sorry

admittedly i need the wireparts so that when another wirepart is made i can check if it is attached to a known circuit. and also dont really understand what that does

sorry for the late reply i thought you ghosted me after the last reply

can i contact you somewhere else like discord? no offence but im getting a bit annoyed having to wait a day between replies

aaaaaaaaand i’ve officially been ghosted for what is in my opinion, no reason. nice.

@PerilousPanther can i ask you one more question? do you think i can repost this seeming as i still have no answers?