Using properties

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

Hi guys.

As we know the properties of a class are variables and they have a name.For example if i want to use the property “motion” of the KinematicBody2D i have to give a type on the variable.For example: var motion=Vector2(), and i can use the propery “x” of Vector, for example motion.x=200.I noticed that if i change the name of the property “motion” in all fields, for exampe move.x=200 then the code has no problem, but, concerning the property “rotation” i can use it directly :(rotation +=2*_delta),and if i change her name then the code has problem.Also, about the property “rotation” the code has problem too if i try to give to her a variable type.
My question is:What’s the point with the properties?The names of them are predetermined and we can use them directly or we have to give a type to them first?I noticed that both apply in some cases.

Thank you and i am sorry about my english.

:bust_in_silhouette: Reply From: Jowan-Spooner

Sorry but KinematicBody2d does not have a motion property.

Maybe this will help:
You can think of the nodes as predefined scripts (it’s more complicated but that’s a good way to understand them). These “scripts” already have some variables that are defined, in your example the rotation variable. Many of them can be edited in the inspector. They are called properties.

On the other hand each node can have another script made by you, that can contain variables that you created yourself. The scripts can also edit/set the value of the nodes properties.

Please note also that Vector2() isn’t setting a type, but instead it returns a value (an empty Vector). You do not have to set the type of any variable, though it is possible since godot3.1. Vector2 is a datatype that contains a x and a y. Of course the code will make problems if you change the name of a property, because the “script” that defines the nodes basic behavior tries to find it. Changing the type of a property leads to trouble to, because godot is expecting a certain type.

Did that help you? I hope so. Feel free to ask more questions if not.

Hmmm…it’s more clear now but i have two more questions.
When i press the F4 button and i type “KinematicBody2D”, in the properties i can see a word “motion”.So, what is it?
And second:Why, when i changed the “motion” word in “move” word in my Script, then the code had not any problem?

Nick888 | 2019-05-11 11:22

I am sorry, I am still using godot3.0 most of the time so I didn’t find it in the first place. This motion isn’t a property of the object. If you look into the inspector you can see that it is the name of one section there, holding the property sync_to_physics.

Secondly: From what you said I believe that you create the variable motion inside your script, which means it is somehow a local variable. So changing all occurrences of the name to something else will not affect any other script, thus let everything work fine. But it would become a problem if you would change the name in one place only.

Does that sound logical. Good luck!

Jowan-Spooner | 2019-05-11 12:34

Now its apsolutely clear. I mean about the “holding the property sync_to_physics”.Now makes sense and the issue with the variable name "motion"You are right!

Thank you very much!

Nick888 | 2019-05-11 12:41

You are welcome, glad I was able to help.

Jowan-Spooner | 2019-05-11 12:57