I am a little confused as to what I am supposed to be changing or adding, am I just making changes/additions in the PoetryLine.tscn? What is the purpose of the PoetryLine.tscn and the script therein?
I was struggling with this as well and the hints were pretty vague, it made me think something was wrong in the PoetryLine.gd script, and the comments they left in that script didn't make it any more clear either. But that isn't the case. All you need to do is add 1 single line of code to the Poem.gd script at the bottom of the add_poetry_line function. Once you know that use their hints from that point.
Hi andrewjhaugen, sorry for the late reply, I just had to cool off my brain a few hours to give it a go later.
Honestly, I have tried so many things that I just don't know if anything that I've tried makes any sense anymore.
I always read the hints provided and try to use logic to understand what is required.
At first I started with the if+ yield statement, since the comment in PoetryLine.gd says these two lines allow you to call the set_text function.. then I've come here, and after seeing rafaelmedeiros94's reply, I tried calling directly the set_text function by its name. I guess I have a problem knowing when to call functions and from where.
I guess I get kind of confused by the structure I should follow. do I have to use set_text() ? Do i have to use poetry_line.set_text() ? Do I have to use poetry_line == text ? None of those made sense, so I just checked back the lessons, and I think it has to be something similar to this screenshot.
If you could please give some form of advice on this, I would appreciate it
I guess my brain is just too tangled up with concepts and lines of code, thus blocking myself out of a clearing even with the slightest of issues.
Apologies in advance for this messy explanation of my plea.
All good adrislayer! Let's try to break this down a bit.
Let's try to simplify things a bit. All I want you to concentrate on is two scripts: Poem.gd and PoetryLine.gd
Let's start with PoetryLine.gd
For now, don't worry about any of the other lines. I just want you to focus on line 6. This is a function declaration. Anytime you see something like func set_text(function arguments go in here, don't focus on that right now) -> void:
I want you to make a little mental note saying "Aha! this is a function that all PoetryLine objects can call!"
So lets say we are somewhere else in our game, maybe even in another script. Well as long as I have an object of type PoetryLine. I can write the name of that object, than a "." and then the name of a function that that object has.
For example, let's say I have a PoetryLine object called my_epic_poetry_line. If I want to call the set_text function that I know that object has. All I have to do is go
my_epic_poetry_line.set_text()
Reread that part a few times if you have to, to let it sink in.
Okay now let's continue with our explanation:
Let's examine the function's signature again (the line I put a red box around)
See the part that says "new_text: String" that is inside the parentheses of the function? This means that in order to call set_text correctly, you need to pass it a string value. So back to my example of we have a PoetryLine object and for demonstration lets call this PoetryLine object my_epic_poetry_line.
Well, if I tried to go
my_epic_poetry_line.set_text()
I'm going to have a problem, I'm not passing anything into the function and we know that set_text needs a String. How did we know that? We studied the function's signature to learn about what parameters that function needs.
ok now I think I'm ready. I have a string called, for example, my_epic_string.
Now putting it all together I would write
my_epic_poetry_line.set_text(my_epic_string)
Ok so now, we should have a pretty good grasp on both what types of objects(objects of type PoetryLine) are allowed to call set_text() and also how to call it from one of those objects.
Now let's look at Poem.gd
I've color coded some boxes around key lines to help with the explanation.
The red box is the place where the comments are telling you you need to add your one line of code to. So to pass the Practice, we need to figure out what line of code should go there.
Let's read the comment together. It says "Set the text of the poetry line instance. Open PoetryLine.gd to learn how"
Ok well we walked through PoetryLine.gd together so you should have a good grasp of the fact that all PoetryLine objects have a nice function called "set_text()", we know the way this function works is that we need to pass it a String.
"Set the text of the poetry line instance" <- ok well by the looks of the line that I boxed in orange
"var poetry_line := preload("PoetryLine.tscn").instance()
It would appear that we have a PoetryLine object that we just instanced and its name is poetry_line.
Where are we going to get the string from though? Remember if we call "set_text()" we need to pass it a string.
Look at the line I boxed in pink. It looks like inside the body of this function we have access to a string called "text"
I can't give you the answer outright, it's just not something we do for the Practices at GDQuest. If we did, it would rob you of the chance to make the leap on your own.
Having read everything I posted really carefully, and maybe taking a break if you need it, try to get what goes in that red box and claim victory on this Practice!
Good Luck!
Andrew
Thanks Andrew, you saved my hide there! I won't say which line I used, because, like you said earlier, I wouldn't want others to skip such an important realisation by themselves!
I won't lie to you, further ahead my learning path, I will most likely get even deeper into the mud, but at least I could extract a valuable lesson from this one.
Dissecting the problem like you did makes easier to comprehend, but I have to ask out of curiosity: is there any other way to make a call to a function like this one?
I want to learn as much as possible out of this
Thanks again, I guess you guys will see me around pretty often x)
Hi Adrislayer glad you were able to find your solution!
Regarding the question "is there any other way to make a call to a function like this one?" There are extra techniques that could be used, but I also don't want to get ahead of where you are at the course, as it is laid out to try to introduce you to things in a logical order.
The general concept that we want to get across here is that, many times, functions are associated with their appropriate object type. This is a good thing! For example lets say you had an Object type in your game that was a Dog. And let's say further that you had given this Dog a bark() method. Well it wouldn't make much sense if all of a sudden the Cat objects in your game starting barking everywhere right? So the function bark() is associated with Objects of type Dog so that they can logically be called from objects that would make sense to have that function.
This is a computer science term-of-art called encapsulation and it's actually very powerful. If you are new to this sort of thing, it can seem limiting at first. "Why can't I just call anything I want anywhere I want to?"
But you will soon learn in projects of scope, this type of design is battle tested for a reason, and helps you group your game code into logical and clean systems and pieces.
Hope that helps a bit,
Andrew
Hey Andrew, thanks for breaking this down, I was having a lot of trouble with this lesson. I went over your entire explanation 3 times and it took me over 1 hour to figure it out… lol.
I think I got the idea but If I was to do this exercise from scratch (meaning, writing all these lines of code from Poem.gd & PoetryLine.gd) on my own, I was going to be in trouble bc I honestly don’t understand about 90% of it. I hope these concepts will be better explained down the line with the coming up exercises.
Hi AJ!
Don't worry, this practice at this point is pretty notorious for pushing people to the brink of madness so I wouldn't worry too much! Also with the practices, yes it is nice to look at all the lines of code and try to understand them, but I also think being able to recall it from scratch is a very high bar to set for yourself. The reason I say that is I don't want you to get the impression that "Ah gee, I don't really understand all these lines of code from the practice scripts, maybe I shouldn't advance." <--I have noticed some students with this takeaway and so I just want to gently correct that by suggesting more of a mindset of "Well, I don't understand every line in these practices. Maybe I only understand the ones that I fixed but, I was able to understand that part of the script well enough to solve the puzzle so yay!"
Hope that makes sense!
Andrew