2019-03-16 02:49:09

Yo. What's happenin'.
So I have a class in bgt which is for a cannonball. The cannonball has values of x, y, and facing. I have all this set up with a cannon, and it works. My problem, however, is this.
Note that the quotes replace brackets Because I was getting a forum error when atempting to paste the original code.
if(facing_cannonballs"i".x<=0)
{
p.play_2d("cannonballwall.ogg",me.x,me.y,facing_cannonballs"i".x,facing_cannonballs"i".y,false);
facing_cannonballs.remove_at(i);
}
else if(facing_cannonballs"i".x>=max)
{
p.play_2d("cannonballwall.ogg",me.x,me.y,facing_cannonballs"i".x,facing_cannonballs"i".y,false);
facing_cannonballs.remove_at(i);
}
I also have it do damage and it all works. All but the lines above. When it reaches a boundry, being either 0, or the max coordinates, it runtimes. I'm not sure why. I will paste the runtime text here, in case it some how sheds some light on the situation.

Call stack size: 3

File: H:\game\includes\facing_cannonball.bgt
Line: 46 (1)
Function: void facing_cannonballloop()

File: H:\game\game.bgt
Line: 61 (1)
Function: void game()

File: H:\game\game.bgt
Line: 52 (1)
Function: void main()

That's all it says. If someone could either explain why it runtimes, or paste the fixed code here, that'd be great. Also let me know if I should paste the class for the cannonball. I could do that as well.
Thanks for any help in advance

-
"There is beauty in simplicity."

2019-03-16 03:08:43 (edited by Hijacker 2019-03-16 03:09:34)

Well, that code doesn't seem to be enough, I guess there is actually a loop ment to run around this code, so without that one we cannot help you. If its a for loop, manually removing entries from an array will end you up accessing higher indices which don't exist anymore and skipping earlier ones, but thats just me guessing what might be wrong here.
I'll try to explain my thoughts to you. Lets just say your array has 10 entries right now, and thus you run a for loop from 0 to 9. Now, while i is 0, your code kicks in. Your first if is correct, thus the sound plays and the variable with index 0 is removed from your array, its now 9 items large, ranging from index 0 to 8, but your loop will still run from 0 to 9. Now, your loop continues and will check your 1'st entry. Note that that already skipped one entry, entry 0, the former entry 1, because you deleted the 0th entry, thus making entry 1 become entry 0. Even if your if-clauses now will always not be true, as soon as i becomes 9, you'll encounter a runtime error, because an item with i = 9 doesn't exist anymore within your array, because the largest one is now 8.
TL;DR: Loops reducing the array's size are usually done by using do-while-loops incrementing the index manually, something like:

int[] arr={0,1,2,3,4,5,6,7,8,9};
i = 0;
do
{
  if(arr[i] == 0)
  {
    arr.remove_at(i);
    continue;
  }
  i++;
}
while(i < arr.length());

Best Regards.
Hijacker

2019-03-16 05:36:46

that and you have to paste the contents of the dialog using something like NVDA's review cursor to copy the text out, that call stack stuff is useless.

Facts with Tom MacDonald, Adam Calhoun, and Dax
End racism
End division
Become united

2019-03-16 07:05:29

I'd try restarting the loop if a cannonball got removed, so then the for loop counter  will  go back to 0, and no more runtime will occur.

2019-03-16 17:48:45

rory-games wrote:

I'd try restarting the loop if a cannonball got removed, so then the for loop counter  will  go back to 0, and no more runtime will occur.

Thats one way, but a rather costly one, that would mean wrapping this loop inside another loop which costs alot of calculation time which you don't need to apply if you do things like the loop I posted above.

Best Regards.
Hijacker

2019-03-16 20:02:30

Wouldn't it just require an if statement?

2019-03-16 20:51:04

If you remove something at i, and there's something else after that bit in the loop, it will crash as described.
I would add a continue after the removals, unless you have some other cleanup to do. Possibly
i--;
continue;
Since leaving out the i-- can have weird results, since it might skip the former i+1.

看過來!
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
    George... Don't do that.