Can I create variable 0?

For the chess engine I am trying to make, I need to be able to check a bunch of variables. Each tile on the board has a variable bound to it. And if there is a pawn on that tile it will be set to 1, with a rook it is set to 2, a knight to 3, etc etc.

All the tiles that fall outside the board will be set to 20, which is not assigned to a chess piece, but is also not an empty tile to move to.

However... The knight, moves just a bit outside those limitations when moving from H1. The formula will then try to read variable 0, which doesn't exist and everything crashes, my computer explodes and the whole town as to evacuate...

Okay, that was a little over dramatic, but the game will crash when the formula tries to read game variable 0... So, can I create a variable 0? Even if it can't be used by any of the default functions?

(I know... I could make that tile var 1 and then the next one var 2 and so on, and so on. But I didn't think of that before. If I want to do that now, I have to re-do 64 events * 12 pages = 768 items)

Edit: Oops, it is not 64*8=512 items, but 64*12=768 items, since there is unique 12 pieces in chess and not 8.
$gameVariables' _data property does actually have a 0-index, as does every array since that's how arrays work, but in accordance with the standard RM data structure, it's assigned the value "null". You could assign it a value, or just check if the variable is not null before doing whatever else you're doing.
So, I can try $gameVariables.setValue(0,10) ?
I'll try that right now, thanks! I could have just tried that before I asked, but I didn't think it was possible. 😛

Edit: Well, that didn't work. 😛
So, I can try $gameVariables.setValue(0,10) ?
I'll try that right now, thanks! I could have just tried that before I asked, but I didn't think it was possible. 😛
That won't actually work because there's a check for the id being above 0. You'd have to use $gameVariables._data[0] = whatever
You can create variable 0 like this:
JavaScript:
$gameVariables._data[0] = 1337;

But I dunno if that's going to solve your problem, because trying to access an empty variable isn't generally going to cause the game to crash. $gameVariables.value(0) is already coded in a way that it should be returning '0' if the 0 variable is null.
JavaScript:
Game_Variables.prototype.value = function(variableId) {
    return this._data[variableId] || 0;
};
Awesome, I'll try that out.
Thanks to both of you.

(Long time since I felt like a noob! Haha)

Edit: Works!! Thanks a lot!

But I dunno if that's going to solve your problem, because trying to access an empty variable isn't generally going to cause the game to crash.
Oh, things got in a weird loop when the script reads var 0 before, what caused this loop I don't know. Maybe it was because pieces move outside the borders of the map this way.

I don't know, but with this "$gameVariables._data[0] = 1337;" the problem is solved.
Maybe I'm having a brain fart, but I can't figure out how the knight could be off the board in the middle of its move and still land on a legal position.
Maybe I'm having a brain fart, but I can't figure out how the knight could be off the board in the middle of its move and still land on a legal position.
That is the whole thing, it wouldn't land on the board even. 😛 It would land 2 steps above, and one step left of the board. 😛

Let me show you. Each tile has a variable assigned to it, also tiles out side the board. If the knight is on H1, which is the upper left corner of the board, and go 2 up and 1 left, it ends up on map tile 0,0. Which is (because of a stupid oversight on my side) assigned variable 0.
(I assigned regions to it, to show which tile is assigned which variable)

1644022780885.png
Then I don't understand why you are writing code that can allow an illegal move.
Then I don't understand why you are writing code that can allow an illegal move.

I didn't. It shouldn't allow an illegal move, the code is supposed to prevent that, not to allow it. But there was a bug, since there was no way to set variable 0, not as far as I knew.

Edit: That was the whole point... Somehow the knight was able to escape trough var 0, and then things went south. That is what I tried to solve. 😛 And did solve thanks to Arthran and Trihan. 😛
I'm not trying to mess with any of the help you got, I was just being confused 😛

In my head, I don't see the need to set a variable to an illegal location in order to be able to do checks to preemptively know it would be an illegal location. I should think you only actually store anything in a variable when a piece is there.

But that's okay, you don't need to talk me through your event logic...I'm pleased it's working for you 😀
Haha, I know you didn't mean to mess. Uhm... I wouldn't mind explaining, but I don't know if I can.

See, the engine as I am making does not see tiles, only variables. And the value of each variable means something different:

0 = Empty
1 = Blue Pawn
2 = Blue Rook
3 = Blue Knight
4 = Blue Bishop
5 = Blue Queen
6 = Blue King

7 = Red Pawn
8 = Red Rook
9 = Red Knight
10 = Red Bishop
11 = Red Queen
12 = Red King

20 = out of border = wall

So... All the variable outside the board are set to 20. That way, if a rook on the left border tries to move left, it bumps into a 20.

However... The knight does jump over a row of 20's and if in the upper left corner, it would jump into variable 0, which should be set to 20, but I did not know how. Because that var was not set to 20, the knight could jump out of the legal borders. 😛

Original thread: https://forums.rpgmakerweb.com/threads/can-i-create-variable-0.144640/