Welcome to AC Web.
Page 1 of 3 123 LastLast
Results 1 to 15 of 32
  1. #1
    Friendly criticism.

    Join Date
    Mar 2009
    Location
    Australia!
    Posts
    1,582

    [Tutorial] Writing to a File With Lua


    REGISTER! (FREE)
    Registered members see less ads
    and also gain access to other great features.
    Hey everyone, I decided I would share some of my knowledge of Lua, so I thought a bit and chose that I'd make a tutorial on file input/output with Lua.
    Without further ado, we shall begin...


    In order to write to a file in Lua, we must first have the actual file itself created.

    In this tutorial I'll be using a file named example.txt.


    To write to my file, I first must actually 'open' the file in Lua. Note: the file isn't actually opened when you do this.

    Code:
    local file = io.open("example.txt", "w")
    The above opens the file example.txt in 'write' mode. So now, the pointer for the file is a local variable called file.
    Note: Write mode will overwrite any text in the file each time the variable - file is called upon.


    Now we want to actually write something to the file. So we use this:

    Code:
    file:write("Lalala. Alvanaar is amazing.")
    That function writes the text in red to our file - file ('example.txt'). Easy.


    We then need to close the file:

    Code:
    file:close()

    Our file example.txt will now look like this:

    Quote Originally Posted by example.txt
    Lalala. Alvanaar is amazing.

    And there you go. That's how you write to a file with Lua. Very simple.

    Below is a list of functions, etc you can use for file i/o.


    Basic file operations and useful symbols, etc:

    Code:
    local file = io.open("filename", "mode")
    	- file is now the file - "filename"
    	- mode: "w" = write mode (all file text is erased)
    	- mode: "a" = append mode (used for adding onto the end of the file)
    file:write("text")
    	- writes "text" to file
    file:flush()
    	- saves the file - file
    file:close()
    	- 'closes' and saves the file - file
    \n
    	- represents a break (like pressing the enter key or return key)
    		o e.g. file:write("Hi\nHow are you?") will appear like so:
    			Hi
    			How are you?


    PLEASE ASK QUESTIONS!


    Enjoy,

    ~Alvanaar
    Last edited by Alvanaar; 10-29-2009 at 10:46 AM.

  2. #2
    I'm not here often

    Join Date
    May 2009
    Location
    Dublin, Republic of Ireland
    Posts
    3,742
    Bump! Very nice tutorial. +7 (:

  3. #3
    Really nice, didnt knew this! +rep.
    Last edited by Grandelf1; 10-22-2009 at 12:33 PM.

  4. #4
    is this for WoW?

  5. #5
    Great tutorial, but what about reading from files? Or is it just?
    Code:
    dofile(PATH_TO_FILE)
    (Looked at your GHS script and tried to learn something)

    Great tutorial +rep.

  6. #6
    Friendly criticism.

    Join Date
    Mar 2009
    Location
    Australia!
    Posts
    1,582
    dofile is to load a file for use in the script it is called in.

    Actually reading text from files requires iterating over the lines in a for loop.


    @nadskap, it's for anything. It's a Lua function, not one that's been created by ArcEmu.

  7. #7
    I'm not here often

    Join Date
    May 2009
    Location
    Dublin, Republic of Ireland
    Posts
    3,742
    Note that you do not have to do dofile(DEFINED_VARIABLES) for a defined variables file to load. All variables inside such files are global and, as such, they 'leave' to join other scripts.

  8. #8
    Friendly criticism.

    Join Date
    Mar 2009
    Location
    Australia!
    Posts
    1,582
    Solid moderating, moderators. That post has existed for over 24 hours. :|

  9. #9
    Quote Originally Posted by Alvanaar View Post
    Solid moderating, moderators. That post has existed for over 24 hours. :|
    It's called reporting.

    - Nice tutorial.

  10. #10
    Friendly criticism.

    Join Date
    Mar 2009
    Location
    Australia!
    Posts
    1,582
    True. Solid thinking, Alvy.

    Thanks. Bump.

  11. #11
    Hardstyle Music <3

    Join Date
    Mar 2009
    Location
    Under your bed :3
    Posts
    699
    Epic! =D

  12. #12
    When using "append" for the io.open does it write EXACTLY at the end of the file?
    eg:
    Code:
    file:write("Heyhey hey")
    file:write("Hey hey hey")
    file:
    Code:
    Heyhey heyHey hey hey

  13. #13
    So could I integrate this into a boss, so that the boss would send Raid Statistics to a file. Such as # of players, Damage Done, highest hit by player, etc?

    If so this would be extremely useful.

    +rep for the effort to help the community anyways =D

  14. #14
    I piss excellence

    Join Date
    Jul 2009
    Location
    Crater Dawes
    Posts
    11,402
    Quote Originally Posted by candyisgood View Post
    So could I integrate this into a boss, so that the boss would send Raid Statistics to a file. Such as # of players, Damage Done, highest hit by player, etc?

    If so this would be extremely useful.

    +rep for the effort to help the community anyways =D
    Yes, but it would be a lot of coding.

  15. #15

    REGISTER! (FREE)
    Registered members see less ads
    and also gain access to other great features.
    Alot of coding is fine with me. Thanks for your feedback =]

    +Rep

 

 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •