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

file handle writes {} to file insted of the inputed content

Started by Ta©ti_Tac0Z, 18 May 2018 - 02:11 PM
Ta©ti_Tac0Z #1
Posted 18 May 2018 - 04:11 PM
code:

local path = "/.VIEW_TMP"
local file = fs.open(path, "w")
file: write("this is truned of right now")
file: close()

when i try to see in the file then the content (does matter what string i give it) all ways is "{}" evrey time?
why this? this is.

this is the only thing useing fs api in my program so i don't fell like showing the hole program here, besides the program is 500 lines long
Bomb Bloke #2
Posted 19 May 2018 - 02:12 AM
When you make use of : within a function call, this sort of code:

myTable:myFunc(arg1, arg2, arg3, ...)

… expands out to:

myTable.myFunc(myTable, arg1, arg2, arg3, ...)

The "abbreviation" offered by colon notation is a shortcut for coders who use the "object orientated" style of coding.

So in your case, the first argument you're passing to write() is your file table. ComputerCraft translates that to text as best it can and writes it to the file - since the table only contains function pointer values (which aren't much use when serialised), you just get an empty table declaration - "{}".

Switch the :'s to .'s.
SoruCoder #3
Posted 19 May 2018 - 10:04 PM
And also, the syntax for using the object-oriented function is:

object:func()
Not:

object: func()
SquidDev #4
Posted 19 May 2018 - 10:14 PM
And also, the syntax for using the object-oriented function is:
To be fair, they're all equally valid - whitespace is pretty insignificant in Lua. Which is why you can get away some fabulous things like:

x : y ( ) x
: y ( ) x :
y ( ) x : y
( ) x : y (
) x : y ( )