Best way to plant a crop?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Martogh

Hi, I’ve been using Godot for a few months now and i have started working on a small farming sim and i am trying to find the best way to plant a crop and have it grow over time. The struggle has been in making it be reusable easily with other crops. My attempt didn’t succeed but if you know why that would be incredibly helpful.

I has a Autoload script which had:

var selectedplant = TestFlower
var TestFlower
var TestFlower2

I would then use this in my main script for my farming tile which is as the following:

func _on_Button_pressed() -> void:
   print("Animation play")

(The Node Tree for the Farmland Scene is as follows)

This though did not work for some reason, there would be no crash or error shown rather it would just not play the animation. If there is a better way to do this or a way to fix my code please tell me it would help a lot!
Thankyou and if you need anything else please tell me!

:bust_in_silhouette: Reply From: exuin

I think the reason your animation isn’t playing is because TestFlower isn’t a string or anything it’s just a variable name, so its value is null.

For your flowers, I think you should look into inheritance. So you should have one base crop scene that can be planted and stuff and then all the other flower scenes should inherit from that. That way they all can be planted too without having to reuse code.

Sorry for the delay, I will start to look into inheritance do you have any good places to start learning about it? Again thankyou so much for the answer although I have one question: Wouldn’t the str in front of the Global.selectedplant change the text to a string allowing for the animationplayer to pick it up. If not could you please explain why.
Thankyou so much for your time!

Martogh | 2021-04-24 13:47

Yes, str() changes text to a string. However, if you give a variable, it changes the assigned value of the variable to a string. Since TestFlower has no assigned value that I can see, it changes null, the default value, to a string instead of the word TestFlower.

As for inheritance, I’m sorry, but I don’t know any Godot-specific resources for it since I learned about it outside of Godot. But KidsCanCode’s guides are usually pretty good so you can take a look at this for a start.

exuin | 2021-04-24 14:34

Thankyou so much!

Martogh | 2021-04-25 01:44