Lists Inside Arrays?

nobiagames

New member
Joined
Sep 6, 2017
Messages
9
Hey,
i made a simple particle/2D effect system for my game, but i got a little stuck and can't find any useful information about this.

So, i have a list in which i add the "particles", but i want to draw them at different times, to make them appear on different layers. i could add a layer field to each particle and when updating/drawing them i skip the ones that don't match the specified layer, but that doesn't feel right. so i thought it would probably be best to have a list for each layer and to keep it adjustable, it would be best to have each layer-list in an array. but is that even possible in cx?

i mean something like this: Global particle:List[]<part> = New List[]<part>
(this doesn't work of course)

so, i want a specified number of layers and the ability to add layers and to draw only a single specified layer at a time. and in each layer should be a list.

i also tried list in list, but i don't know how to adress a specific list then, so i guess i would prefer an array for the layers.

i hope this is possible and you guys can help me out here. thanks :)
 

MikeHart

Administrator
Joined
Jun 19, 2017
Messages
3,167
Absolutely possible. If noone does it before me, I can show you an example tommorow. Need some sleep now.
 

dawlane

Well-known member
CX Code Contributor
Joined
Jun 21, 2017
Messages
856
I would suggest the use of Stacks. They have the advantage of being like an array and a List.
An example.
Code:
Strict

Class CPartical
    Field p:Int
    Field s:Int
    Method New(p:Int, s:Int)
        Self.p = p
        Self.s = s
    End Method
 
    Method Partical:Void()
        Print "partical " + p + " in index " + s
    End
End

Function Main:Int()
    Local stack:Stack<List<CPartical>> = New Stack<List<CPartical>>
    For Local s:=0 To 9
        Local list:List<CPartical> = New List<CPartical>
        For Local l:=0 To 19
            list.AddLast(New CPartical(l,s))
        Next
        stack.Push(list)
    Next
 
    Local s:= stack.Get(4)
    For Local l:= Eachin s
        l.Partical
    Next
    Return 0
End Function

And here is the same this, but using an array of lists.
Code:
Strict

Class CPartical
    Field p:Int
    Field s:Int
    Method New(p:Int, s:Int)
        Self.p = p
        Self.s = s
    End Method
 
    Method Partical:Void()
        Print "partical " + p + " in index " + s
    End
End

Function Main:Int()
    Local lists:List<CPartical>[]
 
    For Local a:=0 To 9
        Local plist:List<CPartical> = New List<CPartical>
        For Local p:=0 To 19
            plist.AddLast(New CPartical(p,a))
        Next
        lists = lists.Resize(lists.Length()+1)
        lists[a] = plist
    Next
 
    Local ls:= lists[2]
    For Local l:= Eachin ls
        l.Partical
    Next
 
    Return 0
End Function
 
Last edited:

Gerry Quinn

Active member
Tutorial Author
Joined
Jun 24, 2017
Messages
153
Basically, any container can contain any other, to any depth.

Not tested, but the code will be something like this according to your original plan. Array is fine if you know from the start how many layers there will be, but another structure such as a stack is better if you don't. A list of lists is possible, but an array or stack is more natural and efficient for layers as you will probably not be inserting or removing them.


Code:
' The array of lists
Field particles:List< Particle >[]

' Initialing three layers
particles = New List< Particle >[ 3 ]
For Local i:Int = 0 Until 3
    particles[ i ] = New List< Particle >()
Next

' Adding a particle to a particular list
particles[ 1 ].AddLast( New Particle() )

' Drawing everything, with layer 0 on the bottom, layer 2 on top
For Local layer:Int = 0 Until 3
    For Local particle:Particle = Eachin particles[ layer ]
        particle.Draw()
    Next
Next
 
Last edited:

Gerry Quinn

Active member
Tutorial Author
Joined
Jun 24, 2017
Messages
153
Edit: this is by the way, and it's something that will never be changed (it's like this in most languages), but I think some of the problems people have stem from the fact that arrays still use the classic C syntax and when you mix them with standard container syntax, the arrays are 'back to front'.

Consider a list of stacks of sets of ints: it looks like xxx:List< Stack< Set< Int > > > Logical enough...

...but a list of arrays of sets of ints looks like xxx:List< Set< Int >[] > It seems out of order!

One could easily make a language using stack syntax for arrays. But you'd have to start from scratch with a new language or it would not be worth the confusion. You'd still cause lots of confusion, even if it brought benefits in the end. (Also, multidimensional arrays would look ugly to most people using stack syntax, so there would be a demand for some special syntax, making the conversion non-trivial.)
 

nobiagames

New member
Joined
Sep 6, 2017
Messages
9
hey guys, thanks a lot for the replies.
unfortunately i still can't seem to get my code to work. i think i have a problem understanding the basic structures of classes, methods, functions and such. i will post my code and if someone could look over it, i would be really glad. otherwise i will probably put more time in it tomorrow and maybe i'll figure it out on my own.
(my goal is to make a simple particle system in a module, that i can easily add to any application. i want to be able to just make it work with maybe an initialize function/method, an addparticle function and a draw function. my first try was with classes only, but i had problems giving the image over to the particlesystem. then i changed it around and used more functions and in that process i think i also went to load the images globally. so maybe the classes weren't even the problem.)
anyway... in GerryQuinns example the list is a field, while in my code i create the list as a global. it feels like that is the problem for me, although i do not really get why this makes a difference.
so, here is my code (currently set to one layer for testing purposes), without the array "layers" everything works fine:

Code:
Global particle:List<part>[] = New List<part>[1]


Class part
Field img:Image
Field x:Float,y:Float
Field duration:Int,timestamp:Int
Field maxalpha:Float
Field size:Float,sizebloat:Float
Field movdir:Float,movspd:float
Method New(image:Image,x:Float,y:Float,duration:Int,size:Float)
  Self.img = image
  Self.x = x
  Self.y = y
  Self.duration = duration
  Self.timestamp = Millisecs()+duration
  Self.maxalpha = 0.3
  Self.size = size
  Self.sizebloat = 0
  Self.movdir = 0
  Self.movspd = 0
End Method
End Class

Function newpart(image:Image,x:Float,y:Float,duration:Int)
particle[0].AddLast(New part(image,x,y,duration,1))
End Function

Function extendpart(maxalpha:Float,size:Float,sizebloat:Float,movdir:Float,movspd:Float)
Local i:= particle[0].Last()
i.maxalpha = maxalpha
i.size = size
i.sizebloat = sizebloat
i.movdir = movdir
i.movspd = movspd
End Function

Function updateparts()
Local apptime:Int = Millisecs()
  For Local i:= Eachin particle[0]
   Local lifetime:Float = Float(i.timestamp-apptime)/i.duration
   If lifetime>0
    Local size:Float = i.size+((1-lifetime)*i.sizebloat)
     i.x += vectorx(i.movspd,i.movdir)
     i.y += vectory(i.movspd,i.movdir)
    SetAlpha(lifetime*i.maxalpha)
    DrawImage(i.img,i.x,i.y,0,size,size,0)
   Else
    particle[0].Remove i
   endif
  Next
End Function
 

Gerry Quinn

Active member
Tutorial Author
Joined
Jun 24, 2017
Messages
153
That looks okay - you are saying that the above is working, but you cannot make it work with several layers? If so, you should be nearly there. Are you remembering that every list of particles (one in each layer) has to be initialised?

Global and Field should both be fine, chances are that you only have one particle collection, so it's just a matter of which way you prefer to structure things.

Edit: are you running in debug mode, and if so are you getting error messages, or just nothing drawing?
 

nobiagames

New member
Joined
Sep 6, 2017
Messages
9
thanks gerry, i finally got it working now. i really only needed to initialize the lists.

and thanks again to everyone else helping me out here. i'm slowly getting into programming again and is would be a lot harder without this forum :)
 
Top Bottom