Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
meigrafd

Problem: Table/Array doesnt get increased

Question

I have a strange behavior with an Table ...

 

I have a Script which listens for 'modem_message' events and to get track about 'Clients' i have a same named Table where i want to add each client-robot to it...

 

The Code looks reduced like:

 

local component = require("component")
local event = require("event")
local wlan = component.modem

local wlanPort = 7331
wlan.open(wlanPort)
local Clients = {}

function tableLength(T)
    local count = 0
    for k,v in pairs(T) do count = count + 1 end
    return count
end

function addClient(remoteAddress)
    local rcount = (tableLength(Clients) + 1)
    Clients[rcount] = remoteAddress
    print("Added new Client #"..rcount)
end

function messageReceived(event, localAddress, remoteAddress, port, senderDistance, message)
    addClient(remoteAddress)
end

event.listen("modem_message", messageReceived)

while true do
    os.sleep(1)
end

Real code: http://pastebin.com/hfjT7e8s

 

The Problem is that 'rcount' doesnt get expanded. Its always same

 

 

It receives a modem_message, so the event works - thats tested and is not the Problem. Also i know that #Clients can return the table length too but i have a good reason why i did it not that way (#tbl only works for numerical keys)

What am i doing wrong?  :unsure:

//EDIT: the reduced code works... mmh :( why not the real?

Link to post
Share on other sites

4 answers to this question

Recommended Posts

  • 0
  • Solution

Solved: I must craft each Wireless card myself, else they dont get an unique address.

The addresses should also be unique if you pull them from the creative menu or NEI - by default components have no address, and they are assinged an address the first time they are inserted into a computer. Of course if you use middle-click in creative mode to clone an "initialized" component, the clone will have the same address.

Link to post
Share on other sites
  • 0

From what I can tell your Clients table is a sequence, i. e. it has numerical keys starting with 1 and no nils in between. By the way, Lua has some great built-in sequence manipulation functions in the table library. Most notably table.insert to insert an object somewhere in the sequence (defaults to at the end, i. e. append) and table.remove to remove an item at a given index from the sequence (defaults to last item, i. e. pop). You can be sure that those functions will always work correctly - they are part of Lua's standard library and are well tested and used.

Link to post
Share on other sites
  • 0

I've also tested it with table.insert() etc but that did nothing change...

But i now found out that all my Robots seems to have the same Address - how can that be?

Does all wireless-cards have the same Address? Thought thats like a NIC mac and is always unique ?

How can i change that?

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.