Author Topic: Double List Entries  (Read 2380 times)

Offline Poetronic

  • Mr. Drawsprite
  • **
  • Posts: 98
    • View Profile
Double List Entries
« on: 2012-Jul-23 »
Hi everyone,

please excuse my not so profound English,  but I thought I might want to put my questions on this international board - just in case someone else stumbles upon some related issues.

1) In my GLBasic SDK Editor the down-key doesn't work. Yeah, that's right, I can't move the cursor down manually without using my mouse. Anyone else having experienced the same problem? And maybe some advice on how to fix it?

2) In my breakout clone I would like to add an item that doubles the amount of balls in the game. My first thought was to just run a FOREACH loop through the list of balls and add a new element with the new variables for each list entry. This of course turned out to be totally non-functional, since the loop then runs ad infinitum. Now my idea is to do it like that: define a local list of virtual balls, run a FOREACH loop through the list of existing balls creating a new virtual ball for each element in the list, and then insert the newly created elements into the existing list of balls. Now for for my question: Might there be a more elegant way of doing this? My solution seems functional to me, but maybe there is a more simple solution...

Thank you!  :)

« Last Edit: 2012-Jul-23 by Poetronic »
ILI-Blocks, my first game ever - please check it out! http://www.glbasic.com/forum/index.php?topic=8654.0

MrTAToad

  • Guest
Re: Double List Entries
« Reply #1 on: 2012-Jul-23 »
Quote
In my GLBasic SDK Editor the down-key doesn't work. Yeah, that's right, I can't move the cursor down manually without using my mouse. Anyone else having experienced the same problem? And maybe some advice on how to fix it?
Sounds like you are running a very old version of the editor which had similar problems.

As for 2 - you can't add anything in a FOREACH loop as it would modify the list and cause problems.  So a temporary list is needed which is then added to the main list

Offline Ian Price

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 4159
  • On the shoulders of giants.
    • View Profile
    • My Apps
Re: Double List Entries
« Reply #2 on: 2012-Jul-23 »
Just create a new TYPE instance of the ball (at the co-ordinates you want) - the FOREACH loop will then deal with however many you create. Or have I missed something?

If your new balls are also spawning new balls, then add a flag that prevents them spawning for a set number of loops/seconds.
« Last Edit: 2012-Jul-23 by Ian Price »
I came. I saw. I played.

Offline Poetronic

  • Mr. Drawsprite
  • **
  • Posts: 98
    • View Profile
Re: Double List Entries
« Reply #3 on: 2012-Jul-23 »
Quote
Just create a new TYPE instance of the ball (at the co-ordinates you want) - the FOREACH loop will then deal with however many you create. Or have I missed something?

The problem was not the creation of the balls, but adding the balls to the list within the FOREACH loop. If you do that, the loop creates an infinited amount of new elements (since added elements also add new elements etc.). I just went with the temporary list as a solution.
ILI-Blocks, my first game ever - please check it out! http://www.glbasic.com/forum/index.php?topic=8654.0

Offline Ian Price

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 4159
  • On the shoulders of giants.
    • View Profile
    • My Apps
Re: Double List Entries
« Reply #4 on: 2012-Jul-23 »
That's why I suggested the flag that delays creation/prevention of new balls if you've just created it. No more infinite balls.
I came. I saw. I played.

Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1137
    • View Profile
    • My GLBasic code archiv
Re: Double List Entries
« Reply #5 on: 2012-Jul-23 »
If you use FOREACH and add something to the list, it gets expanded and processes the new items too, which is what happend to you and what you recognised on your own. So why do you want to use FOREACH at any cost? Normal FOR can do the job for you too and it will not give you above problem. Just a sample, lets say your Code looks like this atm:
Code: (glbasic) [Select]
FOREACH b in ball[]
  a=b
  DIMPUSH ball[], a
NEXT
This would cause the problem that you described. But the following should just do the job:
Code: (glbasic) [Select]
tmp% = LEN(ball[])-1
FOR i=0 to tmp
  ALIAS b as ball[i]
  a=b
  DIMPUSH ball[], a
NEXT
Or did i get you wrong?
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Offline Poetronic

  • Mr. Drawsprite
  • **
  • Posts: 98
    • View Profile
Re: Double List Entries
« Reply #6 on: 2012-Jul-24 »
Hi kanonet,

I like your slim solution, but I am not sure if it will do the job, since I want to base the variables of the new element on the variables of the element from the original list. For example, I want the angle of the new element to be the negative angle of the element in the list.

My solution was to create a local list of "virtual" balls which is then populated with the newly created and afterwards pushed into the primary list. It looks something like this:

Code: (glbasic) [Select]
FUNCTION SplitBalls:
LOCAL vBalls[] AS Tball, vBall AS Tball, Ball AS Tball
FOREACH Ball IN Balls[]
vBall.angle = -Ball.angle
DIMPUSH vBalls[], vBall
NEXT
FOREACH vBall IN vBalls[]
Ball.angle = vBall.angle
DIMPUSH Balls[], Ball
INC ballcounter, 1
DELETE vBall
NEXT
ENDFUNCTION

Rather ugly, I guess, but it works just fine  :-[

What do you think, is it a bad way of getting the task done?
ILI-Blocks, my first game ever - please check it out! http://www.glbasic.com/forum/index.php?topic=8654.0

Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1137
    • View Profile
    • My GLBasic code archiv
Re: Double List Entries
« Reply #7 on: 2012-Jul-24 »
It does the job so its ok. Of cause its a bit more than necessary, so if you want to switch to an easier approach, it should look like this:
Code: (glbasic) [Select]
FUNCTION SplitBalls:
LOCAL tmp% = LEN(Balls[])-1, vBall AS Tball
FOR i%=0 to tmp
ALIAS Ball as Balls[i]
vBall.angle = -Ball.angle
DIMPUSH Balls[], vBall
INC ballcounter, 1
NEXT
ENDFUNCTION
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Offline Poetronic

  • Mr. Drawsprite
  • **
  • Posts: 98
    • View Profile
Re: Double List Entries
« Reply #8 on: 2012-Jul-24 »
Hey kanonet,

thank you very much for your reply! I will make use of your solution, it looks a lot cleaner than mine. I guess the idea of using a "virtual" list is quite similar to the ALIAS command, at least with regard to the implied logic... Still, thank you very much for helping me learn something today! :)

PS - Not that I doubted it: your solution works perfectly. Thank you!
« Last Edit: 2012-Jul-24 by Poetronic »
ILI-Blocks, my first game ever - please check it out! http://www.glbasic.com/forum/index.php?topic=8654.0