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

Trouble whist making an API

Started by KingOfAllChunks, 06 March 2014 - 08:58 AM
KingOfAllChunks #1
Posted 06 March 2014 - 09:58 AM
Hello again. Today i tried making an api that will "help" me with other scripts. It basicly is supposed to spawn a button to an attached monitor at the top of the computer with x and y being its starting point, t being the text, tc and bc are text color and background color. But im having an issue. When i open the interactive lua prompt, i load the api, and then type button.spawn(1,1,test,colors.white,colors.black) i get an error: "button:6: attempt to concatenate string and nil". As my research goes, i could not find solution to it as of now. Thats im asking you!

Link to the script:http://pastebin.com/kLwAFZYZ
Link to the world:https://www.google.com/

The only time IT WORKS is when i set x,y and t to NUMBER values. Aka
button.spawn(1,1,5,colors.white,colors.black)

EDIT: FIXED IT! I just needed to use " and " for the text input ( aka button.spawn(1,1,"InputTextHere",colors.white,colors.black)
i will keep the thread just if someone has the same problem as me. You can awesoe, if you want, use the API for your scripts, if it is in any use for you
Edited on 06 March 2014 - 09:08 AM
Lignum #2
Posted 06 March 2014 - 10:07 AM
Hello again. Today i tried making an api that will "help" me with other scripts. It basicly is supposed to spawn a button to an attached monitor at the top of the computer with x and y being its starting point, t being the text, tc and bc are text color and background color. But im having an issue. When i open the interactive lua prompt, i load the api, and then type button.spawn(1,1,test,colors.white,colors.black) i get an error: "button:6: attempt to concatenate string and nil". As my research goes, i could not find solution to it as of now. Thats im asking you!

Link to the script:http://pastebin.com/kLwAFZYZ
Link to the world:https://www.google.com/

The only time IT WORKS is when i set x,y and t to NUMBER values. Aka
button.spawn(1,1,5,colors.white,colors.black)


button.spawn(1, 1, test, colors.white, colors.black)

Assuming that 't' is the text to be shown, you're passing a non-existent variable named 'test' here.
You need to mark it as a string:


button.spawn(1, 1, "test", colors.white, colors.black)

I recommend changing this:

m.write(""..t)

to:

m.write(tostring(t))
Edited on 06 March 2014 - 09:08 AM
CometWolf #3
Posted 06 March 2014 - 10:11 AM
You had to put "" around a string… You don't say! Why are you concatnating with a blank string anyway?
KingOfAllChunks #4
Posted 06 March 2014 - 10:21 AM
Hello again. Today i tried making an api that will "help" me with other scripts. It basicly is supposed to spawn a button to an attached monitor at the top of the computer with x and y being its starting point, t being the text, tc and bc are text color and background color. But im having an issue. When i open the interactive lua prompt, i load the api, and then type button.spawn(1,1,test,colors.white,colors.black) i get an error: "button:6: attempt to concatenate string and nil". As my research goes, i could not find solution to it as of now. Thats im asking you!

Link to the script:http://pastebin.com/kLwAFZYZ
Link to the world:https://www.google.com/

The only time IT WORKS is when i set x,y and t to NUMBER values. Aka
button.spawn(1,1,5,colors.white,colors.black)


button.spawn(1, 1, test, colors.white, colors.black)

Assuming that 't' is the text to be shown, you're passing a non-existent variable named 'test' here.
You need to mark it as a string:


button.spawn(1, 1, "test", colors.white, colors.black)

I recommend changing this:

m.write(""..t)

to:

m.write(tostring(t))
thanks! but whats the real difrience between m.write(""..t) and m.write(tostring(t))?
Lignum #5
Posted 06 March 2014 - 10:29 AM
thanks! but whats the real difrience between m.write(""..t) and m.write(tostring(t))?
m.write("" .. t) is pretty much the same as just m.write(t). It will convert t to a string if it's a simple type, like a number. However, if something more complicated, that can't be converted to a string easily, is passed, like a table or nil, it will just error. While tostring tries to represent what it receives as a string e.g.: tostring(nil) becomes "nil", tostring({ "some", "table" }) becomes "table: <memory address>".
CometWolf #6
Posted 06 March 2014 - 10:30 AM
Nothing, since concatnating a string and a number would result in a string regardless. write also dosen't care if it's a string or a number anyways.

edit: wasn't aware it represented datatypes. Im too used to not caring, since print does it auto anyways :P/>
Edited on 06 March 2014 - 09:34 AM
KingOfAllChunks #7
Posted 06 March 2014 - 10:45 AM
thanks! but whats the real difrience between m.write(""..t) and m.write(tostring(t))?
m.write("" .. t) is pretty much the same as just m.write(t). It will convert t to a string if it's a simple type, like a number. However, if something more complicated, that can't be converted to a string easily, is passed, like a table or nil, it will just error. While tostring tries to represent what it receives as a string e.g.: tostring(nil) becomes "nil", tostring({ "some", "table" }) becomes "table: <memory address>".
good to know