Page 1 of 1

Displaying (or Drawing) Coordinates

Posted: Thu Dec 14, 2023 4:40 pm
by Benana
Hello, I am new to LOVE's Lua scripting. I do have some knowledge of scripting with C#, and to be honest, translating that knowledge to Lua is not that hard. But, I am having some trouble trying to display, or draw, x and y coordinates for my player's position or however big the window size is. I attempted to make a new variable and call that variable to a draw line, but that obviously didn't work. Could I get some help, please? Thank you!
Here is the code for reference, this is also the only code I have scripted in my learning experience (Be warned - very, very messy):

function love.load() --practically "void Start()"
player = { --table that sets the players values/variable
x = 0,
y= 0,
speed = 75,
}

cords = {
x = 100,
y = 100,
coordinates
}
end

function love.update(dt) --practically "void Update(dt = deltaTime)"
--player movement
if love.keyboard.isDown("d") then
player.x = player.x + 3
end
if love.keyboard.isDown("lshift") and love.keyboard.isDown("d") then
player.x = player.x + 4
end
-----------------------------------------------------------------------------------------
if love.keyboard.isDown("a") then
player.x = player.x - 3
end
if love.keyboard.isDown("lshift") and love.keyboard.isDown("a") then
player.x = player.x - 4
end
-----------------------------------------------------------------------------------------
if love.keyboard.isDown("w") then
player.y = player.y - 3
end
if love.keyboard.isDown("lshift") and love.keyboard.isDown("w") then
player.y = player.y - 4
end
-----------------------------------------------------------------------------------------
if love.keyboard.isDown("s") then
player.y = player.y + 3
end
if love.keyboard.isDown("lshift") and love.keyboard.isDown("s") then
player.y = player.y + 4
end
-----------------------------------------------------------------------------------------
if player.x < 0 then
player.x = 0
end
end


function love.draw() --practically the "unity editor"
love.graphics.rectangle("fill", player.x, player.y, 50, 50)

love.graphics.newText(cords.coordinates)
end

Re: Displaying (or Drawing) Coordinates

Posted: Thu Dec 14, 2023 7:49 pm
by darkfrei
cords.coordinates not defined;
Also

Code: Select all

local k = shiftPressed and 2 or 1
player.x = player.x + dx*k*player.speed
player.y = player.y + dy*k*player.speed

Re: Displaying (or Drawing) Coordinates

Posted: Thu Dec 14, 2023 7:54 pm
by dusoft
Just to clarify: you need to use delta time to match CPU cycles of your computer. Otherwise you would be just easily overflowing both the coordinates (i.e. maxing out in a matter of microseconds). Using delta time takes care of different CPU speeds of different computers, so your game will behave (speedwise) in a similar fashion everywhere.

To sum up: your naive approach of just adding pixels (without multiplying by delta time) wouldn't work in LÖVE framework well.

See:
https://love2d.org/wiki/dt

Re: Displaying (or Drawing) Coordinates

Posted: Fri Dec 15, 2023 12:04 pm
by Bobble68
I also don't think newText is what you want to use here - I'm guessing you want

Code: Select all

--This is just for readability
local x = cords.x
local y = cords.y

love.graphics.print(x..", "..y, 0, 0)
Love (and Lua, specifically tostring()) will print out tables as ids, rather than printing each value, so you need to tell it how to reach the coordinate values yourself.

newText is used for more complex string displaying - it allows a single string to have multiple colours and fonts within it - it doesn't actually display text however, it simply returns a more complex new text object which can then be printed.