Like:
money = 10
Every X seconds
money = money + 10
secondsToWait = 5
money = 0
moneyToAdd = 10
while true do
sleep(secondsToWait)
money = money + moneyToAdd
end
Well you can use a parallel function, just make the while true do loop in its seperate function, and the body of everything else in another, then either doI want to be able to do other stuff in the meantime too…
Is that possible?
And if so.. How?
parallel.waitForAll(func1, func2) --You can add more than 2 functions. I think as many as you want, but I never really need more than 3. You probably wont either.
--Or
parallel.waitForAny(func1, func2) --This will stop both functions when one is finished, not a good chose if you have a function longer than the other that needs to finish.
And btw, do [*code] and [*/code] around your code. :)/>Well you can use a parallel function, just make the while true do loop in its seperate function, and the body of everything else in another, then either doI want to be able to do other stuff in the meantime too…
Is that possible?
And if so.. How?And btw, do [*code] and [*/code] around your code. :)/>/>/>parallel.waitForAll(func1, func2) --You can add more than 2 functions. I think as many as you want, but I never really need more than 3. You probably wont either. --Or parallel.waitForAny(func1, func2) --This will stop both functions when one is finished, not a good chose if you have a function longer than the other that needs to finish.
EDIT: Also, if your making a bank program, be sure to save the last variable in a file before the program quits, I'm sure people wouldn't be happy if there money never made it to there account.
local var = 0
local startTimer = os.startTimer( 1 )
local event = {os.pullEvent()} -- You could param1, param2, param3 = os.pullEvent()
if event[1] == "timer" then
var = var + 1
startTimer = os.startTimer( 1 )
elseif event[1] == "someEvent" then
--etc..
end
parallel API uses coroutines. Please, forget parallel or coroutines for these kind of programs.Tou could also use coroutines
Tou could also use coroutines
Well you can use a parallel function, just make the while true do loop in its seperate function, and the body of everything else in another, then either doI want to be able to do other stuff in the meantime too…
Is that possible?
And if so.. How?And btw, do [*code] and [*/code] around your code. :)/>parallel.waitForAll(func1, func2) --You can add more than 2 functions. I think as many as you want, but I never really need more than 3. You probably wont either. --Or parallel.waitForAny(func1, func2) --This will stop both functions when one is finished, not a good chose if you have a function longer than the other that needs to finish.
EDIT: Also, if your making a bank program, be sure to save the last variable in a file before the program quits, I'm sure people wouldn't be happy if there money never made it to there account.
There are very few times that you need to use coroutines… most cases an event loop is enough, and without more information this is one of those times… do people keep wanting to use coroutines because they are 'cool' or something, because personally I don't see why people wish to use them all the time… I've only ever used coroutines twice, once it was 100% needed, the second was just because it made some integration easier…Tou could also use coroutines
There are very few times that you need to use coroutines… most cases an event loop is enough, and without more information this is one of those times… do people keep wanting to use coroutines because they are 'cool' or something, because personally I don't see why people wish to use them all the time.Tou could also use coroutines
while true do
local event, p1 = os.pullEvent()
if event == 'key' then
-- a key has been pressed
elseif event == 'timer' then
-- the timer has completed
end
end
os.startTimer(10)
local cash = 0
while true do
local event, p1 = os.PullEvent()
if event == "key" then
print("key "..p1.." was pressed.")
elseif event == "timer" then
cash = cash+10
os.startTimer(10)
end
end
^
correct me if I'm wrong, but you key would show up as a number, since when you define a key, you use a number, and it reads as a number anyways.
You can use this code:os.startTimer(10) local cash = 0 while true do local event, p1 = os.PullEvent() if event == "key" then print("key "..p1.." was pressed.") elseif event == "timer" then cash = cash+10 os.startTimer(10) end end
^
correct me if I'm wrong, but you key would show up as a number, since when you define a key, you use a number, and it reads as a number anyways. So you might want to make something to change the number into a word, so the user (or you) can easily read what key was pressed, and not the number for it.
os.startTimer(10)
local cash = 0
while true do
local event, p1 = os.PullEvent()
if event == "key" then
print("key "..keys.getName(p1).." was pressed.")
elseif event == "timer" then
cash = cash+10
os.startTimer(10)
end
end
I normally use the ENTER key whenever a BATCH script says 'press any key to continue', so that's why I used the key event, not the char event.or depending on what you want to display, you could use the 'char' event, which only fires whenever a letter, symbol or number key is pressed; not when a functional key like enter or backspace is pressed, unlike the 'key' event, where you receive the key code of any pressed key.
yes, but that isn't necessarily the use case that Mackan90096 had in mind.I normally use the ENTER key whenever a BATCH script says 'press any key to continue', so that's why I used the key event, not the char event.
Oh wow, thanks for the chart. Thats going to be really helpful, since it is a pain sometimes to search for the key number.^
correct me if I'm wrong, but you key would show up as a number, since when you define a key, you use a number, and it reads as a number anyways.
Yes, you're right. The printed key would appear as it's numerical value as said in this image:
Yeh the only problem is if you're trying to make a program compatible with pre CC1.4 then you need to use the key codes as the keys API was added in 1.4I don't use that image (or others like it) because every key is defined conveniently in the keys table. More typing, but less having to reference an image every time I need to use key events.
^
correct me if I'm wrong, but you key would show up as a number, since when you define a key, you use a number, and it reads as a number anyways.
Yes, you're right. The printed key would appear as it's numerical value as said in this image:
From the wikiWhere did you get that chart anyway?