Any way to know at Which index a table item resides when Item sent as ARG to Function?
Posted: Sun Jun 14, 2026 9:26 pm
This is something that often happens in my codes:
Point in this example is, that I dont want Walls to have X and Y independently, but since they are always coming in certain intervals, I can instead keep drawing them based upon their index number.
As in, I know first wall X will be 100, second one 300, third one 500, therefore I dont need to store those X independently, but as long as I know their index number, I can figure out their X-placing, based upon this simple formula on (StartX + SpaceBetween * Indexnumber).
But problem is in getting that IndexNumber.
As in, I want to simply access this function, by sending it a Wall Item, nothing else. But then I dont have the Index number.
So far I have had to solved this problem by for example adding IndexNumber manually there, as in:
In which case I can do:
But it is not a good solution. I would like to have a cleaner solution.
Therefore, If function is having Wall Item (table) as its ARG, is there any way to know at which IndexNumber this Item is residing in the table that it is residing?
As example:
Code: Select all
Walls= {}
Walls[1] = {hp=100}
Walls[2] = {hp=150}
StartX = 100
SpaceBetween = 200
Y = 300
Function DrawOnTopOfWall(Wall)
GFX.DisplayBrush(StartX + SpaceBetween * IndexNumber, Y)
EndFunction
As in, I know first wall X will be 100, second one 300, third one 500, therefore I dont need to store those X independently, but as long as I know their index number, I can figure out their X-placing, based upon this simple formula on (StartX + SpaceBetween * Indexnumber).
But problem is in getting that IndexNumber.
As in, I want to simply access this function, by sending it a Wall Item, nothing else. But then I dont have the Index number.
So far I have had to solved this problem by for example adding IndexNumber manually there, as in:
Code: Select all
Walls= {}
Walls[1] = {hp=100, Number=1}
Walls[2] = {hp=150, Number=2}
Code: Select all
GFX.DisplayBrush(StartX + SpaceBetween * Wall.Number, Y)Therefore, If function is having Wall Item (table) as its ARG, is there any way to know at which IndexNumber this Item is residing in the table that it is residing?
As example:
Code: Select all
Function DrawOnTopOfWall(Wall)
Number = GetIndexNumberOfItem(Wall)
GFX.DisplayBrush(StartX + SpaceBetween * Number, Y)
EndFunction