In my game I’m trying to implement a powerup that spawns everytime someone scores a goal, currently the powerup spawns after a goal is scored but I’m struggling with the code I would do to make my players be able to collide with this powerup, I have collision objects on the players and on the powerup just struggling on how I would actually code them to collide because I’ve tried many things I would have thought might work but I think it has something to do with message sending.
Can anyone help me with this?
Thanks.
share some of your code, cant undestand what is the problem just by ur text.
But in the end, you want to just check in the message passing if your player collided with the power up, it should be pretty straightforward
This is collision I used for my ball and my walls, I tried something similar for the player and the powerup and nothing happened.
That’s nearly unreadable! You need to paste the actual code, like this:
Use three of this symbol ` on either side of the code, and it will be formatted like this:
local abc = true
My mistake!!
‘’’ function on message(self, message_id, message, sender)
If message_id == COLLISION_RESPONSE then
Local other_id = message.other_id
If self.previous_other_id == other_id then
Return
End
If message.other_group == HORIZONTAL_WALL then
Self.direction.y = self.direction.y * -1
Elseif message.other_group == VERTICAL_WALL then
Self.direction.x = self.direction.x * -1
End
Self.previous_other_id = other_id ‘’’
No worries. Unfortunately we’re still not quite there yet.
‘’’ is what you’ve used around your code - that’s not the same as ```.
Secondly, your code does not match what was in the screenshot. Are you unable to access the forums on the computer you are developing on? I assume this is the case, because you posted a picture of the screen (rather than a screenshot or a text copy of the code) and the code you have now posted is capitalised (in a way that a phone would do). This is an issue because case (UPPERCASE or lowercase) is relevant and can cause issues.
Finally, we are missing context because you haven’t posted all the code. There are variables, such as COLLISION_RESPONSE, that we don’t see the definition of. For all we know, the issue might be that you misspelled something there! We need to see all of the relevant code.
I will respond soon on my computer just cannot get on the forums on my computer at the moment!
Here’s a power-up (coin magnet) example:
local COLLISION_RESPONSE = hash("collision_response")
local HORIZONTAL_WALL = hash("horizontal_wall")
local VERTICAL_WALL = hash("vertical_wall")
function on_message(self, message_id, message, sender)
if message_id == COLLISION_RESPONSE then
local other_id = message.other_id
if self.previous_other_id == other_id then
return
end
if message.other_group == HORIZONTAL_WALL then
self.direction.y = self.direction.y * -1
elseif message.other_group == VERTICAL_WALL then
self.direction.x = self.direction.x * -1
end
self.previous_other_id = other_id
end
end
This is the code I have that collides the ball with walls and my players. The walls consisting of horizontal and vertical walls and my players also consisting of two horizontal and vertical walls on each side so that it gives it better collision. In terms of my power-up I just want it to be able to collide with my player in the player script (this code is taken from the ball script). I pretty much want it so that when a player touches it, it will disappear and then a variable like the speed of the player movement or speed of the ball increases as a result and when a new powerup spawns after another goal is scored the variable resets and then is activated again if it is collided with.
I tried doing something similar to my code above in the player script but the collision didn’t register and nothing happened.
Hopefully this is better for you guys this time
Also the coin magnet tutorial was helpful but I still don’t get the message collision between the two.
Maybe I missed it (on mobile now), but have you checked that your collision objects have their groups and masks configured in a way that they can collide?
The collision objects are all grouped correctly yes.
What my main problem here is what code would I use to actually make these objects collide, I just don’t understand who is messaging who and what is colliding with what in the code I displayed above.
Both the player and the powerup will each receive a message.
You can use this to find the message_id in either script:
print(message_id)
And this to find out what’s in the message:
pprint(message)
If you’re not getting these in either script, then there is something else wrong (e.g. in your groupings, or a collision object is not added, or something like that).
Collision messages will be sent to both of the involved game objects. The message will tell you the id of the other object (message.other_id
) and the group of the other object (message.other_group
).
Is there any way to message specific game objects instead of just ‘message.other_id’ as for it to be less confusing?
The game object with the collision component will receive the message. That is the least confusing way to set things up! Can you try to explain how you think it works, because I’m not sure what you are referring to with your question.
Here’s the manual:
Thanks for this!