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.
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.
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.
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?
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
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.