Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Neverwinter Nights: Enhanced Edition has been released! Visit nwn.beamdog.com to make an order. NWN:EE FAQ is available.
Soundtracks for BG:EE, SoD, BG2:EE, IWD:EE, PST:EE are now available in the Beamdog store.
Attention, new and old users! Please read the new rules of conduct for the forums, and we hope you enjoy your stay!

Confusing order of custom list

BubbBubb Member Posts: 376
Hi everyone,

I have recently been tinkering around with expanding the vanilla cheat menu, and I seem to be having a very weird behavior. I'll try to explain what I am trying to do:

Basically, I am trying to create a list that displays all the resrefs and names of all the creatures in the game. So far it's been easy enough; I've written a very simple WeiDU script that populates a M_ lua file with all the relevant information... but for some odd reason I'm hitting a wall with the actual list implementation. It seems that the in-game list is displaying the elements of the table out of order, even when the table is already sorted in the M_ lua file. I'm probably doing something stupid here, but I've been staring at this for an hour or two now, and just can't find the (what should be very simple) error.

I've attached my override folder so you guys can look at the code, (the files are for BG2EE). The problematic list starts at line 237 of UI.MENU. You can view the list in-game by hitting ctrl+space, and pressing the "Browse Creatures" button in the cheat menu. I expect the resrefs to be displayed alphabetically, as that is the order they are presented in the M_ChtMen.lua file, but alas they seem to be in a completely random order. I've rigged the clicking of a row to output the resref of the row in the feedback area, and for some odd reason THIS provides the correct feedback, while the list itself is wrong.

Any help would be greatly appreciated!

Comments

  • kjeronkjeron Member Posts: 1,465
    I don't know why that method doesn't work (because I don't know much about LUA and have had that same issues myself), but this does keep them in order:
    creInfo = {
    	{
    		res = '25SPELL',
    		longName = 'Lazarus Librarus',
    	},
    	{
    		res = 'AATAQAH',
    		longName = 'Aataqah',
    	},
    	{
    		res = 'ABAZIGAL',
    		longName = 'Abazigal',
    	},
    	{
    		res = 'ABISRED1',
    		longName = 'Abishai',
    	},
    	{
    		res = 'ABYDEM01',
    		longName = 'Tanar\'ri',
    	},
    	{
    		res = 'ACOLYTE1',
    		longName = 'Talon Zogas',
    	},
    	{
    		res = 'ACOLYTE2',
    		longName = 'Dawnbringer Alvanna',
    	},
    	{
    		res = 'ACOLYTE3',
    		longName = 'Watchknight Aabir',
    	},
    	{
    		res = 'AEEXTORT',
    		longName = 'Hanj',
    	},
    	{
    		res = 'AEGNOLL',
    		longName = 'Gnoll',
    	},
    }
    omitting the '[#] = ' for each entry
    as does this:
    creInfo = {}
    table.insert(creInfo, {res = '25SPELL',longName = 'Lazarus Librarus',})
    table.insert(creInfo, {res = 'AATAQAH', longName = 'Aataqah',})
    table.insert(creInfo, {res = 'ABAZIGAL', longName = 'Abazigal',})
    table.insert(creInfo, {res = 'ABISRED1', longName = 'Abishai',})
    table.insert(creInfo, {res = 'ABYDEM01', longName = 'Tanar\'ri',})
    table.insert(creInfo, {res = 'ACOLYTE1', longName = 'Talon Zogas',})
    table.insert(creInfo, {res = 'ACOLYTE2', longName = 'Dawnbringer Alvanna',})
    table.insert(creInfo, {res = 'ACOLYTE3', longName = 'Watchknight Aabir',})
    table.insert(creInfo, {res = 'AEEXTORT', longName = 'Hanj',})
    table.insert(creInfo, {res = 'AEGNOLL', longName = 'Gnoll',})

    Bubb
  • BubbBubb Member Posts: 376
    edited April 2018
    @kjeron
    That was it! I never would have guessed that it was in the M_ lua file; I thought for sure it was something in UI.MENU. I experimented a bit with table order, and it seems that lua's pairs function honors the order if no keys are present in the table declaration, but will not honor the order if the keys are explicitly stated (just like you said). Very weird behavior, to say the least. Maybe by declaring the keys, lua is treating the table as a dictionary instead of an array? Anyways, thanks for your help!

    Edit: It seems I was partially right. I was searching around a bit for more clarification on why this behavior was occurring, and I came across this article: lua-users.org/wiki/TableConstructors. If you look at the bytecode difference between the {1, 2, 3} syntax and the {[1] = 1, [2] = 2, [3] = 3} syntax, you will see that the former generates a SETLIST call, while the latter generates a SETMAP call. Turns out the syntax I was using was generating an unordered map instead of a list; an interesting behind the scenes difference between the syntaxes, especially since they are stated as being identical in the official lua docs.

    Post edited by Bubb on
Sign In or Register to comment.