Trying to get message passing to only trigger once

i’ve already made a post about this once, but the thread died so i’m gonna continue it. A message is passed to the slime script when the bullet collides with the slime. since the bullet continues to collide with the slime for an extended period of time, the result of the message passing, taking one from a health variable, triggers multiple times. i’ve tried implementing a time to live, but that results in the halting problem. a timer just delays the trigger.

bullet script

if message_id == hash("contact_point_response") then
		go.delete()
	end
	
	if message_id == hash("sdeath") then
		msg.post("slime#slime", "bk")
	go.delete()

	end

slime script


				if message.group == hash("bullet") then
					if not hash("bk") then
						
						msg.post("player#bullet", "sdeath")
					end
						local damaged = true
						--tiemr = 1
						pprint("bullet collision")
						if damaged == true then
							
							pprint("health decreased")
							self.health = self.health - dmg1
							damaged = false
						end
						pprint("dmg is ", dmg1)
						pprint("health is ",self.health)
					end
				end

What is this supposed to test? I think you’ve gotten a bit confused here, because you are just evaluating the hash, which will always return true (or in this instance, false, since you’ve placed a not before). In Lua, any value that isn’t false or nil is by definition true. See this and this.

local damaged = true
--tiemr = 1
pprint("bullet collision")
if damaged == true then

This part is redundant as well. You create the local variable damaged and set it to true, then immediately check if it’s true. It always will be, so it won’t have any effect.

2 Likes

it is created as true when the bullet hash is received, and then the if statement is to allow it to pass straight over. then it is set to false so that the true if statement doesn’t happen again. this is the line that i was talking about being an infinite loop for it to work. as to reset damaged to true you would need another true false statement to reset it and so on. i’m looking for a alternative to this.

No, I think you’re a bit confused about some parts. If the script you describe were to be run in a loop, the damage variable would still always be true.

Try:

for i=1,10 do   
    local damaged = true
    if damaged == true then
        print("ping")
        damaged = false
    end
end

I guarantee you’ll get the ping output 10 times, not 1.

Anyway. How about you create a minimal version of this project where the bullet hits the slime, then zip it up and upload it here.

2 Likes

I’ll do that in the morning then, currently 9 pm here and need to get ready for bed

1 Like

Hello there,

I don’t know your project, but I think this is a use case for a trigger response: Trigger
So, in your case: the slime is the trigger. When the bullet enters, you decrease the health.

1 Like

i’ll try messing with this first then.

would this work? i’ve tried having the trigger and wall collision as two separate objects, but it doesn’t seem to be working. it’s not receiving the trigger response at all. the trigger hitbox is bigger than the wall collision one. i’m still not fully sure how triggers work. do i need the script in slime? or bullet?
image

here’s how i restructured the code.

if message_id == hash("trigger_response") then
					pprint("trigger colision")
					if not hash("bk") then
						
						msg.post("player#bullet", "sdeath")
					end
						---local damaged = true
						--tiemr = 1
						pprint("bullet collision")
						--if damaged == true then
							
						pprint("health decreased")
						if message.enter then
							self.health = self.health - dmg1
						end
							
							--damaged = false
						--end
						pprint("dmg is ", dmg1)
						pprint("health is ",self.health)
					end
				end

One thing, you still have this line which doesn’t do anything:

if not hash("bk") then

This is the same as:

if not true then
1 Like

Hello there,

this is hard to tell without knowing your setup better. Would you mind uploading a project like @Alex_8BitSkull has already suggested?

full or just the killing slime bit?

I guess full would be best.

keep in mind that some aspects will seem stupid from me forgetting to change them. (e.g. spelling errors)
game test.zip (2.1 MB)

i would appreciate advice on how to fix rather than restructuring and uploading because it would be more useful to me if i understood how to fix them.

Hi,

am looking at the project now and need some time to understand what’s going on.

a lot is broken, as i’m slowly trying to fix each bug

oh yeah also forgot to comment code, so that’s not very helpful sorry

I changed the slime’s collision type to be kinematic and made the collision code like this:

if message_id == hash("contact_point_response") and message.other_group == hash("bullet") then
		msg.post(message.other_id, "sdeath")
		self.health = self.health - dmg1
		print("dmg is ", dmg1)
		print("health is ",self.health)
	end
end

That works.

The issues were:

  • You didn’t check for a specific type of collision (bullet), so hitting a wall would also kill the slime.
  • You hardcoded the kill bullet message to player#bullet. Use message.other_id instead.
  • As I mentioned previously, the if statement checking hash(“bk”) was not designed properly.

Your code is very messy! The indentation is all over the place and that makes it hard to read. There is a LOT of code that is partly commented out. I encourage you to delete things that aren’t working, it makes it easier to keep track of everything. I’m not saying this to be hard on you, I’m saying this because as an experienced Defolder I still found it hard to read your code, so for you as a beginner it will be much tougher. If you get properly stuck, take a few steps backwards and start over. Layering attempted fixes on top of each other without cleaning up in between will just make the situation hopeless.

5 Likes

right. i’ll work through these points then and clean up. thanks. need to properly read up on message.other_id as well.

1 Like

You’re welcome.

Check this out, it documents every part of the contact point response message:

other_id is simply the id of the object that is causing the collision. So you could even do this rather than sending a message to the bullet script:

go.delete(message.other_id)
1 Like