Displaying (or Drawing) Coordinates

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Benana
Prole
Posts: 1
Joined: Tue Dec 12, 2023 6:01 pm

Displaying (or Drawing) Coordinates

Post 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
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: Displaying (or Drawing) Coordinates

Post 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
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
dusoft
Party member
Posts: 629
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Displaying (or Drawing) Coordinates

Post 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
User avatar
Bobble68
Party member
Posts: 162
Joined: Wed Nov 30, 2022 9:16 pm
Contact:

Re: Displaying (or Drawing) Coordinates

Post 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.
Dragon
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 2 guests