+ Reply to Thread
Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By Adelea

Thread: How to load data from another lua-file?

  1. #1
    Soulwalker
    Join Date
    May 2014
    Posts
    8

    Default How to load data from another lua-file?

    Hello,

    I've been trying for hours now to find out how to load data from one lua file to another. To no avail.

    What I want to do:
    I got database.lua that contains
    text1 = "important text 1"
    text2 = "important text 2"

    And in main.lua I want to be able to somehow load the text1 and text2. I've googled around and it says I should use global files, etc, etc. But nothing's working.

    I tried all kinds of loadfile(database) and so forth but still.
    I've gotten all kind of error messages.

    I've checked alot of other addons that seem to use tables to load but I haven't been able to figure them out.

    There must be a simple way to do this but I just can't figure it out. So please help!

  2. #2
    RIFT Community Ambassador the_real_seebs's Avatar
    Join Date
    Jan 2011
    Posts
    16,859

    Default

    I am pretty sure that the addon environment specifically prevents you from doing this.

    If you want to save and load data, look at SavedVariables. It is intentional that you can't load and save data arbitrarily at runtime.
    You can play WoW in any MMO. You don't have to play WoW in RIFT. Oh, and no, RIFT is not a WoW clone. Not having fun any more? Learn to play, noob! I don't speak for Riftui, but I moderate stuff there. Just came back? Welcome back! Here's what's changed. (Updated for 2.5!)

  3. #3
    Shield of Telara Adelea's Avatar
    Join Date
    Mar 2011
    Posts
    734

    Default

    The only way I can think of doing this is:

    In your RiftAddon.toc :

    Code:
    RunOnStartup = 
    {
      "DataFile.lua",
      "CodeFile.lua",
    }
    Then:

    DataFile.lua:

    Code:
    local addon, data = ...
    data.text1 = "important text 1"
    data.text2 = "important text 2"
    CodeFile.lua:

    Code:
    local addon, data = ...
    
    print("Text1 = "..data.text1)
    print("Text2 = "..data.text2)
    This keeps it all compartmentalized. You could define all your text values as globals in the datafile, but that is untidy and opens up the possibility that something else will overwrite them.
    http://forums.riftgame.com/image.php?type=sigpic&userid=125779&dateline=13553  38065

  4. #4
    Soulwalker
    Join Date
    May 2014
    Posts
    8

    Default

    Adelea,

    I think I tried your method before but get this error message:

    Error: EventViewer/main.lua:3: attempt to concatenate field 'text1' (a nil value)
    In EventViewer / main.lua
    stack traceback:
    EventViewer/main.lua:3: in main chunk
    Defining them as globals messes it up as well.

    I think I've seen addons using databases in other lua files. To load text and other simple data but I'm not sure how they actually reach this info.

    I'll try using SavedVariables and see if I can get that working.

    Thanks for the replies!

  5. #5
    Shield of Telara Adelea's Avatar
    Join Date
    Mar 2011
    Posts
    734

    Default

    Are you using them as 'text1' or 'data.text1' ?

    You also must do the local addon, data = ... at the top of your files (you can use diff names ofc, but then need to change data.text1 to whatever.text1

    You also need to make sure that the datafile comes before the code file in the toc.

    This does work - its something I use in a lot of addons!
    http://forums.riftgame.com/image.php?type=sigpic&userid=125779&dateline=13553  38065

  6. #6
    Soulwalker
    Join Date
    May 2014
    Posts
    8

    Default

    It must've been the order in the toc-file. I beg your pardon, I didn't copy all the code completely.

    Seems to be working now. <3

    Thanks alot

  7. #7
    Soulwalker
    Join Date
    May 2014
    Posts
    8

    Default

    Hello again.

    Not sure if I should've created a new thread or not but..

    Anyways. Now I got that working but I'm stuck on the timed event.
    I want to be able to call a function (SetText) every 10 seconds or so. I've checked the LibCron-library but can't get it to work.

    Code I tried using was:
    Library.LibCron.recurring(2, Repeater, ...)

    function Repeater()
    print("test test")
    end
    Tried without ,... and different versions of it all but either I get some error message or this error from LibCron:

    [LibCron] Event for LibCron threw and exception, removing.
    [LibCron] Exception details: attempt to call a nil value
    That nil value seems to be something in the LibCron-library. Perhaps I haven't loaded it correctly. But I can't get it to work though.

    Please help so I can finish this. Lua is quite confusing to me :P

    Cheers!

  8. #8
    Shield of Telara Adelea's Avatar
    Join Date
    Mar 2011
    Posts
    734

    Default

    Try moving your function definition so that it appears BEFORE the call to LibCron.

    lua will use the value of things when they are used - so if your Repeter function hasnt been defined, it will be nil.
    http://forums.riftgame.com/image.php?type=sigpic&userid=125779&dateline=13553  38065

  9. #9
    Soulwalker
    Join Date
    May 2014
    Posts
    8

    Default

    Thanks again Adelea.

    Everything is working now

    Fast replies, good info. Much appreciated.

  10. #10
    Soulwalker
    Join Date
    May 2014
    Posts
    8

    Default

    Okay, wasn't done!

    Now I can get recurring event for example (print).
    And it works the first time it activate and access the DataFile.lua

    function Repeater()
    print("Updating")
    text_zone1:SetText((""..data.text1))
    end
    But after that it doesn't get any new info from the DataFile. Do I have to redraw it somehow?
    It keeps on repeating "Updating" but doesn't update if the info in DataFile.lua changes.

  11. #11
    Shield of Telara Adelea's Avatar
    Join Date
    Mar 2011
    Posts
    734

    Default

    And this is where you hit the brick wall.

    There isnt anyway to get updated data INTO Rift.

    You can update the datafile, but it wont take affect until you either /reloadui or logout/login.
    http://forums.riftgame.com/image.php?type=sigpic&userid=125779&dateline=13553  38065

  12. #12
    RIFT Community Ambassador the_real_seebs's Avatar
    Join Date
    Jan 2011
    Posts
    16,859

    Default

    That's what I mean about "you are very, very, intentionally not allowed to do this".

    It is an intentional design choice that you cannot get "live" updates from external data files. You have to reload the addon environment (and UI in general) to get new data in.
    You can play WoW in any MMO. You don't have to play WoW in RIFT. Oh, and no, RIFT is not a WoW clone. Not having fun any more? Learn to play, noob! I don't speak for Riftui, but I moderate stuff there. Just came back? Welcome back! Here's what's changed. (Updated for 2.5!)

+ Reply to Thread

Bookmarks

Posting Permissions

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