Timer Value only decreases once.G

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
User avatar
ken.athomos
Citizen
Posts: 77
Joined: Fri Aug 05, 2016 10:13 am
Location: Philippines

Timer Value only decreases once.G

Post by ken.athomos »

Good Day everyone.

I'm currently going back to Love2D after a long hiatus and am finally working on a small project. I apologize if the problem I'm going tor report on is easy for some. I've just been racking my head on this for the past few hours now and I can't seem to see what I'm doing wrong. (Also, I apologize of the formatting of the code is really wonky).

I have a function that is suppose to change the color of a text every time the timer runs out. The problem is that the value of the timer only goes down once instead of going down continuously since it's part of the code being called in love.update. (For example, if the timer value is 10, instead of going [10,9,8...1,0 end], it's just [10, 9 end])

Code: Select all

-- colors.lua
local colors = {}

colors[1] = {255, 255, 255}		-- White
colors[2] = {255, 0, 0}			-- Red
colors[3] = {0, 255, 0}			-- Green
colors[4] = {0, 0, 255}			-- Blue

-- In charge of changing color instantly after a certain amount of time has passed.
function colors.instChange(maxTime, curTime, val, maxVal, dt)
	curTime = maxTime
	curTime = curTime - 1

	if curTime <= 0 then
		if val == maxVal then
			val = 1
			curTime = maxTime
		else
			val = val + 1
			curTime = maxTime
		end
	end
end

return colors

Code: Select all

local titleItems = {'Play', 'How', 'Exit'}
local titleSelected = 0

local color = require('src.bin.color')
local colorTitleMaxTime = 5
local colorTitleCurTime = colorTitleMaxTime
local colorTitleVal = 1

-- The parts below this are being called in the (non-library) gamestate handler I'm using.
function titleUpdate(dt)
	-- Change the color of the game title.
	color.instChange(colorTitleMaxTime, colorTitleCurTime, 1, 4, dt)
end

function titleDraw()
	love.graphics.setNewFont(24)
	love.graphics.setColor(color[colorTitleVal])
	-- Prints the game Title
	love.graphics.print('Mini Proj', 0, 0)
	-- Prints all the Title Items.
	love.graphics.setColor(color[1])
	for i=#titleItems,1,-1 do
		local ti = titleItems[i]
		love.graphics.print(ti, 50, 30*i)
	end
	-- Prints the Title Item Selector.
	if titleSelected ~= 0 then
		love.graphics.print('>>', 10, 30*titleSelected)
	end
end

function titleKeyPressed(key)
	-- For main menu controls. Up and Down to select, Enter/Return to choose.
	-- 1 = Play
	-- 2 = How
	-- 3 = Exit

	-- On startup, no item is selected. This will fix it.
	if titleSelected == 0 then
		if key == 'down' or key == 'up' then
			titleSelected = 1
		end
	elseif titleSelected == 1 then
		if key == 'up' then
			titleSelected = 3
		elseif key == 'down' then
			titleSelected = 2
		end
	elseif titleSelected == 2 then
		if key == 'up' then
			titleSelected = 1
		elseif key == 'down' then
			titleSelected = 3
		end
	elseif titleSelected == 3 then
		if key == 'up' then
			titleSelected = 2
		elseif key == 'down' then
			titleSelected = 1
		end
		if key == 'return' then
			love.event.quit()
		end
	end
end
Arguably, I could just put all the color changing code in the titleUpdate function so that it runs (which I did when I was thinking about the code) but I plan to re-use the color changing code at other parts of the UI.

I would appreciate any help given on this.

Thanks in advance.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Timer Value only decreases once.G

Post by bartbes »

You're setting the timer back to its maximum value (maxTime) every time, then decreasing it by one.
User avatar
ken.athomos
Citizen
Posts: 77
Joined: Fri Aug 05, 2016 10:13 am
Location: Philippines

Re: Timer Value only decreases once.G

Post by ken.athomos »

Not trying to sound like ungrateful but I still can't see how to fix the darn thing hahahahaha.
I honestly thought that since I just transferred the code to a function and called it an update function that it will decrease continuously lol.

EDIT: I'm clearly failing to understand something because if I just code the thing instead of using a function like so:

Code: Select all

function titleUpdate(dt)
	colorTitleCurTime = colorTitleCurTime - math.ceil(dt)
	if colorTitleCurTime <= 0 then
		-- Change the color of the game title.
		if colorTitleVal == 4 then
			colorTitleVal = 1
		else
			colorTitleVal = colorTitleVal + 1
		end
	colorTitleCurTime = colorTitleMaxTime
	end
end
Well it works however if I try to do the one earlier it doesn't seem to work. Is it because of how I made it into a function?

Please put me in the right direction. I'm currently pissed off about this (plus other external variables irl) so please forgive me if I sound like an ungrateful twat.
User avatar
Tjakka5
Party member
Posts: 243
Joined: Thu Dec 26, 2013 12:17 pm

Re: Timer Value only decreases once.G

Post by Tjakka5 »

It seems to me you come from a different language that supported references.
If this is not the case, please mention it so I can redo my explanation.

Variables can be global, or local to a scope (In loops, in functions, etc).
Values passed to functions or defined to another variable are copied.
The only exception to this is tables (And other userdata); they are stored by reference.


So, to shed a little light on why your code behaves like it does:

In 'titleUpdate' you send the value '1' as 'val' for 'colors.instChange'.
Because of this, val will always equal 1.
You are able to edit 'val', but it's changes are only in the scope of that function.

This applies to all the values you pass to the function.


To counteract this, 'colors.lua' should either only hold the color data, and the logic should be moved to your title file.
Or, 'colors.lua' should define the values. This is up to you.
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Timer Value only decreases once.G

Post by pgimeno »

Pay attention to this fragment right at the beginning of your function:

Code: Select all

	curTime = maxTime
	curTime = curTime - 1
That sequence of instructions will always set curTime to maxTime - 1, and will never decrease it past that. It's similar to this:

Code: Select all

repeat
  a = 5
  a = a - 1
  print(a)
until a == 0
This will print 4 over and over. However, this:

Code: Select all

a = 5
repeat
    a = a - 1
    print(a)
until a == 0
will print 4, 3, 2, 1 and 0, which is probably what one wants. Similarly, you need to move the initialization of curTime out of your function, so that it isn't initialized on every call.
User avatar
ken.athomos
Citizen
Posts: 77
Joined: Fri Aug 05, 2016 10:13 am
Location: Philippines

Re: Timer Value only decreases once.G

Post by ken.athomos »

Good day everyone.

Thanks for all the replies. It seems I really am referencing stuff beyond their scope. For the meantime, I'll take into account about what Tjakka said about color.lua holding only color data and writing the code in titleUpdate (basically what I did in the edit of my previous reply).

I'll also be re-evaluating how I understand functions because I swear a part of me believes that the way i was structuring is supposed to work (but it obviously doesn't so...)

Thanks everyone.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 26 guests