Access elements from a list

Monkey Forums/Monkey Programming/Access elements from a list

Raul(Posted 2013) [#1]
1st time when I try to use lists.

I need to access some specific elements from a List.

I have this list:
bouncedServerList


Where I store some ids when the user is selecting an object from my game. Now I want to draw a line between these objects. A 'bounced link'

How do I select a specific value from the list?


Gerry Quinn(Posted 2013) [#2]
A list is a chain of items in which each one points to the next one and the previous one. So you have to 'walk along' the list if you want to find anything specific.

So...which item do you want? If you want to draw a line between each pair in order, for example, you could write:

Local bCurrent:Bounce
Local bPrev:Bounce
For local bounce:Bounce = Eachin bounceServerList
    bPrev = bCurr
    bCurr = bounce
    If bPrev <> Null
        DrawLine( bPrev.pos, bCurr.pos )
    End
Next


That's probably nothing like what you want to do, so give us a clue ;-)


Raul(Posted 2013) [#3]
Hmm, what you wrote there is quite ok but not what I am looking for. Maybe I do not understand how is working exactly so please forgive if my question is stupid:

So, I have this list: bounceServerList
Every time I tap on a 'server' in my game, I add the server 'ID' in that list:
bouncedServerList.AddLast(s)

of course if the server is already in the list I remove it:
bouncedServerList.Remove(s)


Now, I want to draw some yellow lines between these servers from the bounceServerList which is a list of integers.
For example:
1st element in bounceServerList = 4 'server id = 4
2nd element in bounceServerList = 2 'server id = 2
3rd element in bounceServerList = 0 'server id = 0

I want to make something like
DrawLine ( myServer[bounceServerList[value 1st element]].PosX, myServer[bounceServerList[value 1st element]].PosY, myServer[bounceServerList[value 1st element] + 1 ].PosX, myServer[bounceServerList[value 1st element] + 1 ].PosY )


I am trying to port my c# code from a previous XNA game. In that I used this code:
for (int x = 0; x < bouncedServers.Count - 1; x++)
    {
drawLine(2, Color.Green, new Vector2(servers[bouncedServers[x]].pos.X + 32, servers[bouncedServers[x]].pos.Y + 32), new Vector2(servers[bouncedServers[x + 1]].pos.X + 32, servers[bouncedServers[x + 1]].pos.Y + 32));
}


but in C# I was able to access the value of any element from my list like that:
bouncedServers[id here]

Hope I was able to describe my issue.


Raul(Posted 2013) [#4]
ok, I played a little with your code:


Local currentBounce:Int = -1
Local previewBounce:Int = -1

For Local b = EachIn bouncedServerList
    previewBounce = currentBounce
    currentBounce = b

    If previewBounce <> - 1
        DrawLine(myServer[previewBounce].posX, myServer[previewBounce].posY, myServer[currentBounce].posX, myServer[currentBounce].posY)
    EndIf		
Next


and basically I obtained what I wanted..

My initial question is still up:
Is there a way to obtain the value of a certain element from the list?


Gerry Quinn(Posted 2013) [#5]
Not directly. Somebody asked the same question a few days ago, also I think looking for an equivalent to the C# function. Here is some code that will find a particular element:

List< MyObject > myList = new List< MyObject > 
' Populate list
Local ob:MyObject
Local count:Int
For Local getOb:MyObject = Eachin myList
	If count = 5
		ob = getOb
		Exit
	End
	count += 1		
Next
' ob is the sixth object in the list, or Null if the list is shorter than six objects


You could extend Monkey.List to add a function for doing this, but there's nothing there by default.

It looks inefficient, but C# presumably does something similar. That said, it could be tidied up more if nodes were not hidden to derived classes by a Private access qualifier...


Raul(Posted 2013) [#6]
I see. thanks for the answers. I just wanted to verify. Your initial code fits me well for now.

Soon a new thread will appear to Monkey Projects sub-forum :P

R.


Tibit(Posted 2013) [#7]
In a lot of cases I use Stack<MyObject> instead of list because of garbage concerns. With a Stack you can get an index. Instead of addLast you have push(). Stack is much closer to C#'s list, List in Monkey is close to C#'s LinkedList which people do not use that often, especially not in realtime games.

Or you can just use a normal array and then you can go myArray[i]


Raul(Posted 2013) [#8]
@Tibit: arrays were a good choice but I wanted to remove any element from that list easy.

I didn't know about Stack.. Maybe next time!


Gerry Quinn(Posted 2013) [#9]
Basically you have a choice. If you want to access any element quickly, a stack or array is best. If you want to insert of remove elements quickly, a list is best. If you want to do both... some inefficiency is inevitable.