How to achieve this lesson with GDscrpit?

my code is above, because gdscript doesn’t support generic, I dont know how to achieve the
result of this lesson

It looks like you’re trying to challenge yourself by following the lectures and building the project in GDScript instead of C#. That’s a cool idea =)

I haven’t done this course, but I did check the lecture to see Luis’s explanation of generics. Consider this example slide he posts:

There are two components to this function:

  • 1: By changing the generic, you kind of create an overload for the function, which is not directly supported in GDScript anyway. Not a problem because this is not a true overload with different code inside it.
  • 2: Conditional logic (resembling a match statement actually) is then performed on the provided generic.

Both of these things can be emulated in GDScript:

  • 1: To address this, simply avoid statically typing the parameter. Then the argument can be of any type.
  • 2: You can use the is keyword to check if the argument is a particular type, and from there you can run the relevant code.

Consider this simple example I put together:
The output of this is exactly what you would expect it to be. Note that the else-statement will always be required when you do this in GDScript because the lack of generics means that you could pass an argument of any type into this, so you need to include a default case to gracefully handle unexpected input. If you’ve come this far converting C# into GDScript, it should be easy for you to take this example and suit it to your needs =)

1 Like

Thanks for answering, although I still cant get too much, but I achieved similar results like this,
maybe after somedays I will review this code to understand.

I think I see what you’re doing here; sure, whatever works! Good luck!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms