Repeat increment until variable is a specific amount issue

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
ThiefOfPringles
Prole
Posts: 17
Joined: Fri Feb 26, 2016 1:42 pm

Repeat increment until variable is a specific amount issue

Post by ThiefOfPringles »

As the title may suggest, I'm trying to make a variable increment until it hits a certain amount. It's based off the rectangle example on the LOVE wiki. I hope I don't seem like a complete idiot for not knowing and having any help would be amazing.
Thanks!
Attachments
man im so good at lua.love
(305 Bytes) Downloaded 182 times
User avatar
Foogles
Prole
Posts: 14
Joined: Thu Feb 18, 2016 5:33 pm

Re: Repeat increment until variable is a specific amount issue

Post by Foogles »

Hey, you're not an idiot :) We all have to start somewhere, and you're on the right track, there's just a typo or misunderstanding with one of your operators.

Your game crashes on line 8... let's look at that line of code:

Code: Select all

	until h = 580
So it's clear you're using the repeat ... until statement in lua.
The until statement expects a boolean (true or false) value to follow.
When you want to create a boolean comparison of two values, you have to use boolean operators.

Some Regular Operators:
Symbols:
=, +, -, *, /
Interpeted In English: (for your understanding)
set left equal to right, add right to left, subtract right from left, multiply left and right, divide left by right

Some Boolean Operators:
Symbols:
==, >=, <=, ~=
Interpeted In English: (for your understanding)
is left equal to right, is left greater than right, is left less than right, is left not equal to right

When you write h = 580, the program will set h equal to 580, or at least it will try. That's how the operator works.
What you want is to check if h equals 580, so use the == operator.

Code: Select all

	until h == 580
Let's trace this new line. Assume h is 571.
The program will check "576 == 580" and return "false" which will mean the loop continues.
Okay, so let's add 5 to h... now h is 581, so the program checks "581 == 580" and returns false.
Utoh, infinite loop. I would recommend that instead of that that operator you should actually use this:

Code: Select all

	until h >= 580
This way just in case you somehow exceed the value you expect to reach, your loop will still end.
monolifed
Party member
Posts: 188
Joined: Sat Feb 06, 2016 9:42 pm

Re: Repeat increment until variable is a specific amount issue

Post by monolifed »

This might work too

Code: Select all

-- start, stop, step
for h = 2, 580, 5 do print(h) end
ThiefOfPringles
Prole
Posts: 17
Joined: Fri Feb 26, 2016 1:42 pm

Re: Repeat increment until variable is a specific amount issue

Post by ThiefOfPringles »

Thanks to both of you! Glad you guys were able to help me!
Though I was actually able to recieve help from one of my friends and instead I ended up using an if then statement. I'll keep in mind what you guys said for the future.
Thanks again!
User avatar
Foogles
Prole
Posts: 14
Joined: Thu Feb 18, 2016 5:33 pm

Re: Repeat increment until variable is a specific amount issue

Post by Foogles »

You should definitely consider the for loop. I use for loops for iterating through array indices.
It looks like this:

Code: Select all

for i=1,10,1 do
   --do stuff here
end
Essentially it says, set a new local variable i equal to 1, stop this loop when i is greater than 10, and add 1 to i each time the loop is run.
ThiefOfPringles
Prole
Posts: 17
Joined: Fri Feb 26, 2016 1:42 pm

Re: Repeat increment until variable is a specific amount issue

Post by ThiefOfPringles »

Alright, I'll do some experimenting when I'm back at my computer!
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: Repeat increment until variable is a specific amount issue

Post by Beelz »

Another thing you could do is clamp the number within a range:

Code: Select all

function clamp(num, low, high) return math.min(math.max(low, num), high) end

--Then:
h = clamp(h, 100, 600)

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 4 guests