This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Mackan90096's profile picture

[Question] Adding a number to a variable every X seconds?

Started by Mackan90096, 08 May 2013 - 02:33 AM
Mackan90096 #1
Posted 08 May 2013 - 04:33 AM
Hi! I'm testing stuff out and I'm wondering if you can add a number to a variable every X seconds.

Like:

money = 10

Every X seconds
money = money + 10
Frederikam #2
Posted 08 May 2013 - 05:11 AM
Sure, that would be something like this:


secondsToWait = 5
money = 0
moneyToAdd = 10
while true do
    sleep(secondsToWait)
    money = money + moneyToAdd
end
Mackan90096 #3
Posted 08 May 2013 - 06:06 AM
I want to be able to do other stuff in the meantime too…

Is that possible?
And if so.. How?
Spongy141 #4
Posted 08 May 2013 - 09:08 AM
I want to be able to do other stuff in the meantime too…

Is that possible?
And if so.. How?
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 do

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. :)/>
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.
Engineer #5
Posted 08 May 2013 - 09:28 AM
I want to be able to do other stuff in the meantime too…

Is that possible?
And if so.. How?
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 do

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. :)/>/>/>
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.

It depends on the program what he wants to do. If its os.pullEvent based, you can do it like so:

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

No need of unnecessary use of the parralel API :P/>/>
M4sh3dP0t4t03 #6
Posted 08 May 2013 - 09:39 AM
Tou could also use coroutines
LBPHacker #7
Posted 08 May 2013 - 09:43 AM
Tou could also use coroutines
parallel API uses coroutines. Please, forget parallel or coroutines for these kind of programs.
digpoe #8
Posted 08 May 2013 - 09:44 AM
Tou could also use coroutines
I want to be able to do other stuff in the meantime too…

Is that possible?
And if so.. How?
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 do

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. :)/>
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.

Coroutines and the Parallel API are unneeded, and in fact make the code worse. It's probably better to use events.
theoriginalbit #9
Posted 08 May 2013 - 09:44 AM
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… I've only ever used coroutines twice, once it was 100% needed, the second was just because it made some integration easier…
digpoe #10
Posted 08 May 2013 - 09:45 AM
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.

They probably want to use them because they think it's cool to be multithreaded, when in fact coroutines aren't even real threads.
Mackan90096 #11
Posted 08 May 2013 - 09:54 AM
Well I want to add money and still be able to use the keys

Its using os.pullEvent("key")
theoriginalbit #12
Posted 08 May 2013 - 09:57 AM
Still NO need for coroutines.

Engineer gave an example of this.

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
digpoe #13
Posted 08 May 2013 - 10:03 AM
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
Spongy141 #14
Posted 08 May 2013 - 10:14 AM
^
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.
digpoe #15
Posted 08 May 2013 - 10:16 AM
^
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:
digpoe #16
Posted 08 May 2013 - 10:22 AM
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.

If you really wanted to print the key which is pressed, 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 "..keys.getName(p1).." was pressed.")
elseif event == "timer" then
  cash = cash+10
  os.startTimer(10)
end
end
theoriginalbit #17
Posted 08 May 2013 - 10:26 AM
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.
digpoe #18
Posted 08 May 2013 - 10:27 AM
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.
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.
theoriginalbit #19
Posted 08 May 2013 - 10:29 AM
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.
yes, but that isn't necessarily the use case that Mackan90096 had in mind.
Spongy141 #20
Posted 08 May 2013 - 06:05 PM
^
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:
Oh wow, thanks for the chart. Thats going to be really helpful, since it is a pain sometimes to search for the key number.
Kingdaro #21
Posted 08 May 2013 - 06:15 PM
I 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.
theoriginalbit #22
Posted 08 May 2013 - 11:50 PM
I 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.
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.4
Kingdaro #23
Posted 09 May 2013 - 01:05 AM
The only people using a version that old are tekkit classic players, who probably only play for OP EE2 items. That version isn't worth supporting, imo.
sploders101 #24
Posted 09 May 2013 - 11:29 AM
^
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:

Where did you get that chart anyway?
theoriginalbit #25
Posted 09 May 2013 - 12:51 PM
Where did you get that chart anyway?
From the wiki