1. In my OnLoad function I have included the line

    ThisRealm=GetRealmName()

    However, when I try to use ThisRealm in my code, I keep getting the message that it is nil. (yes I have spelt it correctly, lol)

    I am trying to produce a table with three fields, ie

    Table[ThisRealm][ItemName][AuctionHousePrice]

    This (a simplified version) will allow me to keep track of item prices in the AH for each realm that I play on, because prices can vary wildly from realm to realm.

    I'm currently working through other Auction House addons to try and see how they do it, but any help would be greatful. Thanks in advance.

    I know of no cases in which that API would return nil, so there must be some other issue. Please post the code and error messages.

    Thanks for your reply. My code is several hundred lines long, and you have far better things to do than wade through that lot, but the following example shows my problem:

    The XML defines a single frame, with one button and one fontstring, and the LUA code is:

     local abiRealm
    
     function Frame1_OnLoad()
        abiRealm=GetRealmName()
     end
    
     function Button1_OnClick()
        FontString1:SetText(abiRealm.."/1")
     end
    

    I use Swatter to trap errors, and clicking the button gives the error message: Frame.lua line 8: attempt to concatenate upvalue 'abiRealm' (a nil value).

    Thanks for your reply. My code is several hundred lines long, and you have far better things to do than wade through that lot, but the following example shows my problem:

    The XML defines a single frame, with one button and one fontstring, and the LUA code is:

     local abiRealm
     
     function Frame1_OnLoad()
      abiRealm=GetRealmName()
     end
     
     function Button1_OnClick()
      FontString1:SetText(abiRealm.."/1")
     end
    

    I use Swatter to trap errors, and clicking the button gives the error message: Frame.lua line 8: attempt to concatenate upvalue 'abiRealm' (a nil value).

    Add a print statement to your OnLoad. I suspect that it is not being called. Print the results of GetRealmName() as well, without concatenating it. Something like this:

     function Frame1_OnLoad()
       print("OnLoad happening")
       abiRealm = GetRealmName()
       print("Got realm", abiRealm)
     end
    

    See what happens.

    I have changed the OnLoad function to contain the three lines you suggest. On loading, the message "OnLoad Happening" is displayed, along with "Got realm Ghostlands".

    Clicking the button still produces the error message though.

    Is the function which is called when you click the button in the same lua file as the frame's OnLoad function? Just making sure :p

    Yes - just the one LUA file, containing the local declaration and both functions :) But for some reason it just doesn't want to work when you press the button.

    Without the code I can tell you nothing, the API function obviously works properly, so there is a logic issue in your program.

    The code is exactly as show in the above examples, ie:

     local abiRealm
     function Frame1_OnLoad()
        print("OnLoad happening")
        abiRealm=GetRealmName()
        print("Got realm", abiRealm)
     end
     function Button1_OnClick()
        FontString1:SetText(abiRealm.."/1")
     end
    

    abiRealm works fine in the Frame1_OnLoad function, but has a value of nil in the Button1_OnClick function. I've tried using abiRealm as a global variable, the code being:

     abiRealm=GetRealmName()
     function Frame1_OnLoad()
        print("OnLoad happening")
        print("Got realm",abiRealm)
     end
     function Button1_OnClick()
        FontString1:SetText(abiRealm.."/1")
     end
    

    Doing it this way works perfectly, which leads me to believe that the local variable is not being handled correctly. I was hoping not to have to use global variables, but it looks as though I may have to in this case.

    Thanks for your time and suggestions, it is most appreciated that you are prepared to help us out when we get stuck.

    Hence why I have asked to see the code. Unless it is exactly those lines in exactly that order and absolutely nothing else, then I need to see the code, as I've said.

    The scoping rules of Lua are quite clear. What you have should work. It doesn't, which means there is an error somewhere in your code, which I have (again) asked to see. Please pastey the code.

    Hi again.

    The code is exactly those few lines, in exactly that order, with nothing else. I actually pasted the lines from the LUA file to make absolutely certain that I showed you the correct code.

    I'm not going to answer unless you post the code. You could be altering the return value at some other place in the code. You could be shadowing it with another variable declaration.

    Post the code. All of it. Post the error message. The exact error message, in full. I provide help.

    I'm sorry for being short, but I do not have time for games. I'm here attempting to help you and I have nicely asked for something over and over again.

    Im sorry if you think I'm playing games. Whenever I try some new code or API functions, I always write a small test addon first to check results etc, so my main addon has not been updated with any 'realm code'. The only code I've written to examine the realm name is the 9 lines above.

    Having said that, your replies have prompted me to try different ways of writing the code, and I've come up with:

     local abiRealm=GetRealmName()
     function Frame1_OnLoad()
        print("OnLoad happening")
        print("Got realm", abiRealm)
     end
     function Button1_OnClick()
        FontString1:SetText(abiRealm)
     end
    

    which seems to work perfectly. I've no idea why this should work and the previous method not work, but as you pointed out there are rules reguarding the use of API, and I've obviously missed something somewhere.

    Sorry again for being a pain in the backside, it wasn't deliberate and I wasn't witholding the main addon code from you. Thanks for your time once more.

    I would still like to see the code. I can tell you exactly why the error is occuring, I just need the code. If you don't want to share it on a public forum, then you can email it to me, but I still feel it would be in everyone's best interest if you just post the code as it was so I can look at it and give you an answer.

    I mean, I'm glad its working for you, but I could have given you an answer quite some time ago if you had just posted the code =)