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

<Help Me> Make my game update by itself, not after user input.

Started by lolcakes64, 30 April 2012 - 02:06 PM
lolcakes64 #1
Posted 30 April 2012 - 04:06 PM
My game currently only updates where everything is when a user types any key on the keyboard, I'm guessing it's something to do with the character being controlled with "local sEvent, param = os.pullEvent()" or something.

But can I make it so the game updates every quarter of second or something, while still being able to control the main character with the keyboard?
CypherAJ #2
Posted 30 April 2012 - 04:16 PM
try parallel API
lolcakes64 #3
Posted 30 April 2012 - 04:43 PM
Try parallel API.

Is there a way to do it without an API?

EDIT: I thought you had to download it or something, but you don't. So… Derp.
Dirkus7 #4
Posted 30 April 2012 - 04:55 PM
Try parallel API.

Is there a way to do it without an API?
I don't think so, why would you do it without an API? Do you hate them?
lolcakes64 #5
Posted 30 April 2012 - 06:36 PM
Try parallel API.
Is there a way to do it without an API?

EDIT: I thought you had to download it or something, but you don't. So… Derp.
I don't think so, why would you do it without an API? Do you hate them?

I thought it wasn't included in CC, but it is. So… How would you use it to update the game by itself?
MysticT #6
Posted 30 April 2012 - 08:02 PM
I don't think the parallel API is the best way to do this. Iit would be better to use a timer:

local updateTimer = os.startTimer(0.25)

local function update()
  -- update the game
end

local function keyPress(key)
  -- a key was pressed, check wich one and do wathever you need
end

while true do
  local sEvt, arg = os.pullEvent()
  if sEvt == "key" then
    keyPress(arg)
  elseif sEvt == "timer" then
    if arg == updateTimer then
	  update()
	  updateTimer = os.startTimer(0.25)
    end
  end
end
You add the code for updating in the update() function, and handle key press on the keyPress() function. You can add other events also, just by adding an elseif and the corresponding function (you can do it in the loop, but functions make your code more readable, reusable and less repetitive :)/>/>).