• Godot HelpProgramming
  • How to temporarily ignore a collision, and how to refer to nodes outside a script's scene?

Hi, all!

I'm creating a 2D platformer that includes ladders, and I need the vertical ladders to overlap my horizontal platform pieces. The player is a KinematicBody2D, the platform is a StaticBody2D, and the ladder is an Area2D. Using a signal in the Ladder script, When Player enters Ladder's area, the Player script allows up/down input and we can climb the ladder.

This is great until I'm going up a ladder and bonk my head on a platform piece that the ladder passes through on the way up.

When I'm not on a ladder, I absolutely do want those kinds of collisions, but when going up or down a ladder, I want to be able to walk freely up and down, past any platforms the ladder may intersect.

So I'm trying to find a way in gdscript to either: - temporarily turn off the collision detection of an external node (CollisionShape2D has a "disabled" property that can be turned on... I can't figure out how to do that from a node in another scene) - temporarily set my Player to ignore anything on the platforms layer (I don't know how to trigger this change in gdscript) - in my Player script, temporarily add a collision exception for the platform, though I've been reading about it, and still can't figure out how to word it in gdscript.

So basically, an explanation of how to do any of the above would be greatly appreciated!

Also as you can see, I don't have a grasp on how to refer to nodes that are children of other scenes. So for example, from inside my Player scene's script (extending the main node of the Player scene, which is a KinematicBody2D), how do I refer to the CollisionShape2D of my Platform scene?

Thanks in advance for any light you may be able to shed on either of these questions!

-Mark

Referring to nodes outside of the children of the scenes themselves are very similar to referring to the child nodes, but you need to get a reference to them. For example, if you have a scene tree like the following:

  • Root
    • Player
      • Sprite
    • Robot

Then from the Player node, you have a few options for getting the various nodes in the tree:

# The sprite is easy, just use a normal get_node or $
print(get_node("Sprite").name)

# To get root, you can use get_parent
print(get_parent().name)
# Alternatively, you can use the following (I think):
print(get_node("../").name)

# To get Robot, if you know it is a child of the same parent, then you can use the following:
print(get_parent().get_node("Robot").name)
# or
print(get_node("../Robot").name)

However, the way I like to access external nodes via scripting is using exported NodePaths, so long as the node's I'm connecting are not being instanced/spawned while the game is playing:

extends Node
export (NodePath) var external_node_path
var external_node

func _ready():
	external_node = get_node(external_node_path)

As then all you have to do is select the node in the editor, and point it to the node you want, and then you can just access external_node like normal. Another way is using groups, which can be helpful if you have multiple nodes that you want to access and/or nodes that are instances/spawned rather than placed in the scene.

Hopefully that helps a bit explain how it works. If not, let me know and I'll whip up a quick example project.

4 days later
2 years later