LoginLogin

Question about 'Duplicate Variable'

Root / Programming Questions / [.]

MinxrodCreated:
Hello! :) I was trying to make a game where you move around a car/tank thingy and shoot dots at other tanks. But before I got to that step, I tried to test it, but it kept giving me a duplicate variable error at line 33, which said DIM X[4]. It only gets accessed one when running so I don't know why it did that... If anyone knows what I should try, I would like help :) EDIT: What the code looked like:
@BEGINGAME
ATTR 0
DIM X[4]
DIM Y[4]

This may have changed from Petit Computer, but you may need a CLEAR statement at the beginning of your program to clear the variable memory. After you ran it once, the array was declared as expected, but when you stopped it, it stays in memory. Then when you ran the program again, that array already existed.

It's different now, CLEAR is a syntax error in EDIT mode.

You can just run TEMP=POP(X):TEMP=POP(Y) as many times as you have elements in the array. POP is an function that will take the last element from the array, removing the element from the array (actually changing the array size), and returns that element. EDIT: Oh, yeah. You then use PUSH X,0 to add an element to X (in that example, the element being added is 0). Another solution is to do something like this:
VAR X[4]
VAR Y[4]
' blah blah blah
@BEGINGAME
RESET 4 OUT X
RESET 4 OUT Y
DEF RESET SIZE OUT ARR
  VAR AR[SIZE]
  ARR=AR
END
That is a way you can redefine an array. In fact, you don't even need to be redefining. That function can straight up define an array.

The issue I have is not redefining, it is that the array won't work at all in the first place. I define it but it says I can't for some reason. I don't really need to redefine it or anything like that. Also I need my array to stay four elements, so I don't know if POP will help. I appreciate these answers, but I can't see them helping me yet... Maybe I don't understand. EDIT: I missed your edit. But I still can't get the array to define - the error happens on DIM X[4]. The issue isn't using the array yet.

The issue I have is not redefining, it is that the array won't work at all in the first place. I define it but it says I can't for some reason. I don't really need to redefine it or anything like that. Also I need my array to stay four elements, so I don't know if POP will help. I appreciate these answers, but I can't see them helping me yet... Maybe I don't understand.
You should only be defining arrays at the top of your code. If you need to clear the array, you do something like this:
FOR I=0 TO 3
  X[I]=0
  Y[I]=0
END

So if I define the array at a later point in the code it won't work, even if it wasn't defined anywhere else?

Found the issue maybe - It gives me a type mismatch between an array called X and variable X. Moving it to the top did make defining work though, and led to this discovery. Thank you! :)

Have you done an ACLS at the beginning of your program? EDIT: NVM

Apparently array can't be the same name as a variable. I learned something today :)

I found something interesting, actually! Apparently DIM can actually be used to clear an array, as long as that is the only time you DIM that array. It won't give a duplicate variable error either. EDIT: You can see that behavior with this code:
@T
DIM T[1]
?T[0];
T[0]=RND(2)
?T[0]
GOTO @T