r/TheFarmerWasReplaced • • 11d ago

Heelllpppp Variable outside function to be used in function

Hi, Im trying to create a multi drone pumpkin farming setup

I currently have P as the variable for the number of pumpkins seen on the map and its outside of the defined function for goin gup a row counting then planting pumpkins, when i try to run it like this it says the variable isnt defined and needs to be defined before i can use it. how do i define the variable to be used by other functions.

at first i thought it was an import issue so i recreated everything in one file but it still didnt work

2 Upvotes

2 comments sorted by

1

u/PsiThreader 10d ago edited 10d ago

The scope or the accessible addresses of data for functions is only inside that function, unless the address is declared as global. You can only send a copy of a non-global data to inside of it by passing a value to the function as an argument, but even then it won't change the original value of "a" unless you tell it to.

a = 1
def add(k):
	a = a*999999
	k+=1
	return k

#you modify the value of variable "a" to be equal to the "return value" of the function.  
a = add(a)
#variable "a" becomes 2, not 999999

Lists are the exception btw.. If you set variable "a" as a list, the function can modify it. #edited

1

u/Superskull85 11d ago

You'd need global p at the top of your functions but this is something you really shouldn't do from a coding convention. Also, that variable won't be sharable between drones. Each drone will have its own copy.