#Stop runnable from evaluating expression before it's ran

1 messages · Page 1 of 1 (latest)

ebon tulip
#

So I've got this player and I'm getting their location, then in the future I'm getting their new location and calculating a vector between them. Problem is, the expression that calculates the players new location is evaluated before the runnable is executed.

Entity entity = ...
Coordinates oldLocation = entity.getLocation();
delayedTasks.add(new TimedAction(2, TimeUnit.SECONDS () -> {
  Coordinates newLocation = entity.getLocation();
  Direction direction = Direction.getVector(oldLocation, newLocation);
//this is returning no direction because old location and new location are the same
}));
pine fogBOT
#

<@&987246399047479336> please have a look, thanks.

pine fogBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

ebon tulip
#

This would be a neat Java riddle

ebon tulip
#

I'm submitting the task to be delayed and I want Coordinates newLocation = entity.getLocation(); to be evaluated after that time period

#

But instead it's evaluating at the time of me submitting the runnable

acoustic trellis
#

Then you have a bug in the mechanism which delay the execution

ebon tulip
#

No it works totally fine

#

Let's say I had the following

acoustic trellis
#

Then why isn't it delayed?

ebon tulip
#

It is delayed

#

That's not the problem

#

The problem is the assignment is done when I'm creating the runnable, not when I'm executing it.

acoustic trellis
#

Which assignment?

ebon tulip
#

newLocation

acoustic trellis
#

Then it's not delayed

ebon tulip
#

It is delayed 100%

acoustic trellis
#

How can you say the code in the runnable is executed when you create it, but at the same time it is delayed ?

#

It's either one or the other (or both)

ebon tulip
#

Okay let's say I have this

#
int foo = 1;

int randomNumber() {
  return ...
}

Runnable runnable = () -> {
    System.out.println(foo + randomNumber());
};

runnable.run();
#

random number is called on execution of the runnable

#

But what about foo? does it try to access the address that foo is at when it's executed?

acoustic trellis
#

foo will always be 1 in the lambda yes

#

Because int is immutable

#

Which isn't the case in your code

ebon tulip
#

Right but is it copying the value or the reference

#

because if foo was changed by some other thread, would it affect the runnable?

#

Or will foo always be 1?

acoustic trellis
#

It can't be changed

#

It won't compile

#

But if foo was a list

#

And add was for example called

#

Yes it would be changed

ebon tulip
#

Right I understand mutability

#

You're making my second guess myself because now I don't even think I know what it is I'm asking

#

The delayed task processor works for everything I've thrown at it

#

Minus this 1 really weird scenario

acoustic trellis
#

Add a print in the lambda and outside the lambda and see if they execute at the same time

#

If that's the case, then you probably have a bug

ebon tulip
#

I'll try that tomorrow

acoustic trellis
#

But what you said until now doesn't make sense

acoustic trellis
ebon tulip
#

I'm actually gonna test it now

acoustic trellis
#

Because what you are saying is that it's delayed but at the same time not delayed

#

Well

#

Another possible cause would be if you didn't actually edit the entity within the 2s

#

You also need to check this

ebon tulip
#

@acoustic trellis Nvm you're right lol

#

It was running at the same time, I added a little more delay and it works just fine

#

Thank you so much @acoustic trellis sorry about my nonsense but you were absolutely right