|
|
|
|
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.
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.
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.
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.
Post your code i will try to check it.
What exactly you want to do NOW?
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.
Sorry wanted ust to underline somehow the’now’ moment.
Back to your problem:
- Connect signal to function
- Do ‘print’ in that function
I will post the code soon.
func _ready():
connect("body_enter",self,"do_the_printing")
func do_the_printing():
print("collision hapened")
Hope it helps you.
Okay, all is forgiven
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! =]
If you can then post a zip of your project i’ll check it and repost it fixed and then you will see.
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!
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
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!
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.
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!