Another lua question!

Another lua question!

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
User avatar
Tribulex
A.N.T.S. Developer
Posts: 1894
Joined: 26 Sep 2009, 21:26

Another lua question!

Post by Tribulex »

Thank you for solving my first problem. Here is my second problem:

I have a texture and a config value with 4 values. For clarity lets call those values north, east, south, and west.

These values will divide the texture into a 3x3 grid, where the first row is north pixels high, last row is south pixels high, first column is west pixels wide, and the last column is east pixels wide. I want to be able to draw these textures on the screen individually, and i want to do so with scaling.

basically, it so I can make a texture for the background of a gui element with the borders scaling correctly.


sorry for english,
d_b



OLD POST:



I have this table:

Code: Select all

local morphDefs = {
    ant= {
        [1] = {into = 'mantis', metal = 100, energy = 0, time = 5,},
    },
    ...
}
I want to turn it into this:

Code: Select all

{
    [1] = {[2], 100}, 
    ...
}
where 1 is the udef id of 'ant' and 2 is udef id of 'mantis' and 100 is the metal characteristic. Can someone write some code to do this?

Pie and cake,
d_b
Last edited by Tribulex on 24 Oct 2009, 10:32, edited 2 times in total.
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Re: Incredibly confused and frustrated as usual [TABLES/ARRAYS]

Post by AF »

So you want an array of tables? Aren't tables and arrays the same thing in lua?
User avatar
lurker
Posts: 3842
Joined: 08 Jan 2007, 06:13

Re: Incredibly confused and frustrated as usual [TABLES/ARRAYS]

Post by lurker »

Wow, you wrote that confusingly.

Code: Select all

pieCakeDefs = {}
for defName, mDef in pairs(morphDefs) do
   local defID = UnitDefNames[defName].id
   local intoID = UnitDefNames[mDef.into].id
   pieCakeDefs[defID] = {intoID, mDef.metal}
end
Note that this code will crash if you have any names wrong. Adding error checking is left as an exercise to the reader.
User avatar
Tribulex
A.N.T.S. Developer
Posts: 1894
Joined: 26 Sep 2009, 21:26

Re: Incredibly confused and frustrated as usual [TABLES/ARRAYS]

Post by Tribulex »

i dont know what to do to convey to everyone including yourself that you are epically awesome mr. lurker. pie and cake!!!!
User avatar
Tribulex
A.N.T.S. Developer
Posts: 1894
Joined: 26 Sep 2009, 21:26

Re: Incredibly confused and frustrated as usual [TABLES/ARRAYS]

Post by Tribulex »

lurker wrote:Wow, you wrote that confusingly.

Code: Select all

pieCakeDefs = {}
for defName, mDef in pairs(morphDefs) do
   local defID = UnitDefNames[defName].id
   local intoID = UnitDefNames[mDef.into].id
   pieCakeDefs[defID] = {intoID, mDef.metal}
end
Note that this code will crash if you have any names wrong. Adding error checking is left as an exercise to the reader.

wait wait wait this wont work for this:

Code: Select all

local morphDefs = {
    ant= {
        [1] = {into = 'mantis', metal = 100, energy = 0, time = 5,},
        [2] = {into = 'anklet', metal = 200, energy = 0, time = 5,},
        [3] = {into = 'aviantor', metal = 300, energy = 0, time = 5,},
        [4] = {into = 'dirigantible', metal = 400, energy = 0, time = 5,},
    },
    ...
}
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: Incredibly confused and frustrated as usual [TABLES/ARRAYS]

Post by Argh »

Code: Select all

local morphDefs = {
    ant= {
        [1] = {into = UnitDefNames[mantis].id, metal = 100, energy = 0, time = 1},
    },
    ...
}
User avatar
lurker
Posts: 3842
Joined: 08 Jan 2007, 06:13

Re: Incredibly confused and frustrated as usual [TABLES/ARRAYS]

Post by lurker »

Yeah, I tend to do that when I'm not paying attention. My code only supports single-option morphs. I did make a mistake in reading your code, because it looks like you're asking for something that doesn't work.

You want this when you only have a single option, right?

Code: Select all

{
    ant-id = {into-id, 100}, 
    ...
}
So, if you have multiple morphs:

Code: Select all

{
    ant-id = {into-id-1, 100},
    ant-id = {into-id-2, 100}, 
    ...
}
Which just overwrites the first one with the second one.


Unless you actually want:

Code: Select all

{
    ant-id = {
        {into-id-1, 100},
        {into-id-2, 100}, 
    }
    ...
}
Please show me what you want your output to look like *with multiple morph options*.
And I'm not sure what Argh's doing.
User avatar
Tribulex
A.N.T.S. Developer
Posts: 1894
Joined: 26 Sep 2009, 21:26

Re: Incredibly confused and frustrated as usual [TABLES/ARRAYS]

Post by Tribulex »

lurker wrote:Yeah, I tend to do that when I'm not paying attention. My code only supports single-option morphs. I did make a mistake in reading your code, but that's because I can't tell what you want.
You want this, right?

Code: Select all

{
    ant-id = {into-id, 100}, 
    ...
}
So, if you have multiple morphs:

Code: Select all

{
    ant-id = {into-id-1, 100},
    ant-id = {into-id-2, 100}, 
    ...
}
Which just overwrites the first one with the second one.

Unless you actually want:

Code: Select all

{
    ant-id = {
        {into-id-1, 100},
        {into-id-2, 100}, 
    }
    ...
}
Please show me what you want your output to look like *with multiple morph options*.
And I'm not sure what Argh's doing.
I want the second please. No one knows what argh's doing.
User avatar
lurker
Posts: 3842
Joined: 08 Jan 2007, 06:13

Re: Incredibly confused and frustrated as usual [TABLES/ARRAYS]

Post by lurker »

Code: Select all

pieCakeDefs = {}
for defName,morphs in pairs(morphDefs) do
   local defID = UnitDefNames[defName].id
   local cake = {}

   for i, mDef in ipairs(morphs) do
      local intoID = UnitDefNames[mDef.into].id
      cake[i] = {intoID, mDef.metal}
   end
   pieCakeDefs[defID] = cake

end
User avatar
Tribulex
A.N.T.S. Developer
Posts: 1894
Joined: 26 Sep 2009, 21:26

Re: Incredibly confused and frustrated as usual [TABLES/ARRAYS]

Post by Tribulex »

lurker wrote:

Code: Select all

pieCakeDefs = {}
for defName,morphs in pairs(morphDefs) do
   local defID = UnitDefNames[defName].id
   local cake = {}

   for i, mDef in ipairs(morphs) do
      local intoID = UnitDefNames[mDef.into].id
      cake[i] = {intoID, mDef.metal}
   end
   pieCakeDefs[defID] = cake

end
Thank you very much...
User avatar
Tribulex
A.N.T.S. Developer
Posts: 1894
Joined: 26 Sep 2009, 21:26

Re: New Lua Problem that i have no clue how to approach

Post by Tribulex »

i updated the first post btw. I need a way of displaying a rectangular section of a texture. People keep saying "texrect" and then ignoring me, but thats not what i need. I want to display a fragment or part of the texture which is rectangular, not the whole texture.

Thank you.
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: New Lua Problem that i have no clue how to approach

Post by Argh »

TexRect accepts texture coordinates as parameters you're allowed to pass (try to imagine texture coordinates as an arbitrary quad you place over your texture, even outside its bounds). This will allow you to build your offset and show a non-square area sample from a texture.

So, for example, gl.TexRect(0,0,600,600,0,0,0.5,0.5) would render your TexRect with one quarter of your texture used to cover the whole quad.

Anyhow, hope that helps.
User avatar
Tribulex
A.N.T.S. Developer
Posts: 1894
Joined: 26 Sep 2009, 21:26

Re: New Lua Problem that i have no clue how to approach

Post by Tribulex »

Argh wrote:TexRect accepts texture coordinates as parameters you're allowed to pass (try to imagine texture coordinates as an arbitrary quad you place over your texture, even outside its bounds). This will allow you to build your offset and show a non-square area sample from a texture.

So, for example, gl.TexRect(0,0,600,600,0,0,0.5,0.5) would render your TexRect with one quarter of your texture used to cover the whole quad.

Anyhow, hope that helps.
YAY SUPER SUPER HELPFUL GUY!!!!!!!!! YOU WIN!!!!!!!!!!
User avatar
Tribulex
A.N.T.S. Developer
Posts: 1894
Joined: 26 Sep 2009, 21:26

Re: Another lua question!

Post by Tribulex »

How do i translate this command in cmdcolors.txt:

Code: Select all

alwaysDrawQueue   1
into something i can control in my mod. I would imagine there is some lua trickery that can help, but i don't know. Can someone give help/advice please?
Post Reply

Return to “Lua Scripts”