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

[Open Peripherals] function only works once

Started by Zamithal, 11 August 2013 - 06:26 AM
Zamithal #1
Posted 11 August 2013 - 08:26 AM
Title[Open Peripherals] function only works once


function changeBoxHeight(desiredHeightChange, object, speed, direction)
   local boxWidth = object.getWidth();
   local boxHeight = object.getHeight();
   local boxX = object.getX();
   local boxY = object.getY();
   print(object.getHeight());
   for i = 1, desiredHeightChange/5 do
      object.delete();
   if direction == "up" then
      object = net.addBox(boxX, boxY, boxWidth, boxHeight - 5*i, 0xFFFFFF, 0.5);
   end
   if direction == "down" then
      object = net.addBox(boxX, boxY, boxWidth, boxHeight + 5*i, 0xFFFFFF, 0.5);
   end
      sleep(speed);
   end
end

This function works perfectly..once, after it fires once whatever I put in for object will return nil if

variable.getHeight();
is asked of it. Any idea what I did wrong?
Bubba #2
Posted 11 August 2013 - 09:05 AM
Split into new topic.

You call object.delete in your code, deleting all of the box instances so that they can't be used again. I see that you add new boxes later in the code, but you'd have to return them from the function if you wanted to actually do anything with them.

However, I'm not sure why you're deleting/creating boxes in the first place. I believe that OpenPeripheral has a setHeight and setWidth method so that you can just change the already existing boxes to suit your purposes.
Zamithal #3
Posted 11 August 2013 - 10:29 AM
Im doing it incrementally, so it gets a nice expanding/shrinking affect whenever I want it to change sizes instead of sudden changes. I didn't try setheight though, does it do an effect or is it sudden? Also, pertaining to my question, can I not just return 'object' itself?
Bubba #4
Posted 11 August 2013 - 10:35 AM
Im doing it incrementally, so it gets a nice expanding/shrinking affect whenever I want it to change sizes instead of sudden changes. I didn't try setheight though, does it do an effect or is it sudden? Also, pertaining to my question, can I not just return 'object' itself?

Well it's instant, but if you use a very small sleep or use a tiny step then it would make the effect you're looking for. However, I would suggest using a larger step and a sleep in order to reduce lag/unnecessary operations (the human eye can only see so much anyway).

And because you delete the object, you could return it but not do anything with it.

If you want incremental height, just use something like this:

local function adjustHeight(object, newHeight, speed, direction) --# For speed I would suggest using something like 0.05 (the smallest sleep you can use)
  local nX,nY = object.getX(),object.getY()
  local diff = object.getHeight() > newHeight and -1 or 1 --# Use this to determine whether the size is increasing or decreasing, and adjust the step accordingly
  for y=object.getHeight(), newHeight, diff do

    object.setHeight(y)

    if direction>0 then --# If you want to adjust the height in the opposite direction, we have to actually move the box upwards while increasing it's height
      object.setPosition(nX,nY-1)
    end

    sleep(speed)
  end
end
Zamithal #5
Posted 11 August 2013 - 10:40 AM
Oh,I see what you mean, instead of deleting it in my function I could use setHeight, I thought you meant instead of the function all together. OK, well if get rid of the deleting that should fix it, thank you you have been a big help.