2

I have a lot of variables that seem to lend themselves to be referenced a lot easier, but I'm not sure how to do it; the variable names are related to entries on a coordinate system, and where they are written to is also coordinate based. I have tried searching google, I've gone through the Lua documentation, but as I'm not sure what I'm looking for I think that is hindering my search. Here is how the variables look in an excerpt of my code:

-- Bank 1

local Object111 = sItem.getTargetDetails("-4,3,-4")
local Object112 = sItem.getTargetDetails("-5,3,-4")
local Object113 = sItem.getTargetDetails("-6,3,-4")
local Object114 = sItem.getTargetDetails("-7,3,-4")
local Object115 = sItem.getTargetDetails("-8,3,-4")
local Object121 = sItem.getTargetDetails("-4,4,-4")
local Object122 = sItem.getTargetDetails("-5,4,-4")
local Object123 = sItem.getTargetDetails("-6,4,-4")
local Object124 = sItem.getTargetDetails("-7,4,-4")
local Object125 = sItem.getTargetDetails("-8,4,-4")
local Object131 = sItem.getTargetDetails("-4,5,-4")
local Object132 = sItem.getTargetDetails("-5,5,-4")
local Object133 = sItem.getTargetDetails("-6,5,-4")
local Object134 = sItem.getTargetDetails("-7,5,-4")
local Object135 = sItem.getTargetDetails("-8,5,-4")

-- Bank 2

local Object211 = sItem.getTargetDetails("-4,3,1")
local Object212 = sItem.getTargetDetails("-5,3,1")
local Object213 = sItem.getTargetDetails("-6,3,1")
local Object214 = sItem.getTargetDetails("-7,3,1")
local Object215 = sItem.getTargetDetails("-8,3,1")
local Object221 = sItem.getTargetDetails("-4,4,1")
local Object222 = sItem.getTargetDetails("-5,4,1")
local Object223 = sItem.getTargetDetails("-6,4,1")
local Object224 = sItem.getTargetDetails("-7,4,1")
local Object225 = sItem.getTargetDetails("-8,4,1")
local Object231 = sItem.getTargetDetails("-4,5,1")
local Object232 = sItem.getTargetDetails("-5,5,1")
local Object233 = sItem.getTargetDetails("-6,5,1")
local Object234 = sItem.getTargetDetails("-7,5,1")
local Object235 = sItem.getTargetDetails("-8,5,1")

I would then proceed to call each of these variables in a function, where the position on screen is a function of the numbers in the name, the first two numbers relating to x coords and the last number to the y coords:

mon.setCursorPos(4,4)
mon.write(Object111.StoredPercentage)
mon.setCursorPos(10,4)
mon.write(Object112.StoredPercentage)
mon.setCursorPos(16,4)
mon.write(Object113.StoredPercentage)
...
mon.setCursorPos(8,4)
mon.write(Object121.StoredPercentage)
mon.setCursorPos(8,4)
mon.write(Object121.StoredPercentage)
...
mon.setCursorPos(36,4)
mon.write(Object211.StoredPercentage)
mon.setCursorPos(42,4)
mon.write(Object212.StoredPercentage)
mon.setCursorPos(48,4)
mon.write(Object213.StoredPercentage)
etc....

I can see that this should be able to be generated on the fly, but I don't know where to start without calling it out manually; I would prefer it if my code was cleaner. At this stage I really just need to be taught how to fish; if anyone can point me to documents that explain how to do what I'm trying to I would be most grateful.

hjpotter92
81.1k36 gold badges148 silver badges188 bronze badges
asked Sep 1, 2014 at 8:57
3
  • 3
    There is a way of accessing local variables in Lua. Check the PiL: debug.getlocal(). Another similar already answered question is here. If I were You, I'd try to avoid this kind of clotting and put those objects into local scoped table. Commented Sep 1, 2014 at 9:11
  • 1
    Yes! I really am showing my greeness... Tables are the first step to solving my problem; I have some reading to do. Can you give me some pointers on how to break down a associative array variable name so I can take the last number from Object111 to calculate the x coord, and the first two to calculate the y coord? Thanks! Commented Sep 1, 2014 at 9:41
  • Don't worry... I got it! I'll post my results once I'm finished. Thanks again... you gave me all the little push I needed. Commented Sep 1, 2014 at 10:27

1 Answer 1

3

Here's my final solution for anyone trying to achieve same:

local Banks = 2
local Rows = 3
local Columns = 5
function Objectstatus(bank,row,column)
 Id = bank .. row .. column
 worldx = "-" .. column+3
 worldy = row+2
 if bank == 1 then worldz = -4; end
 if bank == 2 then worldz = 1; end
 Object = {}
 Object[Id] = s.getTargetDetails(worldx..","..worldy..","..worldz)
 xcursor = (column*7)+3
 if bank == 1 then 
 ycursor = (row*4)+8
 end
 if bank == 2 then 
 ycursor = (row*4)+24
 end
 mon.setCursorPos(xcursor,ycursor)
 powertext(Object[Id].StoredPercentage) -- A function that sets Texts settings based on result
 mon.write(Object[Id].StoredPercentage) -- Actually writes the result
end
 for BankTemp = 1, Banks do
 for ColumnTemp = 1, Columns do
 for RowTemp = 1, Rows do
 Objectstatus(BankTemp,RowTemp,ColumnTemp)
 end
 end
 end
answered Sep 1, 2014 at 11:41
Sign up to request clarification or add additional context in comments.

3 Comments

one tiny suggestion - I'd make Id, worldx, worldy, xcursor, ycursor local ones too. This way it won't clog global space and perform a little bit faster.
The way I have written my script means these variables are called outside the function, which I believe means they have to be global unless I am mistaken. But thanks for the continuing advice as it is warmly received.
uffff... it's not the best practice. You know, in Lua functions can return multiple values? Ref.: PiL5.1 - multiple results

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.