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.)
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())
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 )
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
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())
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)
touche @MightyPhil
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.
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.
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()
.
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.
This is what the collision masks are for, but it sounds like you figured that out already.
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.
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!
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â)
No test