Hello, I want my enemy when my player be on same platform with him
platform is static collision, I want enemy to go on player only if collides
on platform. I done the static collision and took pos of player
but don’t know what message pass to enemy? I think all this is with an if
but don’t know how to do. Thanks
to be more precise how see the collision of platform with player inside enemy. thanks
I read now message passing on manual to understand but yet sth I am missing . Thanks
So when the player game object collides with a platform game object you want the enemy game object to react?
Is there more than one enemy or is it just one?
I want when collides the enemy go to player, the enemy is only one on platform
Thanks
I pass the message in a way with an if and now need move enemy. Thanks
Ok, I see. One solution could be to have a script on the platform that tracks if both the player and enemy are on the platform. If both are on the platform you send a message to the enemy:
-- platform.script
function update(self, dt)
-- clear state every frame
self.enemy = nil
self.player = nil
end
function on_message(self, message_id, message)
if message_id == hash("collision_response") then
if message.other_group == hash("enemy") then
self.enemy = message.other_id
elseif message.other_group == hash("player") then
self.player = message.other_id
end
if self.enemy and self.player then
msg.post(self.enemy, "attack_player", { player = self.player })
end
end
end
I see you do through platform which I didnt thought I was trying only through enemy and player. Thanks
Something else if I take /player position in update of enemy must give to walk at x ???
Britzl I find many things to learn in this site
http://britzl.github.io/publicexamples/
first time I saw don’t know where you mention but I think for a beginner like me is very good to see these things… Thanks a lot that you saw such infos
Sorry to ask again but is a way to pass message in update function? Thanks
Yes, you can call msg.post() from any script function. But keep in mind that message passing isn’t the same as calling a function. It is going to be slower.
I want to move the enemy sofoklis on my player when goes on platform which is static
The enemy receive the message but don’t know how receive it in update and how exactly move the enemy towards the player only left or right depend where is player on platform.
go.property("coin", 1500)
--go.property("speed", 50)
function init(self)
timer.delay(5, false, build_sofoklis_R)
self.velocity = vmath.vector3()
end
function build_sofoklis_R(self, handle, time_elapsed)
my_coin = coin
--my_speed = speed
factory.create("#sofoklis_R",nil,nil,{ coin = my_coin },0.7)
-- how I put second property?
end
function final(self)
-- Add finalization code here
-- Remove this function if not needed
end
function update(self, dt)
local pos = go.get_position()
local playerPos = go.get_position("/player")
if message_id == hash("run") then
if pos.x < playerPos.x then
self.velocity.x = 50
msg.post(go.get_id(), "play_animation", {id = hash("sofoklis_right")})
else self.velocity.x = -50
msg.post(go.get_id(), "play_animation", {id = hash("sofoklis_left")})
end
go.set_position(go.get_position() + self.velocity * dt)
end
end
function on_message(self, message_id, message, sender)
if message_id == hash("plat_mykinai") then
print("I receive")
msg.post(".", "run")
end
end
function on_input(self, action_id, action)
-- Add input-handling code here
-- Remove this function if not needed
end
function on_reload(self)
-- Add reload-handling code here
-- Remove this function if not needed
end
You can use self
to store information. The self
variable is passed to every lifecycle function (init, final, update, on_message, on_input and on_reload). So if you get something in on_message() and you need it also in update() then save it in self
. This is explained here:
function init(self)
self.foobar = "Nothing!"
end
function update(self, dt)
-- this will initially print "Nothing!" but when you receive a
-- message "something" it will instead start printing "Got something!"
print(self.foobar)
end
function on_message(self, message_id, message)
if message_id == hash("something") then
self.foobar = "Got something!"
end
end
Ok I will read on this you say and hope I fix it… sth else in code I have does will move the player or I wrote mambo jambo and does not work at all… and only the message is problem? thanks
britzl thanks I read what you send I try from start to do it, I saw that maybe does not need update function or maybe it needs a certain way. First I ll try use the self correctly. Sorry to ask all the time but I am a beginner. thanks
I change code and works but not move so I made new thread this you can close . Thanks