Strange objects Interactions

hey, guys , sorry for disturbing you constantly, but i created 2 kinematic objects and I copied the content of the scripts of the objects and made some subtle changes with regard to URLs etc. The problem is : when I move an object, the other drops a few pixels until it loses contact with the ground and falls. Additionally, the masks of the respective objects are not linked, so that i cant understand what’s is their bond. For example: if a move character X, character Y’s position decreases a little amount of pixels and vice versa. Anyone knows the reason ???
being more precise: this happens after releasing the action button

there’s the code:

go.property("speed", 200) -- the speed of movement
local jump_height = 300

-- play animation unless the same animation is already playing
local function play_animation(self, animation)
	if self.current_animation ~= animation then
		self.current_animation = animation
		msg.post("#sprite", "play_animation", { id = animation })
	end
end

function on_message(self, message_id, message, sender)
	-- Handle collision
	if message_id == hash("contact_point_response") then
		print("Contact!")

	end

end


function init(self)
	msg.post(".", "acquire_input_focus")
	self.direction = vmath.vector3(0, 0, 0) -- the current direction of movement
	self.actions = {} -- mapping of input states (action_id mapped to pressed state)
	self.current_animation = nil -- the current animation
	self.speed = 0 
	self.fall = true
	msg.post("@system:", "toggle_physics_debug")

end

function final(self)
	msg.post(".", "release_input_focus")
end

function update(self, dt)
	
	if self.fall then 
		self.p = go.get_position()
		self.p.y = self.p.y - 500 * dt
		go.set_position(self.p)
	end

	if self.actions[hash("up")] then
		self.count = 0 
		if self.fall == false then 

			play_animation(self, hash("jump"))
			self.posicao = go.get_position()

			self.posicao.y = self.posicao.y + (self.speed * 6) * dt

			if self.posicao.y <= 600 then 
			
				go.set_position(self.posicao)
				
			else 

				self.fall = true 

			end

	
		else
			self.fall = true 
		end

		
			
  
	end

	if self.actions[hash("left")] then
		play_animation(self, hash("run"))
		sprite.set_hflip("#sprite", true)
		

		self.direction.x = -self.speed
	elseif self.actions[hash("right")] then
		play_animation(self, hash("run"))
		sprite.set_hflip("#sprite", false)
		self.direction.x = self.speed
	

	else
		self.direction.x = 0
	end


	if self.direction.x == 0 and self.direction.y == 0 then
		play_animation(self, hash("idle"))
	end

	go.set_position(go.get_position() + self.direction * dt)


	

	msg.post("@render:", "draw_text", { text = "Move using arrow keys", position = vmath.vector3(10, 100, 0) } )

	self.atual = go.get_position()

	print(self.atual)


end



function on_input(self, action_id, action)
	if action_id then
		if action.pressed then
			self.actions[action_id] = true
		elseif action.released then
			
			self.actions[action_id] = false

			self.fall = true 
		end
	end

	
	
end



function on_message(self, message_id, message, sender)
	-- Handle collision 
	if message_id == hash("contact_point_response") then
		
		self.fall = false 
	
	end

end

this is the first character’s code, now see the player 2’s code :

go.property("speed", 200) -- the speed of movement



-- play animation unless the same animation is already playing
local function play_animation(self, animation)
	if self.animacao_atual ~= animation then
		self.animacao_atual = animation
		msg.post("#sprite2", "play_animation", { id = animation })
	end
end



function on_message(self, message_id, message, sender)
	-- Handle collision
	if message_id == hash("contact_point_response") then


	end

end


function init(self)
	msg.post(".", "acquire_input_focus")
	self.direcao = vmath.vector3(0, 0, 0) -- the current direcao of movement
	self.acoes = {} -- mapping of input states (action_id mapped to pressed state)
	self.animacao_atual = nil -- the current animation
	self.velocidade = 0 
	self.caiu = true
	msg.post("@system:", "toggle_physics_debug")


end

function final(self)
	msg.post(".", "release_input_focus")
end

function update(self, dt)

	if self.caiu then 
		self.p1 = go.get_position()
		self.p1.y = self.p1.y - 500 * dt
		go.set_position(self.p1)
	end

	if self.acoes[hash("player2_up")] then
		self.count = 0 
		if self.caiu == false then 

			play_animation(self, hash("jump"))
			self.posicao1 = go.get_position()

			self.posicao1.y = self.posicao1.y + (self.speed * 6) * dt

			if self.posicao1.y <= 600 then 

				go.set_position(self.posicao1)

			else 

				self.caiu = true 

			end


		else
			self.caiu = true 
		end




	end

	if self.acoes[hash("player2_left")] then
		play_animation(self, hash("run"))
		sprite.set_hflip("#sprite2", true)


		self.direcao.x = -self.speed
	elseif self.acoes[hash("player2_right")] then
		play_animation(self, hash("run"))
		sprite.set_hflip("#sprite2", false)
		self.direcao.x = self.speed


	else
		self.direcao.x = 0
	end


	if self.direcao.x == 0 and self.direcao.y == 0 then
		play_animation(self, hash("idle"))
	end

	go.set_position(go.get_position() + self.direcao * dt)




	msg.post("@render:", "draw_text", { text = "Move using arrow keys", position = vmath.vector3(10, 100, 0) } )

	self.atual = go.get_position()

	print(self.atual)


end



function on_input(self, action_id, action)
	if action_id then
		if action.pressed then
			self.acoes[action_id] = true
		elseif action.released then

			self.acoes[action_id] = false

			self.caiu = true 
		end
	end



end



function on_message(self, message_id, message, sender)
	-- Handle collision 
	if message_id == hash("contact_point_response") then

		self.caiu = false 

	end

end


Note: the variables means the same thing, but they are in portuguese (my first language).

You need to supply more information. Please post screenshots of your setup and the script code.

1 Like

Could it be that you are using a script scoped variable or two?

-- some.script
local foobar = "this value is shared between all instances of this script"

function init(self)
    foobar = "Yeah! Now all instances will read this value instead"
end

Or perhaps you are using a global function or two that for some reason shadows/replaces other functionality.

You need to share some code and perhaps also some screenshots of your setup to know for sure.

If you want to post your code here, that will help us find the problem. One potential source could be that one of the collision objects’ physics is set to ‘dynamic’, causing it to fall very slowly (only a few pixels) the first few seconds and then accelerate, which could be misinterpreted as being caused by your input, if you input immediately as the game starts.

2 Likes

as you can see, both objects (players, of course) are set to kinematic

You have not attached the script ‘player.script’ anywhere, but I suppose that might be intentional.

Could you post the contents of ‘testar.script’ and ‘player2.script’?

1 Like

player2.script (2.4 KB)
testar.script (2.5 KB)

im sorry

There are multiple on_message() functions in both of these scripts.

1 Like

is this the cause?

No idea, but you should have only one per script component.

1 Like

ill fix this bug. If the problem persists, i’m gonna report

It’s not gonna fix the problem…

What happens is that both scripts get all input and you set a flag on key release in both scripts, no matter which key:

function on_input(self, action_id, action)
	if action_id then
		if action.pressed then
			self.acoes[action_id] = true
		elseif action.released then
			self.acoes[action_id] = false
			self.caiu = true                       -- <--- THIS! 
		end
	end
end

and then you have code in update that is controlled by that flag:

function update(self, dt)
	if self.caiu then 
		self.p1 = go.get_position()
		self.p1.y = self.p1.y - 500 * dt
		go.set_position(self.p1)
	end

	...
end

So releasing keys, any keys, will affect both game objects.

1 Like

i see. So, what shall i do? Would be a solution if the action.released was inside the update function? or create a new input blinding?

You can’t access action.released in the update function. There are several options. Here are two:

  1. Discard action.released for keys that does not belong to the player. Each player script can keep a list of valid keys and check against that list, for instance.
  2. Gather all input in a separate game object w script that sends messages to each player to “move_left”, “move_right” etc.
1 Like

I would solve the problem using this solution. You can use the same script for both players and use script properties and a mapping between say player id and a set of valid keys.

2 Likes