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

A bit help with code?

Started by Datamats, 08 March 2013 - 06:24 AM
Datamats #1
Posted 08 March 2013 - 07:24 AM
I got this script and i don't get the bundled output can someone fix the script i'm new to lua :3

I don't know how get a bundled output from the buttons in the script can someone help me for someone this is well easy but for me so just started i need the learn so plz show me some help…



--Requires advanced monitors and computers!!!

function toggle(n)
  if not n then
    return
  end
  local state=lightState[n]
  if state then
    state = false
  else
    state = true
  end
  lightState[n] = state
  return state
end

function sayState(n)
  if not n then
    return
  end
  x = buttonX[n]-1
  y = buttonY[n]
  term.setCursorPos(x,y)
  if lightState[n] then
    term.setTextColor(colors.lime)
    write("on ")
    term.setTextColor(colors.white)
  else
    term.setTextColor(colors.red)
    write("off")
    term.setTextColor(colors.white)
  end
end

function getButton(xPos,yPos)
  for i=1,12 do
    bxPos=buttonX[i]
    byPos=buttonY[i]
    xMax=bxPos+2
    xMin=bxPos-2
    yMax=byPos+1
    yMin=byPos-1
    if xPos >= xMin and xPos <= xMax and yPos >= yMin and yPos <= yMax then
      return i
    end
  end
end

function mPrint(w)
  write(w)
  x,y=term.getCursorPos()
  term.setCursorPos(1,y+1)
end

function allTheSame()
  local state = lightState[1]
  for i=2,10 do
    if state==lightState[i] then
    else return false
    end
  end
  return true
end

function stateWriter()
  mPrint("  _____     _____     _____     _____")
  write("  ")
  for i=1,4 do
    write("|")
    term.setTextColor(colors.red)
    write("off")
    term.setTextColor(colors.white)
    if i<4 then
      write("|     ")
    else
      mPrint("|")
    end
  end
  mPrint("  ~~~~~     ~~~~~     ~~~~~     ~~~~~")
end

function startText()
  term.setCursorPos(1,1)
  mPrint("_______________________________________")
  mPrint(" Welcome to spawn select! Control mob")
  mPrint(" spawn choice by right click on screen")
  mPrint(" ")
  mPrint("  Skele     Wither    Ender     Zombie")
  stateWriter()
  mPrint(" ")
  mPrint("  Creep     Witch     Pigman    Cow")
  stateWriter()
  mPrint(" ")
  mPrint("  Blaze     Slime     Toggle    Master")
  stateWriter()
  mPrint("_______________________________________")
end

function bundleState()

end

display = peripheral.wrap("right")
term.redirect(display)
term.clear()
term.setCursorPos(1,1)
buttonX = {
5,15,25,35,
5,15,25,35,
5,15,25,35
}

buttonY = {
7,7,7,7,
12,12,12,12,
17,17,17,17
}

lightState = {false,false,false,false,false,false,false,false,false,false,false,false}

spawnerColor = {
colors.white, colors.orange, colors.magenta, colors.lightBlue, 
colors.yellow, colors.lime, colors.pink, colors.gray, 
colors.lightGray, colors.cyan, colors.purple, colors.blue
}

local resume=true
startText()
for i=1,10 do
  sayState(i)
end
term.setCursorPos(buttonX[12]-1,buttonY[12])
term.setTextColor(colors.red)
write("off")
term.setTextColor(colors.white)
term.setCursorPos(buttonX[11]-1,buttonY[11])
term.setTextColor(colors.cyan)
write("TGL")
term.setTextColor(colors.white)
while resume == true do
  local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  local button = getButton(xPos,yPos)
  if button==11 then
    for i=1,10 do
      toggle(i)
    end
  elseif button==12 then
    toggle(12)
    for i=1,10 do
      lightState[i]=lightState[12]
    end
    sayState(12)
    allSame=true
  else
    term.setCursorPos(buttonX[12]-1,buttonY[12])
    term.setTextColor(colors.lightGray)
    write("---")
    term.setTextColor(colors.white)
    toggle(button)
  end
  for i=1,10 do
    sayState(i)
  end
  if allTheSame() then
    lightState[12]=lightState[1]
    sayState(12)
  else
    term.setCursorPos(buttonX[12]-1,buttonY[12])
    term.setTextColor(colors.lightGray)
    write("---")
    term.setTextColor(colors.white)
  end
end


--[==[Tables are used to quite an extent throughout this program.
The order of said tables is important as these must remain consistent in order for the script to execute.]==]
Relly need it to my Mob Spawner control! ;)/>/>
[right]-Datamats[/right]
Lyqyd #2
Posted 08 March 2013 - 08:55 AM
Split into new topic.

I think you'll need to explain what you expect to happen and what actually happens (or doesn't happen) a bit better. Code isn't something that just magically works and can be plugged into any vaguely similar set of circumstances. Just saying "doesn't work" does not imply any effort on your part and is likely to get you less help than if you were to put in more effort to getting it working.
Datamats #3
Posted 09 March 2013 - 01:42 AM
I relly need help with my mob control system!
LordIkol #4
Posted 09 March 2013 - 03:17 AM
and we really need more explenation to help you!

1. What ist the Script supposed to do exactly? (dont say "Spwn Mobs" :D/> )
2. Is the script you posted working or not?
2.1 if it is working what do you want to modify
2.2 if it is not working what error does it spit out
2.2.1. if there is no error what does the script do wrong?
3. Use more information in your Topic like "Help Changing a Script to control Mobspawn" or "Mobspawn Script throws error xy"

For me it looks like you got a script from the Internet that basicly does what you need in your project and then post it here so that someone here does the rest for you.
Thats ok for me (not for everybody) but when you do so then at least do some work in explaining what you want.
Nobody will help you when you get told to provide more information so that we can help and your answer is


I relly need help with my mob control system!

Greets Loki
theoriginalbit #5
Posted 09 March 2013 - 03:45 AM
take a look at the Redstone API in particular the bundled cable functions.
Datamats #6
Posted 09 March 2013 - 03:48 AM
take a look at the Redstone API in particular the bundled cable functions.
I try but idk how to get the variables to bundled singnal that is what i need help to
theoriginalbit #7
Posted 09 March 2013 - 03:53 AM
there is another API called colours (or colors) and you can use it like
colours.red
or
colors.blue
etc. these are nice and easy variables to access, however all that the colours actually are is numbers on a binary scale, 2^0 through to 2^15 … the redstone api functions want these 'colours' to output with. you need to make your code according to these restrictions.

An example can be seen here which I got to just by clicking on the function on the previous link I supplied.
Datamats #8
Posted 09 March 2013 - 04:03 AM
there is another API called colours (or colors) and you can use it like
colours.red
or
colors.blue
etc. these are nice and easy variables to access, however all that the colours actually are is numbers on a binary scale, 2^0 through to 2^15 … the redstone api functions want these 'colours' to output with. you need to make your code according to these restrictions.

An example can be seen here which I got to just by clicking on the function on the previous link I supplied.
Sorry i know the colours api but i need to get this script



--Requires advanced monitors and computers!!!

function toggle(n)
  if not n then
	return
  end
  local state=lightState[n]
  if state then
	state = false
  else
	state = true
  end
  lightState[n] = state
  return state
end

function sayState(n)
  if not n then
	return
  end
  x = buttonX[n]-1
  y = buttonY[n]
  term.setCursorPos(x,y)
  if lightState[n] then
	term.setTextColor(colors.lime)
	write("on ")
	term.setTextColor(colors.white)
  else
	term.setTextColor(colors.red)
	write("off")
	term.setTextColor(colors.white)
  end
end

function getButton(xPos,yPos)
  for i=1,12 do
	bxPos=buttonX[i]
	byPos=buttonY[i]
	xMax=bxPos+2
	xMin=bxPos-2
	yMax=byPos+1
	yMin=byPos-1
	if xPos >= xMin and xPos <= xMax and yPos >= yMin and yPos <= yMax then
	  return i
	end
  end
end

function mPrint(w)
  write(w)
  x,y=term.getCursorPos()
  term.setCursorPos(1,y+1)
end

function allTheSame()
  local state = lightState[1]
  for i=2,10 do
	if state==lightState[i] then
	else return false
	end
  end
  return true
end

function stateWriter()
  mPrint("  _____	 _____	 _____	 _____")
  write("  ")
  for i=1,4 do
	write("|")
	term.setTextColor(colors.red)
	write("off")
	term.setTextColor(colors.white)
	if i<4 then
	  write("|	 ")
	else
	  mPrint("|")
	end
  end
  mPrint("  ~~~~~	 ~~~~~	 ~~~~~	 ~~~~~")
end

function startText()
  term.setCursorPos(1,1)
  mPrint("_______________________________________")
  mPrint(" Welcome to spawn select! Control mob")
  mPrint(" spawn choice by right click on screen")
  mPrint(" ")
  mPrint("  Skele	 Wither	Ender	 Zombie")
  stateWriter()
  mPrint(" ")
  mPrint("  Creep	 Witch	 Pigman	Cow")
  stateWriter()
  mPrint(" ")
  mPrint("  Blaze	 Slime	 Toggle	Master")
  stateWriter()
  mPrint("_______________________________________")
end

function bundleState()

end

display = peripheral.wrap("right")
term.redirect(display)
term.clear()
term.setCursorPos(1,1)
buttonX = {
5,15,25,35,
5,15,25,35,
5,15,25,35
}

buttonY = {
7,7,7,7,
12,12,12,12,
17,17,17,17
}

lightState = {false,false,false,false,false,false,false,false,false,false,false,false}

spawnerColor = {
colors.white, colors.orange, colors.magenta, colors.lightBlue,
colors.yellow, colors.lime, colors.pink, colors.gray,
colors.lightGray, colors.cyan, colors.purple, colors.blue
}

local resume=true
startText()
for i=1,10 do
  sayState(i)
end
term.setCursorPos(buttonX[12]-1,buttonY[12])
term.setTextColor(colors.red)
write("off")
term.setTextColor(colors.white)
term.setCursorPos(buttonX[11]-1,buttonY[11])
term.setTextColor(colors.cyan)
write("TGL")
term.setTextColor(colors.white)
while resume == true do
  local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  local button = getButton(xPos,yPos)
  if button==11 then
	for i=1,10 do
	  toggle(i)
	end
  elseif button==12 then
	toggle(12)
	for i=1,10 do
	  lightState[i]=lightState[12]
	end
	sayState(12)
	allSame=true
  else
	term.setCursorPos(buttonX[12]-1,buttonY[12])
	term.setTextColor(colors.lightGray)
	write("---")
	term.setTextColor(colors.white)
	toggle(button)
  end
  for i=1,10 do
	sayState(i)
  end
  if allTheSame() then
	lightState[12]=lightState[1]
	sayState(12)
  else
	term.setCursorPos(buttonX[12]-1,buttonY[12])
	term.setTextColor(colors.lightGray)
	write("---")
	term.setTextColor(colors.white)
  end
end


--[==[Tables are used to quite an extent throughout this program.
The order of said tables is important as these must remain consistent in order for the script to execute.]==]
i don't know how i can get the state off the buttons in the script to give hes own colour output in the RP2 bundled cable so know you how to modify the script to do that idk…. So i will try get this script working from http://www.computercraft.info/forums2/index.php?/topic/10607-selector-touchscreen/page__p__89257#entry89257
theoriginalbit #9
Posted 09 March 2013 - 04:12 AM
Sorry i know the colours api but i need to get this script
– code snip –
i don't know how i can get the state off the buttons in the script to give hes own colour output in the RP2 bundled cable so know you how to modify the script to do that idk….
You didn't need to post the code again, we are all capable of scrolling.

it seems that you already have the code to detect which button was clicked, all you need to do is use
rs.setBundledOutput(<side>, <color>)
based on whichever needs to be activated where <side> is the side that the cable is on, and <color> is the colour to turn on.
Datamats #10
Posted 09 March 2013 - 08:08 AM
Ik i'm a noob but i don't get it to work :blink:/>
Datamats #11
Posted 09 March 2013 - 08:15 AM
And that i need help with are transfer the button state to bundled signal in this script O_O
R167 #12
Posted 10 March 2013 - 06:04 PM
1. please give me, author of the script, credit as link or something.
2. I added infrastructure for the colors thing, but have as of yet to setup a thing to combine the colors in the output. I was hoping someone else would help with that.
3. there is this thing called spoilers.