How do I create an if statement that does something if the player collides with a specific item from another scene?

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

I am new to GDscript and Godot and coding in general. I put the player’s scene and the environment into one main scene. I have a few questions:

  1. How do I reference an object(CSGbox) from another scene into a player movement script linked to the player (kinematic body) in another scene?
  2. What code do I use to detect if my player collides with the object referred to in question 1 from the other scene.
    I am planning to make a bounce pad/trampoline kind of thing. My code will be something like this: if the player is in contact with the bounce pad, it will change the variable jump speed to 1000 and automatically jump into the air, acting as a launch pad. And immediately change the jump speed back to the original value after jumping.

I don’t quite understand but I think you are talking about collision
did you use “Area3d”

ramazan | 2022-03-08 09:30

You can make global script for your player or you can adress the node which you want to use like:
onready var Bounce-pad-variables = get_node(“/root/Level1/bouncce_pad”) or
onready var Player-variables = get_parent.get_child.get_node(“player”)

For detecting collision you can use signals from Area2D(On the right side of the inspector press “node” and you can pick a signal ).
For specific items you can either use groups
func _on_bouncearea_area_entered(area)
if area.is_in_group(“push”):
if area ==“player”

horsecar123 | 2022-03-08 16:03

No, I didn’t use an area node the kinematic body along with the mesh instance, collision shape and the camera, etc. does fine. Is that needed to do this?

stevenotfound001 | 2022-03-09 00:16

:bust_in_silhouette: Reply From: w411-3

Long story short, you probably want signals: Using signals — Godot Engine (stable) documentation in English

First of all, for the Player and Box to be available to each other, they will both be under some mutual parent node anyway, so they are in the SceneTree when you play. One way or another, it’s clear they can probably find each other.

Anyway, add your CSGBox to the scene. Then add an Area node on top of that. Then add a CollisionShape on that Area node, and set it’s shape to “box” in the inspector; it should just fit the box immediately. Now click on the Area node, and find the “node” tab, and you should see all the signals it can do. Choose one like “area entered” and choose your player in the dialog that comes up to connect it to. Your player script will automatically fill with the function that will be called when something collides with that CSGBox.

To make sure it only works with the player, one way is to add a class_name Player or something to the top of your player script, and in the area_entered callback, make sure you check if body is Player before you do anything, or check the node’s name, etc.