Hello, I have written a script in C# for a scene that contains a TileMap that is supposed to be updated when the user presses the “enter” key near a tile. I am trying to setup the script to change the tile’s appearance when the player has pressed “enter” multiple times and that part works, but if I move the player character to another spot on the TileMap when the scene is running, nothing happens. Only the tile near where the player character first spawns at the start of the scene changes as the player presses “enter”. I have included print statements to see the Global Position and the TileMap position, but each time, they return the same the positions for both:
“Player’s global position: (585, 157)
Tilemap position: (18, 4)”
This is the function in question:
`public override void _PhysicsProcess(double delta)
{
PlayerCharacter playerCharacter = GetNode<PlayerCharacter>("PlayerCharacter");
if (Input.IsActionJustPressed("INTERACT"))
{
PRESS_COUNT++;
Vector2 playerGlobalPosition = playerCharacter.GlobalPosition;
GD.Print("Player's global position: ", playerGlobalPosition);
if (tilemap == null)
{
GD.PrintErr("Tilemap is not assigned.");
return;
}
Vector2I tileMapPosition = tilemap.LocalToMap(playerGlobalPosition);
GD.Print("Tilemap position: ", tileMapPosition);
if (PRESS_COUNT == 1)
{ GD.Print("Hit Enter Once");
Vector2I tilledAtlasCoordinates = new Vector2I(7, 1);
tilemap.SetCell(0, tileMapPosition, 2, tilledAtlasCoordinates);
}
else if (PRESS_COUNT == 2)
{
GD.Print("Hit Enter Twice");
Vector2I wateredAtlasCoordinates = new Vector2I(7, 4);
tilemap.SetCell(0, tileMapPosition, 2, wateredAtlasCoordinates);
}
else
{
GD.Print(PRESS_COUNT);
}
}
}`
I am not sure why it doesn’t update the character’s position anymore. Any guidance in the right direction would be appreciated!
we don’t know what it is, and how you moving. your script looking fine. try debug your PlayerCharacter check inside this scripts your global positions off-topic
you could like make GD.Print like this maybe
GD.Print($"Player's global position: {playerGlobalPosition}");
This is a singleton I set up called “PlayerCharacter” and attached to the root node of my “PlayerCharacter” scene. My root node is an Area2D and I handle movement with a child node CharacterBody2D and MoveAndSlide() with the direction keys.
Running it again with more print statements, I noticed that the positions that the terminal printed were where the pivot point of my “PlayerCharacter” scene was located on the scene that contains my tilemap.
I’m thinking I’ll probably have to use something smaller to detect what tile the player is near- maybe attach a small Area2D child node to the “PlayerCharacter” CharacterBody2D? I’m relatively new to programming so I’m not entirely sure.
I fixed the positions not updating as my character moved by instead using my CharacterBody2D child node from the “PlayerCharacter” scene. In the script, I replaced using the root node Area2D with the CharacterBody2D for the “playerGlobalPosition” variable. Like this:
//The rest of the code preceding and following this snippet
//are still pretty much the same from my initial post
var characterBody2D = playerCharacter.GetNode<CharacterBody2D>("PlayerCharacter");
if (Input.IsActionJustPressed("INTERACT"))
{
PRESS_COUNT++;
Vector2 playerGlobalPosition = characterBody2D.Position;
GD.Print("Player's global position: ", playerGlobalPosition);
I am now running into problems with other tiles not updating when I press enter near them, and I think it’s due to my reliance on my If statement and “PRESS_COUNT” variable to set cells, so I will try to change that to use Custom Data Layers instead. Also, some of the positions that the print statements return are a little out there and nowhere near where my CharacterBody2D sprite is visibly appearing when I run the project so I’m not sure what’s going on there but still trying to figure out