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!
Repeat increment until variable is a specific amount issue
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 17
- Joined: Fri Feb 26, 2016 1:42 pm
Repeat increment until variable is a specific amount issue
- Attachments
-
- man im so good at lua.love
- (305 Bytes) Downloaded 182 times
Re: Repeat increment until variable is a specific amount issue
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:
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.
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:
This way just in case you somehow exceed the value you expect to reach, your loop will still end.
Your game crashes on line 8... let's look at that line of code:
Code: Select all
until h = 580
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
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
Re: Repeat increment until variable is a specific amount issue
This might work too
Code: Select all
-- start, stop, step
for h = 2, 580, 5 do print(h) end
-
- Prole
- Posts: 17
- Joined: Fri Feb 26, 2016 1:42 pm
Re: Repeat increment until variable is a specific amount issue
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!
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!
Re: Repeat increment until variable is a specific amount issue
You should definitely consider the for loop. I use for loops for iterating through array indices.
It looks like this:
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.
It looks like this:
Code: Select all
for i=1,10,1 do
--do stuff here
end
-
- Prole
- Posts: 17
- Joined: Fri Feb 26, 2016 1:42 pm
Re: Repeat increment until variable is a specific amount issue
Alright, I'll do some experimenting when I'm back at my computer!
Re: Repeat increment until variable is a specific amount issue
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)
Who is online
Users browsing this forum: No registered users and 3 guests