Page 1 of 1

Comparing color tables and GUIStyle

Posted: Wed Nov 04, 2015 5:48 am
by MisterBrownZA
Greetings,

I would like to know the following:

1. How would I be able to compare the current player color with a color table of my own (granted my own table has the full rgba).
eg.

Code: Select all

if (game.player.color == mycolors.white || game.local_player.color == mycolors.white) then
--#change player color
2. What is the best way to iterate through my table ?
eg.

Code: Select all

If (game.player.color in mycolors) then
--#true/false
3. I have no idea how to work with guistyle.
I created this:

Code: Select all

data.raw["gui-style"].default["my_label"] = 
{
	type="label_style",
	parent="label_style",
	color={r=1,g=1,b=1},
	fontcolor={r=0,g=0,b=0}
}

Code: Select all

path["flow"..ii].add({type="label",name="label"..ii, caption="caption", stype="my_label"})
However, whenever I try and use it in my code, it gives me an error stating that my_label doesn't exist... Can someone please explain this to me ? The wiki of guistyle only states the properties, but doesn't give a default value, min/max value, or any form of example of implementation.
4. I tried doing inline styles, and it will tell me that the key value doesn't exist
eg.

Code: Select all

player.gui.top.frame["flow"..ii]["label"..ii].style.fontcolor 	= {r=1,g=1,b=1}

Re: Comparing color tables and GUIStyle

Posted: Wed Nov 04, 2015 9:13 am
by prg
MisterBrownZA wrote:Greetings,

I would like to know the following:

1. How would I be able to compare the current player color with a color table of my own (granted my own table has the full rgba).
eg.

Code: Select all

if (game.player.color == mycolors.white || game.local_player.color == mycolors.white) then
--#change player color
The player color is also rgba, but game.local_player.color == mycolors.white will only ever be true if both colors are exactly the same table, not just tables that happen to contain the same values. You could try something like

Code: Select all

function compare_colors(c1, c2) return c1.r == c2.r and c1.g == c2.g and c1.b == c2.b and c1.a == c2.a end
But since the color values are floats you might want to see if they are close enough together instead of exactly the same.
MisterBrownZA wrote:2. What is the best way to iterate through my table ?
eg.

Code: Select all

If (game.player.color in mycolors) then
--#true/false

Code: Select all

for _, v in pairs(mycolors) do
    if compare_colors(game.local_player.color, v) then
        --color found
        break
    end
end
MisterBrownZA wrote:3. I have no idea how to work with guistyle.
Never done anything with that, either.

Re: Comparing color tables and GUIStyle

Posted: Wed Nov 04, 2015 10:17 am
by MisterBrownZA
-snip-
Never done anything with that, either.[/quote]
Thanks for the assistance.

Was wondering about the table values as well... Will compare values instead then, thank you very much!

GUIstyle isn't documented very well, and I can't find a solid working example of implementation.

I have tried downloading some GUI's that are currently available, but when I try copy their example, I get the same error.

So I'm not sure what I'm doing wrong.

I'm hoping someone can assist with this!

Re: Comparing color tables and GUIStyle

Posted: Wed Nov 04, 2015 10:35 am
by prg
BTW turns out someone already wrote a deep compare function for tables.

Code: Select all

require "util"
util.table.compare(t1, t2)
Won't work from the console though since you can't use require there. Also won't work if you want to add name="white" or something like that to your own table.

Re: Comparing color tables and GUIStyle

Posted: Wed Nov 04, 2015 11:46 am
by rk84
3. Is that in data.lua file or does data.lua request the file with that code?

You can dig the default values from Factorio\data\core\prototypes\style.lua

Also font_color with an underscore.