I have a small issue with storing direction, I have a variable (isAligned) that will check whenever the enemy is aligned with the player’s X or Y axis. If it is, then it will move toward the player based on what side of it it’s on. But, when it isn’t aligned at all, then it stops moving. I have a variable (lastMovement) that is supposed to store the last movement value, but I’m not sure how to do that. How can I make it so the last movement value is stored when the enemy isn’t aligned?
Here’s the code:
Vector2 movement;
Vector2 lastMovement;
bool isAligned;
isAligned = (Position.X - target.Position.X > -2 && Position.X - target.Position.X < 2) || (Position.Y - target.Position.Y < 2 && Position.Y - target.Position.Y > -2);
if (isAligned)
{
if (Position.X - target.Position.X < -2)
movement = Vector2.Right;
else if (Position.X - target.Position.X > 2)
movement = Vector2.Left;
else if (Position.Y - target.Position.Y < -2)
movement = Vector2.Down;
else if (Position.Y - target.Position.Y > 2)
movement = Vector2.Up;
}
else
{
}
lastMovement = movement; // gives me an unassigned local variable error
Position += lastMovement * speed * (float)delta;
I’m going to go ahead and assume your code snippet exists within a function. If that’s the case, your lastMovement variable (and the other variables) are defined in the local space of the function. Local variable definitions do not exist outside of a function call and are not persistent in memory. If you want to store a value for use in future updates, you have to define a variable in the scope of the class, not the function.
I hate to tell you that this is basic programming knowledge. If you don’t know what scope is in programming, I suggest you read up on what it means.
Here’s an example of defining lastMovement in the scope of the class:
public partial class Enemy : Node2D
{
Vector2 lastMovement;
public override void _PhysicsProcess(double delta)
{
lastMovement = Vector2.Right;
}
}
Just so you know this as well, it’s a good idea to use the same datatype in comparisons. Your Position.X is a float so your -2 should be a float as well. This can be done with the f suffix like so: -2f.
A final tip for you is that your code’s readability is important. It wasn’t obvious to me what your condition(s) was, and I didn’t immediately notice that the conditions in isAligned actually differ from those used within the if-block. I would suggest improving your code’s readability like so:
# ...
Vector2 posDiff = Position - target.Position.X;
bool xAligned = (posDiff.X > -2f && posDiff.X < 2f);
bool yAligned = (posDiff.Y < 2f && posDiff.Y > -2f);
isAligned = xAligned || yAligned;
if (isAligned)
{
if (posDiff.X < -2f)
movement = Vector2.Right;
else if (posDiff.X > 2f)
movement = Vector2.Left;
else if (posDiff.Y < -2f)
movement = Vector2.Down;
else if (posDiff.Y > 2f)
movement = Vector2.Up;
}
# ...
You should probably also avoid using explicit values in your code. It would, for example, be tedious to change all 2f to 1f. Therefore, store this value (whatever it is meant to represent) inside a variable so it can be easily changed if needed. I’ll let you decide if this is needed.
I hope this helps you solve your issue. Let me know if there is anything else you need help with.
Thanks for the response, your solution worked perfectly. I had a feeling it was an error pertaining to general programming knowledge, at the time it just didn’t hit me that it had to do with scopes. But I did realize how much of a difference there is between a variable in a function scope or class scope. Also, thank you for the tips! They’re perfect because I was planning to overhaul my original code (it was just a framework to get the logic down) to make it more readable. Thanks again for the help!