How to get a variable value before it was null ?
Hi, I'am having a problem with my chess game at the moment, I'am trying to make a protection system for example if the pawn is protected by a knight then the other team's King shouldn't be able to capture the pawn. in every chess pieces' script there is a variable c, (of the type Chessman), which equals the piece/pieces on the piece's allowed moves' spaces. So if I'am on the White team the Pawn script checks c if its not null (there is a piece of some kind/team on this piece's diagonal left/right) if c is black then this black piece is capture-able if c is white then it is protected by this piece (Pawn in this example). This part works fine, pieces are getting protected, but if i move the protected pieces to a non-protected place it stays protected and it shouldn't be. The problem here is this part of the Pawn script
else if (c == null) {
c.isProtected = false;
Debug.Log(c.gameObject.name);
}
Since c is null then I cant change this piece's isProtected value (because its null, the script cant access it) So, is there any workarounds for this problem ? Here is a sample of the Pawn script:
c = BoardManager.Instance.Chessmans[CurrentX - 1, CurrentY + 1];
if (c != null) {
if (!c.isWhite) {
r[CurrentX - 1, CurrentY + 1] = true;
if (c.GetType() == typeof(King)) {
Debug.Log("Checkmate");
BoardManager.Instance.Blackcheckmated = true;
}
else {
BoardManager.Instance.Blackcheckmated = false;
}
}
else {
c.isProtected = true;
}
}
else if (c == null) {
c.isProtected = false;
Debug.Log(c.gameObject.name);
}
Hello there.
You scrpipt is little confusing. But what is confised is you. If something is null, it means is not defined, it does not ahve a value, so this:
else if (c == null) {
c.isProtected = false;
Debug.Log(c.gameObject.name);
}
i have no sense, you saying if C is not defined, if C is nothing,.... so is imposible you do "something" to nothing.
I did not get whats the exact problem you have (didnt read the post), but what you have there is "imposible".
Good luck!
example of confusion : "equals the piece/pieces on the piece's allowed moves' spaces"
yeah, thats what im saying.. idk how to fix it.. I need to change the value of C but when it doesn't even exist. Thanks :D
Hmmm... . $$anonymous$$oving a pawn should remove protection from the 2 diagonals, but not really -- another pawn may also have been protecting. Plus it may open/close a lane for a rook or bishop.
It seems like each move needs to do an all-around redo of captureable pieces. I'm not sure your "only look at this one moving piece" plan will work.
So there is no other way to get the last variable value before it was null ?
Use another variable to save the previous value. Everywhere you have c==null; change it to cOld=c; c=null;. cOld would be the piece that used to be there (or null if nothing was ever there). But that piece could be anywhere, and could be protected by anything else. There's no reason moving a pawn which was protecting it several moves back should matter.
Answer by ZeroGravity2000 · Dec 08, 2019 at 08:44 AM
This sound some other problem in code, but I am not able to identify it because I cannot see the whole code.
However, you can store c value to another variable. Like cOld or something, and every time you are in if(c!=null) { .. } you set the cOld value. Then at least you have the previous value of the c.
c = BoardManager.Instance.Chessmans[CurrentX - 1, CurrentY + 1];
if (c != null) {
if (!c.isWhite) {
...
} else {
cOld = c;
c.isProtected = true;
}
} else if (c == null) {
if(cOld != null) {
cOld.isProtected = false;
Debug.Log(cOld.gameObject.name);
}
}
cOld should be (private) member of class.