I'm new to programming. Can you name a function anything you want?

4.3

Hi so i’m currently trying to learn godot and programming and from what I’ve gathered is that you can name your functions anything you want.

However what confuses me is why in Godot and other programming languages, is why is there fixed function names that do specific things?

e.g: if I write the function:

func update_score():

This function above is a function already in the GDscript, so I’m confused because it’s not like I can just change the name of this function, since it already does something?

And it’s not like I can just write a function called “func abc1():” and get it to do the same thing as the functions already in GDscript?

Hopedfully this makes sense, I’m struggling to articulate it

Your answers mean a lot, thank you so much

I don’t think GDScript has a function called update_score
You can name functions anything you wish, but they should reflect what you want that function to do.
But it’s up to you what code you write in there, and what that function will do.

There are quite a few built-in functions in Godot that can help you develop your game, but I recommend reading up on Object Oriented Programming to better understand basic concepts about creating functions and dealing with classes.

1 Like

Yes, you can name it anyway you want, but there are certain rules you must follow, i.e Your function should not start with digits, or some special characters that have significance in your language: ^ & * = + etc.

1 Like

you can call thant functions in your new function so easy like this :

func myfunction123() :
    move_and_slide()
    set_deferred("someting")
    set_visible(true)
    queue_free()

even you can update your function by calling by it self !

func functt ():
    functt()

May I ask what makes you think this is a built in function? Where did you get that idea or what made you believe it was built in?

Just curious.

Naming functions is more difficult than you think. At first it seems to not be an issue at all, until months later you come back to a piece of code and realise your function names are obscure, misleading or sometimes downright mystical. This is often the case with variable names too.

Like what do these functions do?

func update_player():

func refresh_skills():

func pass_to_safety():

But now they make more sense:

func update_player_ui_stats():

func reset_skill_cooldown_timers():

func teleport_player_to_checkpoint():

I can’t even count the amount of times I have come back to older code and wondered “what on earth is this piece of code doing?”. With good function names and variable names, this rarely happens.

Take your update_score function. What exactly is it doing?

increment_player_score(points_scored: int) -> void:

set_current_score(new_score: int) -> void:

sync_player_score_with_server() -> void:

apply_score_multiplier_and_update_ui(multiplier: float) -> int:

update_high_score_if_exceeded() -> bool:

add_bonus_score_for_time_remaining(time_left: float) -> int:
4 Likes

Good answer!
A few months later you may forget what that function does, if you write it anyway you like

2 Likes

I see, thank you, so from what I understand, all game engines have built in functions so we don’t have to code a lot of stuff and make the functions ourselves?

I see so you can name the function definition (the top line) anything you want, but the lines inside the function should actual functions? i hope that makes sense lol

Yeah I understand that. Thanks for the help, it was still really useful. But what I’m asking, is, how do you make a function you create using your own customised name, do anything in your game?

Like doesn’t the name of the function decide what it does?

Hope this makes sense, im really new and just confused about functions since it seems like there are premade functions in every game engine, as well as functions you can name your own names? or can you name them all your own names?

but im just confused since i swear the name of the function is what tells the game engien what you want the game to do?

Yes when declaring a function or variable you can name it anything you want, declaring means the line starts with var for variables and func for functions. You cannot declare a name with spaces, operators (arithmatic + - * /, or programming [ ] ( ) ,), and the name cannot start with a number.

var my_variable: int = 1
func myFunction123() -> void:
   print("Hello from my function, my variable is ", my_variable)

When using a variable or function it must have been declared somewhere else in the script, in the same scope or a greater scope, or Godot will have no idea what you are telling it to do.

There are some overrideable functions that are called automatically by Godot, like func _ready and func _process. The former is called once the node is added to the scene tree, and the latter is called every frame after that. You can read more about them in the documentation for your extended node.

Check out this tutorial on scripting in GDScript

1 Like

Nope! The name can be anything you wish for the most part, what matters is the actual code that is inside the function’s body, aka: What’s written under the function name before you close it. The name itself should tell you what the function does, yes, but the name itself won’t influence what the function actually does.

Ahh this makes a lot more sense, thank you Tiba

I see thank yuo for explaining. I’ll check out the tut too!

The code inside functions gets executed once the function gets called at some place in the code. Your main entry points are the _ready and _process functions of nodes, that can then call other functions.

Be aware that _ready and _process are just internal functions of the class Node, that you overwrite with your code.

You can always overwrite existing functions of the class you are extending by simply declaring them in your script. But mostly you will come up with your own custom namings and call them in some other part of your script.