Conditions based on attributes of other objects in BGE

and this ‘jumping around’ is why I would love to have collapsing enabled in the text editor…and yes, I come from a ‘C’ background :)…I really need to stay on topic and I apologize for, yet again, posting with no real response to the thread starter.

Thank you monster, logic and simple.
I didn’t get that I have to use a module insted of a script.
Now there’s one more question based again on the title. How do I compare colors?

I tried something like this to test it out.

(own defined, cont defined)

Green = [0,1,0,True]
Blue = [0,0,1,True]

own.color = Green
#works as always
 if own.color == Green:
    own.color = Blue
else:
    own.color = Green

It becomes green…
Therefore the if statement is wrong. So, I supposed, that own.color is not a number row output.
But how can I compare then the colors. I need them to check if the random generated solution is the same as the selected colors.
What would you suggest, how to transform:
if own.color == Green

Is it the same thing with the ‘unique colors’?

just make a attribute like own[“color”] = “green” and compare against that.

store your colors in a dict and use the names of the colors as the key like this


pieces = {}

pieces["blue"] = [0,0,1,True]
pieces["green"] = [0,1,0,True]
pieces["red"] = [1,0,0,True]
pieces["yellow"] = [1.000,0.712,0.013,True]
pieces["brown"] = [0.207,0.105,0.030,True]
pieces["pink"] = [2.55,0.20,1.47,True]


then you can pick a color like this


piecelist = list(pieces)
random.shuffle(piecelist)
color = piecelist.pop(0)

and store that in a attribute on the object


own["color"] = color

or if you wants to give the object the color values stored
do this


own.color = pieces[color]

here is a blend that could give some ideas

http://15b.dk/blendfiles/mastermind.blend
press spacebar to restart the scene

Because it uses the module as storage as explained before.

When you want to remain with script mode you need an additional module that can be imported into your script.

Why do you need that?

When the colors are chosen by the player, I have to control, if those colors are correct. The output is a white cube if the color is correct and a black color and place are correct. Then the player continues and guesses the combination again with his new knowledge.

@edderkop - Thank you for the blend file. But this is for random unique colors, isn’t it? This problem is solved, I need to compare the colors now.

the code to do that is already in the blendfile

i have updated the blend in post #24 to make use of that code , use the link in post #24 to download it.

Thank you, but maybe a bit too complicated for me. You didn’t use the same way as I did to create the mastermint (lineup and co).
You didn’t use any controllers? And I see what you’ve made, but how to apply it to my version

#you have to move the camera in a good position before pressing p

I mean I have to make a color output black and white.


My scripts:

Blue

import random
from colorGenerator import POSSIBLE_COLORS

import bge

cont = bge.logic.getCurrentController()
own = cont.owner
    
own.color = POSSIBLE_COLORS[0]
# I don't post the other 5 color scripts, they work all the same.

colorCreator.py

# module
# color cache
import random

#Thank you monster

#--- module level code 
''' 
This code will be called a single time 
when this module gets accessed the first time
'''

# A pseudo constant that defines the initial colors
POSSIBLE_COLORS = [
      [0,0,1,True]
    , [0,1,0,True]
    , [1,0,0,True]
    , [255,255,0,True]
    , [0.207917,0.105,0.030,True]
    , [2.55,0.20,1.47,True]
]


# list
availableColors = list(POSSIBLE_COLORS)



#--- BGE  function (argument: controller)
def setRandomColor(controller):


    owner = controller.owner
    
    pickedColor = random.choice(availableColors)
    availableColors.remove(pickedColor)
    
    owner.color = pickedColor
    

#Tryed to create variables, but can't output them without running the whole thing - if I do so the random color thing crashes
#define Colors as well as values    
def Numberoutput(pickedColor):    
    
    pickedColor = setRandomColor(0)
    
    if pickedColor == availableColors[0]:
        y = blue
        
    elif pickedColor == availableColors[1]:
        y = green
        
    elif pickedColor == availableColors[2]:
        y = red
        
    elif pickedColor == availableColors[3]:
        y = yellow
        
    elif pickedColor == availableColors[4]:
        y = brown
        
    elif pickedColor == availableColors[5]:
        y = pink
    return y
        

prepared ControlScript:

import random
from colorGenerator import POSSIBLE_COLORS

import bge

cont = bge.logic.getCurrentController()
own = cont.owner