Any way to know at Which index a table item resides when Item sent as ARG to Function?

Discuss any general programming issues here
Post Reply
Bugala
Posts: 1422
Joined: Sun Feb 14, 2010 7:11 pm

Any way to know at Which index a table item resides when Item sent as ARG to Function?

Post by Bugala »

This is something that often happens in my codes:

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
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:

Code: Select all

Walls= {}
Walls[1] = {hp=100, Number=1}
Walls[2] = {hp=150, Number=2}
In which case I can do:

Code: Select all

GFX.DisplayBrush(StartX + SpaceBetween * Wall.Number, Y)
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

Function DrawOnTopOfWall(Wall)
	Number = GetIndexNumberOfItem(Wall)
	GFX.DisplayBrush(StartX + SpaceBetween * Number, Y)
EndFunction
plouf
Posts: 732
Joined: Sun Feb 04, 2018 11:51 pm
Location: Athens,Greece

Re: Any way to know at Which index a table item resides when Item sent as ARG to Function?

Post by plouf »

i guess currently the only way is to iterate all elements with ForEach, until you find it
Christos
Bugala
Posts: 1422
Joined: Sun Feb 14, 2010 7:11 pm

Re: Any way to know at Which index a table item resides when Item sent as ARG to Function?

Post by Bugala »

But even with ForEach, you still would need to know the main tables name where the Wall item is from.

I mean, in this examples case all the walls probably reside in some single table. But this was just to illustrate the problem I have run into several times in my codes.

As in, to make it more complicated, situation could be that there are three different tables containing different wall items, and they all would use this same function.

I could be doing:

Code: Select all

DrawOnTopOfWall(PermanentWalls[1])
DrawOnTopOfWall(TemporaryWalls[2])
DrawOnTopOfWall(EnemyWalls[3])
In this case even ForEach loop requires to know which table the wall item was contained in.

I wonder is it even possible (For Andreas to do) to have such a command where you could get the Index ID in Hollywood?

Point being that as long as table is ordered table, as in, contains IDs 1, 2, 3... then they by default all take same amount of memory, in which case by knowing at which memory location current item is found, and if it also knows where in memory the actual table resides, then it should be able to calculate the index number, if I have understood that logic right.

However, if table contains indexes like "stringindex" then I guess it is undoable.
Post Reply