The Farmer Was Replaced

The Farmer Was Replaced

Syzo May 14, 2024 @ 10:18am
Variables don't work properly in high speed loops.
i've noticed something weird with variables, they don't seem to update correctly when working with high speed loops, its kinda hard to explain so ill post an example:
if comp[1] - get_pos_x() > 0 :
for X_M in range(comp[1]-get_pos_x()):
move(East)
if comp[2] - get_pos_y() > 0 :
for Y_M in range(comp[2]-get_pos_y()):
move(North)
if comp[1] - get_pos_x() < 0 :
for X_M in range(abs(comp[1]-get_pos_x())):
move(West)
if comp[2] - get_pos_y() < 0 :
for Y_M in range(abs(comp[2]-get_pos_y())):
move(South)
works fine. (just imagine its part of a big loop, i wont post the whole thing due to size)
however, doing something like this:
Loc_x = get_pos_x()
Loc_y = get_pos_y()
if comp[1] - Loc_x > 0 :
for X_M in range(comp[1]-Loc_x):
move(East)
if comp[2] - Loc_y > 0 :
for Y_M in range(comp[2]-Loc_y):
move(North)
if comp[1] - Loc_x < 0 :
for X_M in range(abs(comp[1]-Loc_x)):
move(West)
if comp[2] - Loc_y < 0 :
for Y_M in range(abs(comp[2]-Loc_y)):
move(South)
dosen't work properly. this isnt the first example i've had this issue as well.

it just seems like variables are sometimes unrelieable and dont update properly, did anyone else encounter such an issue or is it just me?
Last edited by Syzo; May 14, 2024 @ 10:20am
< >
Showing 1-5 of 5 comments
KoJeT_ May 14, 2024 @ 10:37am 
So what i've noticed is variables you call at the start of a function don't seem to get updated when you have faster loops. I have typically just been updating the drones position after each movement which sure isn't the best way to write it but i mean... i works that way so its how ill do it until i personally figure it out.
Syzo May 14, 2024 @ 10:43am 
Yeah, i've noticed that aswell, but those are variable located right near the code (inside the loop even) of where they are used.
i mean, its not a huge problem to deal with, just makes the code less read-able and it took a while to figure out where the problem was but it just infuriated me abit and i wanted to see if anyone else has noticed it.

The bigger issue i've noticed is with lists (while is an extension of the variable issue)
sometimes manipulating lists just straight up dosent work.
append works perfectly but remove has like a 5% chance of not doing anything.
i've tested it out with random_list.remove(max(random_list)) and yea, sometimes it just fails to remove anything.

(i've tested it by filling random_list with the measure() of 4 fields worth of flowers and then doing
quick_print(len(random_list))
random_list.remove(max(random_list))
quick_print(len(random_list))
)
Last edited by Syzo; May 14, 2024 @ 10:48am
SharkBite May 14, 2024 @ 5:16pm 
I've not done much with lists yet, but in your original post you never actually update any variables.

The two code snippets do different things, the second version only looks at the drone's position at the start of the snippet and then moves relative to that, whereas in the first version the first set of horizontal movement can affect the second. You could chain your statements with 'elif' if you specifically only wanted to execute a single branch rather than run multiple comparisons with side-effects.

I'm probably missing the point of your query, but it's hard to say without a better example of what you're seeing.
SharkBite May 14, 2024 @ 5:21pm 
This code works fine for me, which is about as fast a loop as you can get:
xs = [] for i in range(10): xs.append(random() * 10) quick_print(("Starting list:", xs)) while len(xs) > 0: xs.remove(max(xs)) quick_print(xs)

edit: It's possible that the remove operation is failing to find which element to remove based on floating point inaccuracy, but max should return an exact value so that seems unlikely. The above example seems to work reliably with inexact values too.
Last edited by SharkBite; May 14, 2024 @ 5:23pm
Stoob May 21, 2024 @ 5:21pm 
Originally posted by Syzo:
i've noticed something weird with variables, they don't seem to update correctly when working with high speed loops, its kinda hard to explain so ill post an example:
if comp[1] - get_pos_x() > 0 :
for X_M in range(comp[1]-get_pos_x()):
move(East)
if comp[2] - get_pos_y() > 0 :
for Y_M in range(comp[2]-get_pos_y()):
move(North)
if comp[1] - get_pos_x() < 0 :
for X_M in range(abs(comp[1]-get_pos_x())):
move(West)
if comp[2] - get_pos_y() < 0 :
for Y_M in range(abs(comp[2]-get_pos_y())):
move(South)
works fine. (just imagine its part of a big loop, i wont post the whole thing due to size)
however, doing something like this:
Loc_x = get_pos_x()
Loc_y = get_pos_y()
if comp[1] - Loc_x > 0 :
for X_M in range(comp[1]-Loc_x):
move(East)
if comp[2] - Loc_y > 0 :
for Y_M in range(comp[2]-Loc_y):
move(North)
if comp[1] - Loc_x < 0 :
for X_M in range(abs(comp[1]-Loc_x)):
move(West)
if comp[2] - Loc_y < 0 :
for Y_M in range(abs(comp[2]-Loc_y)):
move(South)
dosen't work properly. this isnt the first example i've had this issue as well.

it just seems like variables are sometimes unrelieable and dont update properly, did anyone else encounter such an issue or is it just me?

I'm having the same issue. Randomly get_pos seems to output a number while in between two tiles.

Below is what i have:

clear()

while True:
x = get_pos_x()
y = get_pos_y()


if x + y == (0):
plant_carrot()
if x + y == (0):
plant_carrot()
if x + y == (0):
plant_bush()
if x + y == (0):
plant_bush()

return

However when I add a delay by asking it to print its position it doesn't hang:

clear()

while True:
x = get_pos_x()
y = get_pos_y()


if x + y == (0):
print(get_pos_y (),get_pos_x ())
plant_carrot()
if x + y == (0):
print(get_pos_y (),get_pos_x ())
plant_carrot()
if x + y == (0):
print(get_pos_y (),get_pos_x ())
plant_bush()
if x + y == (0):
print(get_pos_y (),get_pos_x ())
plant_bush()

return[/qoute]
Last edited by Stoob; May 21, 2024 @ 5:30pm
< >
Showing 1-5 of 5 comments
Per page: 1530 50