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

A little help here?

Started by popdog15, 26 May 2012 - 04:42 PM
popdog15 #1
Posted 26 May 2012 - 06:42 PM
Im trying to make a code where that the user needs to input a value that they want for x, and I can't seem to find any sort of code that someone could input a value for x (on the computer).
LordDrako #2
Posted 26 May 2012 - 06:52 PM
I guess you have to use os.pullEvent and handle the char/key events…
this way you can build an input string which you can convert to a number afterwards (tonumber)
Cloudy #3
Posted 26 May 2012 - 07:30 PM
x = tonumber(read())
Zalerinian #4
Posted 26 May 2012 - 07:45 PM
You need to give more detail. what is the variable x going to do?
Maarten551 #5
Posted 26 May 2012 - 08:16 PM
I think I know what you mean.

I have used this in my code :
write("Height : ")
height = read()
heightmax = tonumber(height)

It will ask for an input and the variable "height" will be the input, the return from the input will be a string.
the tonumber function is to make it from a string to an integer.

Hope this helps
popdog15 #6
Posted 28 May 2012 - 05:35 AM
You need to give more detail. what is the variable x going to do?

Its going to be the name you set, for a text adventure.
Noodle #7
Posted 28 May 2012 - 05:48 AM

x = "String"
print(x)
xVar = read()
if xVar == "OTHER STRING" then
print("ok.")
end

That prints the variable (x) then it reads the input and gives a reply.
Example:

x = "Username: "
write(x)
username = read()
if username == "hohoho" then
print("Accepted")
else
print("Denied")
end

EDIT: If you are searching for a word, "Dig" then you are going to have to read the "xVar" for that specific thing..
Bossman201 #8
Posted 29 May 2012 - 06:39 PM

function handleEvent()
	evt, p1, p2 = os.pullEvent()
if evt == "char" then
   writeChar(p1)
	elseif evt == "key" then
   handleKey(p1)
elseif evt == "rednet_message" then
   messageReceive(p1, p2)
end
end

This is what LordDrako was talking about but it doesn't have to be so complicated.

Using a simple:

x = io.read()
will work as well, although it will halt your program until you press enter.