r/TheFarmerWasReplaced • u/Dragonman558 • 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
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.
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 pat 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.