Weird function behaviour on message

Hi guys! I am totally new here, so my problem is probably less than interesting, but I couldn’t find the answer.
I want to use a GUI button to control my Player via messages.
for that I have tried to make a function and it works but not as wanted.

"local function move_left(self)

  • self.input.x=-1 print(“KFDSK”)*
    end

function on_message(self, message_id, message, sender)

  • if message_id==hash(“left”) then move_left(self)*
  • end*
    end

function on_input(self, action_id, action)

  • if action_id == hash(“up”) then*
  •   self.input.y = 1*
    
  • elseif action_id == hash(“down”) then*
  •   self.input.y = -1*
    
  • elseif action_id == hash(“left”) then*
  •   --self.input.x = -1*
    
  •   move_left(self)..."*
    

now, when I press on the keyboard, the “move_left” function works fine, it’s moving the player and prints “KFDSK” as my debug message. But when I press the GUI button, it only prints the debug text but the player character does not move. So it works, but not really works…

Could you help me on what I am missing?

Thanks in advance.
Gabe

Hi @gabetot, and welcome!

A small tip, paste the code as-is between three apostrophes like this (`) on each side of the code block to format it:

```
paste your code here
```

This will make the code look readable, and it will be easier for us to help you.

1 Like

Hi! Thanks for the tip!

just for sure, once again
I want to use a GUI button to control my Player via messages.
for that I have tried to make a function and it works but not as wanted.

local function move_left(self)
	self.input.x=-1 
	print("KFDSK")
end

function on_message(self, message_id, message, sender)
	if message_id==hash("left") then 
		move_left(self)
	end
end



function on_input(self, action_id, action)
	if action_id == hash("up") then
		self.input.y = 1
	elseif action_id == hash("down") then
		self.input.y = -1
	elseif action_id == hash("left") then
		move_left(self)

now, when I press on the keyboard, the “move_left” function works fine, it’s moving the player and prints “KFDSK” as my debug message. But when I press the GUI button, it only prints the debug text but the player character does not move. So it works, but not really works…

The ` symbol is under the escape button on keyboard. Don’t confuse with the ’ symbol near the enter key.

And either you copy the text from Word or similar app or you have “smart quotes” enabled on your system that invert first quote making it illegal for program code.

1 Like

Ok, the code was a a little easier to read, but it seems to be missing the end of the code.
E.g. the on_inputfunction wasn’t complete.

If move_left()is called, and the print is coming out, then the self.input.x=-1 must be set, right?
So, the question is, is the self.input reset somewhere in the script?

1 Like

Well, the best it seemed to me to upload it.
A short video
and a picture

Thank you for the help.

Well, funny thing is, I have a qwertz keyboard :smiley:
But I think I see it now.

just for test

local function move_left(self)
	self.input.x=-1 
	print("KFDSK")
end

function on_message(self, message_id, message, sender)
	if message_id==hash("left") then 
		move_left(self)
	end
end



function on_input(self, action_id, action)
	if action_id == hash("up") then
		self.input.y = 1
	elseif action_id == hash("down") then
		self.input.y = -1
	elseif action_id == hash("left") then
		move_left(self)
	elseif action_id == hash("right") then
		self.input.x = 1
	elseif action_id == hash("fire1") then
		if self.fire_cooldown1 > 0 then
			return
		end
		msg.post("#fire1", "play_sound")
		local offset = vmath.rotate(self.direction, vmath.vector3(150, 0, 0))
		factory.create("#fire1Factory", go.get_position() + offset)
		self.fire_cooldown1 = 0.12
	end
	if vmath.length(self.input) > 0 then
		self.moving = true
		self.dir = vmath.normalize(self.input)
	end
end
1 Like

You can also press this button while having the text marked:

Another solution is to start each line with four spaces or a tab.

1 Like

Let’s get back to your problem. So you’re saying that when moving using the keyboard it works but not while pressing a GUI button (which I assume is sending a message)?

You have not shown your update() function yet and I’m guessing there is something in that function which is causing a problem.

1 Like

Thank! For now, I see it would better to copy the rest of it as well, maybe it is useful.

function init(self)
	msg.post(".", "acquire_input_focus")

	self.direction = go.get_rotation()
	self.rot = vmath.quat()
	-- Base rotations half a lap in each direction
	self.rot_left = vmath.quat_rotation_z(3.14159)
	self.rot_right = vmath.quat_rotation_z(-3.14159)

	self.moving = false
	--self.firing1 = false notinuse
	self.fire_cooldown1 = 0
	--self.thrustLeft=false notinuse
	--self.thrustRight=false notinuse
	self.input = vmath.vector3()
	self.dir = vmath.vector3(0,1,0)
	self.speed = 200
end

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

function update(self, dt)
	if self.moving then
		local pos = go.get_position()
		pos = pos + self.dir * self.speed * dt
		go.set_position(pos)
	end
	self.input.x=0
	self.input.y=0
	self.moving=false
	--self.firing1=false notinuse
	self.fire_cooldown1 = self.fire_cooldown1 - dt
end

local function move_left(self)
	self.input.x=-1 
	print("KFDSK")
end


function on_message(self, message_id, message, sender)
	if message_id==hash("left") then 
		move_left(self)
	end
end


function on_input(self, action_id, action)
	if action_id == hash("up") then
		self.input.y = 1
	elseif action_id == hash("down") then
		self.input.y = -1
	elseif action_id == hash("left") then
		move_left(self)
	elseif action_id == hash("right") then
		self.input.x = 1
	elseif action_id == hash("fire1") then
		if self.fire_cooldown1 > 0 then
			return
		end
		msg.post("#fire1", "play_sound")
		local offset = vmath.rotate(self.direction, vmath.vector3(150, 0, 0))
		factory.create("#fire1Factory", go.get_position() + offset)
		self.fire_cooldown1 = 0.12
	end
	if vmath.length(self.input) > 0 then
		self.moving = true
		self.dir = vmath.normalize(self.input)
	end
end

Looks like self.moving = true is missing on_message

2 Likes

By doing this, the character starts moving, but to forward by default. If I press any arrow keys, it starts moving into that direction. So if I press Right, it starts moving right by the Gui button, if I press Down, it moves down.

The same happens if I write it into the function itself.

But I guess it’s getting closer.

Because your logic got problems. You are assigning direction on_input with self.dir = vmath.normalize(self.input) but not in on_message. You can find lots of examples about movement on Tutorials

on_input and on_message are totally different things.

3 Likes

Thank you! By generally revamping some lines, it finally reacts as wanted.
As for the tutorials, thank you. I have already finished them, before I have even started anything, but I just couldn’t apply everything as intended for the first time.
But I will continue to learn and I am really grateful for all of you guys for this help you showed.
Thank you

3 Likes