Jump to content

[No more]errors during my first table experiment


Recommended Posts

As you could have read in a recently answered topic, Alexander said it would be a performance improvement to not constantly read from a XML file. Tables should be more efficiently.

So I decided to try something out with these tables. Armed with a Lua tutorial I tried out some things and learned about tables for a bit.

Now I have 3 problems:

1. The table WRITE could be wrong.

2. The table READ could be wrong.

3. I dunno which is wrong. :S

So well, I recently learned to work with XML, and thought that would be a perfect test. It's an accountdata system, currently only containing money data and username data, but if this works it would be very likely to be expanded!

So here are some pieces of code, the accountdat file, and, ofcourse, the error.

accountdat.xml:

<cash> 
   <user name="Gamesnert" money="10000"/> 
   <user name="Someothersucker" money="1"/> 
</cash> 

Table creation code:

function xmlLoadInTable() 
    local xmlFile=xmlLoadFile("accountdat.xml") 
    if xmlFile then 
        i=0 
        dataNode=true 
        while dataNode~=false do 
            dataNode=xmlFindSubNode(xmlFile,"user",i) 
            if dataNode then 
                local xmlReadName=xmlNodeGetAttribute(dataNode,"name") 
                local xmlReadMoney=xmlNodeGetAttribute(dataNode,"money") 
                accountdat[xmlReadName]={name=xmlReadName, money=xmlReadMoney} 
            else 
                outputDebugString("The while loop had reached " .. i) 
            end 
            i=i+1 
        end 
        xmlUnloadFile(xmlFile) 
    end 
end 
addEventHandler("onResourceStart",getResourceRootElement(getThisResource()),xmlLoadInTable) 

The read, intended to check if the saving works. But I still don't know which is wrong... :

function readTableLine(thePlayer,command,target) 
    outputDebugString(accountdat[target].name .. ", " .. accountdat[target].money) 
end 
addCommandHandler("output",readTableLine) 

(Note: you can and must use playernames now! Improvement so I don't have to use loops to find the right string all the time :P)

(the ex)Error!

ERROR: :78(2 in the readscript): attempt to index field '?' (a nil value)

I don't know why he starts about a ?... I just type the command "output 0" or "output 1" in the console, and they both give this error... Why?

Edited by Guest
Link to comment

Ok, I changed:

-accountdat to accountdat[i+1].

-The read attributes are now printed in the server window.

Now comes something expected: (atleast by me) The XML system works fine... It must be in the writing or reading. So every line which involves writing and reading the table has more chance of being the problem than any other line... Anyone can see the problem with that info?

Link to comment

Ok all nevermind! Found the problem!

The readsystem seemed to act a little weird... I entered a linenumber, but apparently something was wrong with that... In the check code try to replace "line" with "1". Works for me. But I still do not understand what was wrong in those... 4 lines... Only "line" seemed to be wrong... Although the importance now is VERY low, I would like to know what I did do wrong. If anyone knows why that was wrong, please for... Eh... My sake. :P Tell me.

EDIT: I'll edit my first post, to replace the non-working script with the working one. Just for other people so they might learn from it. (MIGHT learn from it :P)

Link to comment

Even bigger question is: Why in the heck would you even use an .xml file for that kind of data? If one day you had a huge playerbase, it would bog your server down like something crazy to start up. An SQL database with that kind of information would be much more efficient, and you don't have to load/read the whole thing from a file when the resource starts. XML should be used only for semi-static data that is only customized/changed via a human being from outside of the game. (Such as gamemode settings and that kind of thing, being opened/changed with an XML editor or notepad or something.) Then again, that's just my stupid opinion and I am legally retarded by several states' definitions.

Link to comment

Having ADHD does not mean someone is retarded.

The word is just an invention of some idiots who think the human brain is not capable of debugging hardware by itself without the use of medication.

Back to topic:

In this case, SQL would be preferable.

At least, keep that in mind and use re-usable functions that can be changed into SQL functions easily.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...