Simple Collision Scripting Question

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By spicy
:warning: Old Version Published before Godot 3 was released.

Hello!

It’s me again. This time I have a question about collisions.

I’ve got my game setup so that when a barrel hits my player it recognises that and bounces (using RigidBody2D and CollisionShape2D/CollisionPolygon2D). My question is this:

How would I set up a simple script so that when the barrel hits the player, it will print a simple message? (I’m hoping this will point me in the right direction so that later on, I will be able to create a sort of function where when the barrel collides with the player, it’ll take away a life point. But baby steps for now!)

I’ve tried looking at some tutrials online but I could never get it to work. Any help would be appreciated, thank you :slight_smile:

:bust_in_silhouette: Reply From: KRL

You should read about connect function. You have to connect “colision enter” signal to function.

Edited on 18.03.16 15:04:

Code have to look like this:

func _ready():
   connect("body_enter",self,"do_the_printing")

func do_the_printing():
   print("collision hapened")

Then enable contact monitor on your rigidbody object and set “contact reported” parameter to 5

Someone on the IRC mentioned the body enter function (which you can do through the signal connection) but I still have not been able to get it working unfortunately.

spicy | 2016-03-16 19:04

http://op.godotengine.org/projects/godot-engine/wiki/Scripting

Skip that site to the part ‘handling a signal’

Personal advise: you shouldn’t skip tutorial.

KRL | 2016-03-16 19:09

I’ve already read it already! I’ve used the signals button to create various connections before, that is not my issue. I’ve done that bit already!

I created a ‘func _on_Player_body_enter( body ):’ using the signals button, inside that function I’ve put in a simple print command. But I’m not entirely sure where to go next. Obviously I need instructions to say something along the lines of (pseudo code):

if player collides with barrel then
   print ("Collision detected!")

But what I’ve tried hasn’t worked so far.

spicy | 2016-03-16 19:23

print -it writes to comand promt of godot (while using Microsoft windows), if you want to write on game screen to check if your script works properly use a label element.

KRL | 2016-03-16 19:25

Writing to the command prompt of Godot is exactly what I want it to do, so that it tells me that a collision has been detected (ie that it works).

Then from there (once I’ve got that part sorted) I can work towards making it so that when the barrel hits the player, a life is taken away from the player. But that isn’t what I’m concerned about at the moment.

spicy | 2016-03-16 19:31

Post your code i will try to check it.

KRL | 2016-03-16 19:38

What exactly you want to do NOW?

KRL | 2016-03-16 19:39

Not to come across as rude but please can you not use capitals, it looks like you’re shouting and quite frankly, it’s a little irritating. Maybe that is not what you intended but it’s certainly how I took it.

Now, back to the matter at hand. I thought I made it pretty clear about what I would like to accomplish. It’s quite simple really.

Player hits Barrel. Or vice versa. When that occurs print a simple message in the command prompt.

I don’t really have any code to provide as I have said before, I’ve looked at some tutorials online, tried to incorporate a few of these into my game. But I could never get it to work so I deleted any previous attempts. Right now all I have is this (which is in my player script along with other stuff like character movement, variables, inputs etc…)

func _on_Player_body_enter( body ):

Player being the name of my RigidBody2D (which inside has a sprite, collision detection, and RayCast2D).

Obviously it’s something very simple, I don’t believe it would be so difficult to get it working. If you could provide any assistance or point me to some other tutorial I may have missed, it would be appreciated. Thank you.

spicy | 2016-03-16 20:13

Sorry wanted ust to underline somehow the’now’ moment.

Back to your problem:

  1. Connect signal to function
  2. Do ‘print’ in that function

I will post the code soon.

KRL | 2016-03-16 20:20

func _ready():
   connect("body_enter",self,"do_the_printing")
  
func do_the_printing():
   print("collision hapened")

Hope it helps you.

KRL | 2016-03-16 20:44

Okay, all is forgiven :slight_smile:

I’ve tried using your code as a starting point but unfortunately I wasn’t able to get it to work. I used the signal connections to create a new body enter function called ‘do_the_printing’ (which I put in my RigidBody2D Player Node, as that is where all my other player related code is), I put the print code in there.

Then I went into the ready function (which was also located in my RigidBody2D Player Node) and put in the same code you had in yours. The program run fine, but it didn’t work. There was an error in the debugger, I’ve linked an image of the result from the debugger below.

Here is the debugger result

So then I thought maybe I need to get_node first. So I then tried that

get_node("../Player/Mario").connect("body_enter",self,"do_the_printing")

But I was given a different error, something about signal ‘body_enter’ being already connected to given method ‘do_the_printing’ in that object.

You wouldn’t happen to know what I’m doing wrong would you? Sorry in advance for being such a pain! =]

spicy | 2016-03-17 13:34

If you can then post a zip of your project i’ll check it and repost it fixed and then you will see.

KRL | 2016-03-17 14:22

Very well.

Here you go: link removed

The level in question where I’m trying to do this is ‘level_one_game.scn’ Just so you know!

spicy | 2016-03-17 15:04

The line numer 41 in “player_script” should look like this:

connect("body_enter",self,"do_the_printing")

Then enable contact monitor on your “Player” object and set “contact reported” parameter to 5

KRL | 2016-03-17 18:35

I had no idea about the contact monitor or contact reported parameters. But thanks to you I finally got it to work. You wouldn’t be able to quickly explain those two bits would you and why you didn’t mention them previously?

Thank you for being so patient and finally help me solve this, I really appreciate it. And sorry again for my little outburst earlier!

spicy | 2016-03-17 21:07

Contact monitor is a array that holds the list of collisions if you set it to 5 for exaple it will hold max 5 collision reports at the same time if your object will collide with six things it will remeber only five and first one will be not listed in contact monitor array.

Contact Reported is a integer that specifies the amout of listed object in contact monitor.

I didn’t knew they were needed to get it working.

You are welcome.

KRL | 2016-03-17 21:13

So it seems everything the Player Node comes into contact with a collision is detected. Now I just need to make it so that it only does that when it collides with the Barrel. Hopefully it shouldn’t be too hard to figure out, thanks again!

spicy | 2016-03-17 21:34