This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
lifewcody's profile picture

Disappearing variable in tale

Started by lifewcody, 12 April 2017 - 07:15 AM
lifewcody #1
Posted 12 April 2017 - 09:15 AM
For reference, all code is available here: https://github.com/I...s/IPv2/tree/dev

This problem has been happening a little bit in other modules, but in modules/modem.lua it's happening.

(For this example I'm using test/modemTest.lua)

1) run core_router/core_router.lua (to set some env variables)
2) run disk/test/modemTest.lua

modem.lua:79: attempt to index? (a nill value)

In the function openModems(), line 79 is:
print(modems.top.WiFi)

Now you're probably thinking the variable isn't set, but it is (eventually). I'll walk through the logic of it.

test/modemTest.lua ONLY calls modem.openModems()

The first line of modem.openModems() is line 79, so technically is isn't set. Remove this line and you get something (ish) like this


Cool! we have our SSID, channel, and password. Re-run the modemTest again, note how (SAved cache to disk) is printed.



Now we have a new channel and password? Why?
In the function openWiFi(side) it has this code:

if modems[side]["WiFi"] == nil then
		local WiFiInfo = { -- Create our Wifi table
			[ "channel" ] = math.random(150, 175), -- Open a random channel on 150 to 175
			[ "SSID" ] = getSSID(side), -- Generated the SSID. If it's on the top and CC ID is 1 then SSID is CC1TO
			[ "password" ] = generatePassword(6) -- Generates a 6 digit password
		}
		modems[side]["WiFi"] = WiFiInfo
	end

It should check to see if the WiFi is there or not, and if it's not THEN create a new SSID, channel, pass. Well, remember that line we took out earlier, add it back.


It shows *table: 212aa5c5) and it shows the SSID, channel, and pass in that table. So I KNOW that it's being loaded in the modems.load() function. Now I am going to add the following:
[font=Consolas,]print(modems.top.WiFi[/font][font=Consolas,])[/font]
to the first line of openWiFi(side). It is 'nil' when we run it (it's been in the screenshots fyi, the 'nil').

I have no clue why it is being passed to other functions, but not that one. I have the cache being loaded first thing in load() and it's there. It's in openModems(), but once openModems() calls openWiFi(), ONLY that dissapears (in the table, RX and TX is there, just not WiFi) and I have no clue why.

Thanks for the help.
Bomb Bloke #2
Posted 13 April 2017 - 01:57 AM
Consider what getActiveSides() does, and where in your code you're calling it.
lifewcody #3
Posted 18 April 2017 - 05:26 AM
Consider what getActiveSides() does, and where in your code you're calling it.
Ahhhhh

Thank you so much. I've been pondering on why it wasn't working for the longest time. I just needed fresh eyes on it.

Thank you :)/>))))