How to get Rawget differ t.key=info and t["key"]=info

Discuss any general programming issues here
Post Reply
Bugala
Posts: 1400
Joined: Sun Feb 14, 2010 7:11 pm

How to get Rawget differ t.key=info and t["key"]=info

Post by Bugala »

I have these two different lines in my code:

Code: Select all

If RawGet(receivedcreaturedata, "AI")                    Then tempcreaturedata.AI                    =      receivedcreaturedata["AI"]

If RawGet(receivedcreaturedata, AI)                    Then tempcreaturedata.AI                    = receivedcreaturedata.AI
if i put right after these:

Code: Select all

DebugPrint(receivedcreaturedata.AI)
it shows there is string in there.

However, if i use:

Code: Select all

If RawGet(receivedcreaturedata, "AI")                    Then debugprint("1")

If RawGet(receivedcreaturedata, AI)                    Then debugprint("2")
neither of these happen.


I suppose I am using RawGet wrong way.


Reason for these two different lines is because I am using TILED and I am wanting the lua code it puts out to be usable without any modifications. However, I have been using different system in my code for storing data, which i think its clearer, and hence I did these two different lines depending wether data is coming from my own code, or from TILED.


Data is in following ways.

TILED uses following:

Code: Select all

TABLE = { ["NAME"] = data, ["AI"] = data, ["X"] = data, ["Y"] = data...}
My own code uses following:

Code: Select all

TABLE = { NAME=info, AI=data, X=data, Y=data...}

By otherwords, TILEDs data is accessible through TABLE["NAME"] and mine is accessible through, TABLE.NAME


RawGet works fine when getting data from TILED, but how do i make it also work when it comes from my own code.


By otherwords, what is the right usage of RawGet to see if table.name exists? Obviously it is not RawGet(table, name)
Bugala
Posts: 1400
Joined: Sun Feb 14, 2010 7:11 pm

Re: How to get Rawget differ t.key=info and t["key"]=info

Post by Bugala »

I got forward with this problem. It seems the problem is in using CAPITALICED LETTERS when using Rawget. Seems it demands using small letters.


However, could someone please explain me about this a bit.

First of all. Have i understood right that you cant use rawget to check for example "HP" but you have to check "hp".

And another thing. Is it so that table.name and table["name"] are one and same thing?
Bugala
Posts: 1400
Joined: Sun Feb 14, 2010 7:11 pm

Re: How to get Rawget differ t.key=info and t["key"]=info

Post by Bugala »

Even more, It seems sometimes it accepts CAPITALICED LETTERS, and sometimes not. Is there any logic into when capitaliced letters are accepted and when not, or shoudl i just use small letters always (makes code harder to read)?
User avatar
airsoftsoftwair
Posts: 5887
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: How to get Rawget differ t.key=info and t["key"]=info

Post by airsoftsoftwair »

This is a known bug that has been there since Hollywood 2.0. If you use the "." directive to access table items, it doesn't matter what kind of spelling you use. However, when you use RawGet() or the [".."] directive you need to use lower case strings because internally all table items are stored in lower case format. It is planned that Hollywood does this automatically but it's actually quite tricky to implement because it concerns quite lowlevel functionality of the lua vm.
Bugala
Posts: 1400
Joined: Sun Feb 14, 2010 7:11 pm

Re: How to get Rawget differ t.key=info and t["key"]=info

Post by Bugala »

As I am trying to use TILEDs lua file without changes, I am asking this still.

I currently have

table["LVL"] coming from LUA.

If i then use RawGet(table, "lvl")

it will work, right?

Or should i change table["LVL"] to table["lvl"]?
User avatar
airsoftsoftwair
Posts: 5887
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: How to get Rawget differ t.key=info and t["key"]=info

Post by airsoftsoftwair »

Bugala wrote:As I am trying to use TILEDs lua file without changes, I am asking this still.

I currently have

table["LVL"] coming from LUA.

If i then use RawGet(table, "lvl")

it will work, right?
No, the [..] directive and RawGet() are both case sensitive. So the spelling needs to be exactly the same for these two. If you use the "." directive, Hollywood will always convert the string to lower case first, i.e. table.lvl, table.LVL, table.LvL, table.lVl all refer to the table["lvl"] whereas if you use [..] or RawGet() they will all point to individual table entries, i.e. table["lvl"] and table["LVL"] are entirely different entries.
Post Reply