Error - Program Error: Cannot read properties of undefined (reading 'fields')

I want my player character to lose if they stay in a dead end on a maze for more than 10 seconds. Whenever I run this, I get the error code in the title
Here’s the code


Thanks in advance!

1 Like

uh maybe add it in game update 500 Ms

Unless there’s something weird going on with the “after…” block, which I’ve never used, your problem is most likely that, at some point in your program, this code runs before “PlaceHolderCharacter” is a sprite. If this code runs before that is made into a sprite, it will error because it can’t find the sprite location, because it isn’t a sprite. To fix this, either spawn the sprite at the very start of the On Start block (or at least before any delays happen) or add a variable like “playerIsCreated”, set it to False at the top of On Start, and then set it to True after the sprite is created. Then put a block like “If (playerIsCreated)…” around all the code in the On Game Update block.

I will say, though, that what you are trying to do is… not how you should do it. Specifically, if the player is in a dead end, your program will start a new 10,000 millisecond timer every frame, which will probably make your game very, very laggy. Also, you never keep track of the position of the player. This means that if someone went into one dead end and then went into another less than 10 seconds later, they would die.

What I would do is have 2 variables. I’ll name them something like “deadEndLocation” and “deadEndTime”, but you can name them whatever you want. First, I would switch the On Game Update block to an “On Game Update every (100) ms” block. Then, inside the If blocks that check for the 3 tiles, I would use this:

This code does a few important things: First, if the location of the player is not the same as it was last time this was run, then that means the player must have exited the dead end and found a new one. If this is the case, then the “deadEndLocation” would not be equal to the current player location, which would cause the “else” bit to run, setting the “deadEndLocation” to the current player location and, importantly, resetting the “deadEndTime” to 0. If the “deadEndLocation” is the same as the current player location, then that must mean that the player is still in the dead end, which will continue to increase the “deadEndTime” until it reaches 100 and kills the player.
The last important thing, not shown in the image, is that all 4 of the dead-end-checking If blocks should be turned into one long “If, else if, else if, else if, else” chain. This is so that, if the player is not in any dead ends, the code will run the “else” section. Inside that, I would place a “set (deadEndTime) to (0)” so that the timer gets reset if the player is not in any dead ends. This will prevent the player from dying if they go into a dead end, leave before the timer is done, and then go back into the same dead end again, which would make them die much faster if the timer was not reset when they left.

Hope this helps and happy coding!

ngl i litterally cant understand that at all

1 Like