You are close to what you want but here is the problem. I think what is happening is that you are trying to do "too much" inside one loop. The "uplayer" and "downlayer" don't exist in your table before you call them.
I split your code into two different loops, creating the table first so all the layers exist in it before comparing the selected layer to the table fields and it works.
However there is the problem that the variables aren't reassigned on the click after a different layer is selected (I plugged this code into your original select layer script for testing). You get the results of the last click... you have to click twice on the same layer to get the layer variables to update. I haven't played with it enough to see what is causing this. I think that is just the issue. The layer switching ocurrs AFTER the variables are called. They are not reset until you click again so you are always off by which ever the last layer was selected.
What you can do is make sure the layer variables (uplayer, downlayer, layer) are reassigned AFTER you switch layers.
Here is a test I did just to see that it works. I moved the layertable variable and declared it outside the loop so it can be read by the second loop. I changed the "layerid" variable to "layercount" so it made more sense to me. It isn't the "ID" it is actually the layer count... this is a nitpick

:
In the first part I create the layertable. Note I use local outside the loop and just the variable inside the loop to assign the values. Just the table with layer "objects" indexed by the ID number is created by the first loop.
Code: Select all
local layer = moho.layer
local layertable = {}
local layercount = layer:Parent():CountLayers()-1
for i = 0, layercount do
local layerobj = layer:Parent:Layer(i)
layertable[i] = layerobj
end
In the next part I loop through the table itself using the same layercount variable. That variable will be the same as the number of items in the table. I compare the layers in the table to "layer" (moho.layer). Bingo! Success.
Code: Select all
for i = 0, layercount do
if (tostring(layertable[i]) == tostring(layer)) then
print(tostring(layertable[i]) .. tostring(layer))
uplayer = layertable[i+1]
print(tostring(uplayer))
downlayer = layertable[i-1]
print(tostring(downlayer))
end
end
I didn't spend a ton of time on this short of just getting it to work without errors.
I was a bit annoyed at one part... using "tostring()" to compare those dang layers. It SHOULD work by just comparing layers for goodness sakes. Those layers should be able to be compared. It works with bones dangit.
Oh well, converting the userdata to strings works too. Just keep in mind those strings from the layer objects will change every time AS is opened or each time the same project file is opened. Never expect them to stay the same. It doesn't really matter since it works fine in a "session". I do this myself sometimes. In fact I think I used that technique for the same reason.
EDIT: You still get "nil" at the "top" and "bottom" of the loop since there are no more layers for "uplayer' and "downlayer". layertable[i+1] is nil if there is no layer above the last layer. Same with the first layer.
-vern