Hide Lines of Code (Function)

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

I have empty RPC Functions that Simply pass

They are confusing to look at being along with The Whole Script Code… Is there anyway to hide these lines, as how when you close up an If Statement?

an answer would heavily be appreciated

:bust_in_silhouette: Reply From: crossbito

Maybe it’s a dumb idea, but if you want to hide it for now, you could create a new class that expands on the script where you previously defined those functions. For example:

Before:

myscript.gd

extends Node

// Functions I don't want to see

After

newScript.gd

extends Node
class_name NameOfThisClass

// Functions I don't want to see


myscript.gd

extends NameOfThisClass
// Now here you don't have the functions because they are in the other class.

Now you wouldn’t see those functions in the main script. This is a hack that I think would work, but when you finish your script, I would delete the script with the new class and transfer the functions back to the original script.

I have never heard of extends before, But It sounds incredibly Useful… If i am correct in my Understanding, You would use “extends” to graft in another Script as if it all together was as one?

How Would I impliment extends? I put the Functions in the new Script, and am attempting to “extend” my new Script at the Bottom of my Main Script.

“extends (GetFilePathToNewScript)” is my attempted Line to make use of “extends”

Lastly… You claimed that its recommended of me to Return the Functions to the Main Script instead of keeping “extends” on a full release… Why is that reccomended?

Nickyroo | 2023-06-04 02:20

Extending a class signifies inheritance itself. For example, in Godot, you have the Node and Node2D classes. Node2D extends from Node, which means that all Node2D objects(with all the properties and functions of a Node) are Nodes, but not all Nodes are Node2D.

It is not recommended because, in your case, Script A and Script B are essentially the same. You extend A from B, but you only intend to use A. Therefore, A and B are identical objects, defeating the purpose of “extends”. Using “extends” in a way that deviates from its intended purpose, thats why I said it’s a hack. It would be better to create a new class with its own purpose and use it as needed.

I recommend reading about inheritance if you are not familiar with it. It is an important concept in object-oriented programming (OOP). However, personally, I wouldn’t recommend relying heavily on OOP in Godot (although you can if you prefer to do so). Ultimately, it’s a personal choice.

For example, you can use “extends” to create a class called “Enemy” that extends from “CharacterBody2D”. Then, you can create classes like “Ogre” and “Guard” that extend from “Enemy”. This allows you to define common properties and functions in the “Enemy” class that all enemies share, while also having the flexibility to add unique properties and functions to the “Ogre” and “Guard” classes.

When you create a script in Godot, it automatically adds an “extends Something” line. However, you only need to include the “class_name” statement to define your custom class. Then, when you create a new script, you replace the “extends” keyword with your custom class to inherit from it.

For example, let’s say you have a script that extends “CharacterBody2D” and you want to name your custom class “Ship”:

extends CharacterBody2D
class_name Ship

In another script, let’s say you have a player that is a Ship:

extends Ship
class_name Player

This means that the player is both a Ship and a CharacterBody2D because the Ship class extends from CharacterBody2D.

crossbito | 2023-06-04 05:03

Thanks for sharing this Answer with me, You have been, of course, amazing help!

This feature will be great for more than simply Organizational tools… I have tested it with Basic Nodes and it works great for what I have imagined it to be, I just have left to figure out how to make this compatible with an Auto-Loaded Script if possible, and of course… Learn more about how to use it

Thank you! :slight_smile:

Nickyroo | 2023-06-06 05:50