Is there such thing as export constants?

4.5

Just a quick question. I just needed to know if export is able to be a constant because I usually use speed as a constant and I wanna make it changeable in the editor. (unless export var is like a constant.)

(I have another question because this one is so short soo..)

I have an issue with adding a child.

var raycast: RayCast2D = owner.add_child(RayCast2D)

It says invalid argument… why?

RayCast2D is the class, but you can only add objects of a class to your scene. You can create an object via the new() function

var raycast: RayCast2D = owner.add_child(RayCast2D.new())
2 Likes

If you want to change the in editor, it is no longer a constant.
( i will anwser the second question becasue the first one is so short :stuck_out_tongue: )

RayCast2D is a class not a node. Maybe “RayCast2D.new()”

But owner.add_child() does not return anything so you cannot assign it to a var

6 Likes

add_child() doesn’t return anything. Try:

var raycast: Raycast2D = RayCast2D.new()
owner.add_child(raycast)

Or if you don’t need to keep raycast around in the local function:

owner.add_child(RayCast2D.new())
6 Likes

Wait I have another question… if this does make a raycast then how do I know what direction it’s pointing at? Because i’m making some sort of physics controller, so that raycast is meant for seeing if it interacted to the wall, if so, turn. But the thing is how do I know if the raycast is facing left or right? And how do I make 2 raycasts?

(Sorry for bad grammar, it’s late for me.)

You probably want to set things like target_position.

var left_ray: RayCast2D = RayCast2D.new()
var right_ray: RayCast2D = RayCast2D.new()

left_ray.target_position = Vector2(something...)
right_ray.target_position = Vector2(something...)

owner.add_child(left_ray)
owner.add_child(right_ray)
2 Likes

touche @MightyPhil :smiley:

Phil has a point about the constants, but figured Id add more insight. Just remember that constants are variables whose values don’t change at runtime. Theyre like the settings required for the software to run properly, which is why they need to remain unchanged.

So a variable you update in the inspector could be a constant so long its value remains unchanged during runtime. Of course, you can export any variable you like.

2 Likes

But it’s saying:

_physics_process(): Can’t add child ‘@RayCast2D@3’ to ‘PhysicsController’, already has a parent ‘PhysicsController’.

Why?

var left_ray: RayCast2D = RayCast2D.new()
var right_ray: RayCast2D = RayCast2D.new()
func _ready() -> void:
	left_ray.target_position = Vector2(-14, 0)
	right_ray.target_position = Vector2(-14, 0)
	if DetectWalls:
		owner.add_child(left_ray)
		owner.add_child(right_ray)
		if left_ray.is_colliding():
			owner.direction = 1
			if right_ray.is_colliding():
				owner.direction = -1

Hahaha, that’s true. :grinning_face_with_smiling_eyes: :+1:

1 Like
		owner.add_child(left_ray)
		owner.add_child(right_ray)

This should happen only once, so you should do this in _ready(), not in _physics_process().

1 Like

Omg thanks! Thank you everyone too:)

I just had to add .call_deferred()
Because it told me to.

Ok but how do I make it only collide to the environment and not enemies or the player?

Edit: I think I fixed it.

1 Like

This is what the collision masks are for, but it sounds like you figured that out already.

1 Like

As people mentioned, a constant can’t change during runtime. Otherwise it’s just a variable. However not changing a variable does not make it a constant. A constant is processed differently and is therefore faster to access when your game is running. If you have values that don’t change, use constants. Otherwise, don’t worry about it.

Making speed as an @export variable is very common and won’t hurt anything.

2 Likes

There are no export constants.
If you type something like@export const SPEED into the Godot script editor, it will show an error.
Hope this helps! :godot:

1 Like

Hi, thanks for the info.

But I have a question… when you press my account, do you see my profile description that says “test”? If not, good.

(Because that test word is meant to be hidden because I turned on “hide public profile”)

2 Likes

No test :gdparty:

1 Like