Distance tracker and teleportation(SOLVED)

Ok so I made a distance tracker and now I want teleportation to add some meters to it
the thing is I used elseif(yes ik it doesnt to work but I still tried) and I have no idea for another way to do it, I will also like ideas for improvement and stuff
Thanks in advance

	elseif message_id == hash("teleport") then
		gui.set_text(gui.get_node("distance"), self.distance + 10)

elseif message_id == hash("distance") then
		gui.set_text(gui.get_node("distance"), math.floor(message.distance))
		self.distance = math.floor(message.distance)
	end

I am not sure what you are trying to make here. Can you explain in more detail?

Ok I will try, so what Im trying to do is when the player teleports it will add 10 more meters to the distance tracker
here simplified example:
normal running = 1m every seconds(or whatever amount)
teleportation = adds 10m to the current distance score

here is the related code:

hud.script:

elseif message_id == hash("teleport") then
		gui.set_text(gui.get_node("distance"), self.distance + 10)

elseif message_id == hash("distance") then
		gui.set_text(gui.get_node("distance"), math.floor(message.distance))
		self.distance = math.floor(message.distance)

controller.script

    function distance(self)
    	self.currentime = socket.gettime()
    	self.distance = (self.currentime - self.startime) * self.speed
    	msg.post("hud", "distance", {distance = self.distance})
    end

this function running on update function on the controller.script, and the player script(the one that executes the teleportation only sends msg.post(“hud”, “teleport”) to the hud.script

should I try to explain better?

I think it’s better of you share the entire script and an explanation of what does not work. I can’t figure out what’s wrong based on the snippets of code you shared.

Could it perhaps be that you think that the variables in self are shared between scripts? The self.distance in controller.script is not the same as in hud.script.

Alright

Controller.script

go.property("speed", 4)

local grid = 460
local platform_heights = { 100, 200, 350 }
local coins = 3

function init(self)
	msg.post("ground/controller#script", "set_speed", { speed = self.speed })
	self.gridw = 0
	self.spawns = {}
	self.spawn_ids = {}
	self.score = 0
	self.phase = false
	self.startime = socket.gettime()
end

function update(self, dt)
	self.gridw = self.gridw + self.speed
		
	if self.gridw >= grid then
		self.gridw = 0

		if math.random() > 0.2 then
			local h = platform_heights[math.random(#platform_heights)]
			local f = "#platform_factory"
			local coins = coins
			if math.random() > 0.5 then
				f = "#platform_long_factory"
				coins = coins * 2
			end
			
			local p = factory.create(f, vmath.vector3(1600, h, 0), nil, {}, 0.6)
			table.insert(self.spawn_ids, p)
			msg.post(p, "set_speed", { speed = self.speed })
			msg.post(p, "create_coins", { coins = coins })
			table.insert(self.spawns, p)
		end
	end
	phase(self)
	distance(self)
end

function phase(self)
	if self.phase == true then
		for i, p in pairs(self.spawn_ids) do
			msg.post(msg.url(nil, p, "danger_edges"), "disable")
			msg.post(msg.url(nil, p, "collisionobject"), "disable")
		end
		timer.delay(4, false, function(self, id)
			self.phase = false
		end)
	end
end

function distance(self)
	self.currentime = socket.gettime()
	self.distance = (self.currentime - self.startime) * self.speed
	msg.post("hud", "distance", {distance = self.distance})
end

function on_message(self, message_id, message, sender)
	if message_id == hash("reset") then
	    msg.post("hero#script", "reset")
	    -- Delete all platforms
		for i,p in ipairs(self.spawns) do
			go.delete(p)
    	end
		self.spawns = {}
    elseif message_id == hash("delete_spawn") then
		for i,p in ipairs(self.spawns) do
			if p == message.id then
				table.remove(self.spawns, i)
				go.delete(p)
			end
    	end    	
    elseif message_id == hash("score_coin") then
		self.score = self.score + 100
		msg.post("hud", "set_score", { score = self.score })
    end
end

hud.script

function init(self)
	self.hits = 3
	self.distance = 0
end

function on_message(self, message_id, message, sender)
	if message_id == hash("set_score") then
		local s = gui.get_node("score")
		gui.set_text(s, message.score)

	elseif message_id == hash("teleport") then
		gui.set_text(gui.get_node("distance"), self.distance + 10)

     elseif message_id == hash("distance") then
		gui.set_text(gui.get_node("distance"), math.floor(message.distance))
		self.distance = math.floor(message.distance)
	end
end

player.script

local function teleport(self)
	if self.action == true then
		self.position = vmath.vector3(500, self.position.y, self.position.z)
		go.set_position(self.position)
		self.action = false
		msg.post("hud", "teleport")
		timer.delay(4, false, function(self, id)
			go.animate(".", "position.x", go.PLAYBACK_ONCE_FORWARD, 298.735, go.EASING_LINEAR, 1)
		end)
	end
end

function init(self)
	msg.post(".", "acquire_input_focus")
	self.position = go.get_position()
	local orgp = go.get_position()
	msg.post("#", "reset")
	self.action_count = {}
	self.startime = socket.gettime()
end

function update(self, dt)
     teleport(self)
end

What am I trying to do?
Add to the distance tracker 10 more meters when teleported as shown in hud.script, the message that is sent to the hud is via the player.script

What doesn’t work(sorry I forgot to mentioned that my mistake)?
It doesn’t execute the line that adds the 10 more meters to the distance tracker(probably because its in an elseif)

In player.script:

print() something just before this line or set a breakpoint. Is the line of code called or not?

Next in hud.script:

print(message_id) immediately after this line or set a breakpoint. Is the “teleport” message received?

did that and it did get the teleport message

Ok, and you’re saying that your elseif is ignored?

Did you use the debugger and step through the on_message function? Double check everything. It doesn’t matter that it is an elseif.

just checked, it doesnt ignore the elseif only the

gui.set_text(gui.get_node("distance"), self.distance + 10)

Then you must be getting an error in the console?

No error no nothing, I also checked if the self.distance value is matched to the score value and it does

And the value of the label? It should be 10.

The value of the label(the text) should be the meters before the teleportation + 10 meters
And it doesn’t even change the text

Also I’m off my computer for today

Do you ever send this message from somewhere? Does distance actually count up on the label?

Never sent distance from somewhere else and you can see the counting on the label

A few things…

hud.script

The line gui.set_text(gui.get_node("distance"), self.distance + 10) only changes the state of the label.
The distance itself (self.distance) doesn’t change.

So, if you send teleport it will briefly show self.distance + 10, but in the next update() in controller.script, you’ll send distance, which is self.distance, which means you’ve removed the teleporting part.

player.script:

Is this really the whole script?
You send a message to this script, but you have no on_message() function?
When does the self.action = true get set?
Does teleport() really get called? (try a print() in there)

1 Like

Ah, I see now that it is sent from the controller.script. And this makes me confused.

  • You have a self.distance variable in controller.script that counts up and continuously sends the distance to the hud.script
  • You also have a self.distance in hud.script that gets updated every time it receives a “distance” message (sent from controller.script)
  • You send a “teleport” message from the player.script. When it is received you set the distance text to whatever distance you currently have in hud.script plus 10
  • The next frame you will send a new distance message from the controller.script. This value will overwrite the distance+10 you set the previous frame

Solution: Track distance in a single place and update distance via a single message. I would send the teleport message from the player to the controller and from the controller to the hud. Right now you’re updating from two different places and track the distance in two places.

2 Likes

I’ll try everything you said tommorow thank you

The teleport() does get called, it is teleportation