To name the functions by what it does, and so that not all script lines get read/used until used/called.
also to be used multiple times by different objects.
for example 10 objects runs 1st function. some other objects can use the other functions if needed. so 1 script for lots of functions that basically does the same thing. (keeping things together instead of creating 10 scripts)
from bge import *
from random import *
Don’t use * it’s outdated, import bge does the same thing or if you need a sub module use
from bge import logic,render,etc
COLOR_RANDOM = [randint(0,1),randint(0,1),randint(0,1),1]
why use randint here, you could use randint(1,4) instead of your property or random.choice(), or if you use upbge using weighted random like random.choices.
oh wait you use it
cont.owner["COLOR_NUMBER"] = randint(1,4)
and then you do
if cont.owner["COLOR_NUMBER"] == 1:
so the whole property is wasted resources
because with a list you can pick directly from it like i did.
then you get the if statement
if cont.owner["COLOR_NUMBER"] == 1:
OBJ = logic.getCurrentScene().addObject("PROP : CUBE", cont.owner)
OBJ.color = COLOR_BLUE
if cont.owner["COLOR_NUMBER"] == 2:
OBJ = logic.getCurrentScene().addObject("PROP : CUBE", cont.owner)
OBJ.color = COLOR_RED
if cont.owner["COLOR_NUMBER"] == 3:
OBJ = logic.getCurrentScene().addObject("PROP : CUBE", cont.owner)
OBJ.color = COLOR_GREEN
if cont.owner["COLOR_NUMBER"] == 4:
OBJ = logic.getCurrentScene().addObject("PROP : CUBE", cont.owner)
OBJ.color = COLOR_YELLOW
repeating code all over while it could be just 3 lines of code
also you check for things that never gets True.
if you make an if statment like that then atleast use elif, like this
if cont.owner["COLOR_NUMBER"] == 1:
OBJ = logic.getCurrentScene().addObject("PROP : CUBE", cont.owner)
OBJ.color = COLOR_BLUE
elif cont.owner["COLOR_NUMBER"] == 2:
OBJ = logic.getCurrentScene().addObject("PROP : CUBE", cont.owner)
OBJ.color = COLOR_RED
elif cont.owner["COLOR_NUMBER"] == 3:
OBJ = logic.getCurrentScene().addObject("PROP : CUBE", cont.owner)
OBJ.color = COLOR_GREEN
elif cont.owner["COLOR_NUMBER"] == 4:
OBJ = logic.getCurrentScene().addObject("PROP : CUBE", cont.owner)
OBJ.color = COLOR_YELLOW
then if you want to define an object you could simply do:
if SPAWN_DEFINED == 1:
#Define the selected colors to spawned objects
OBJ = logic.getCurrentScene().addObject("PROP : CUBE", cont.owner)
if cont.owner["COLOR_NUMBER"] == 1:
OBJ.color = COLOR_BLUE
elif cont.owner["COLOR_NUMBER"] == 2:
OBJ.color = COLOR_RED
elif cont.owner["COLOR_NUMBER"] == 3:
OBJ.color = COLOR_GREEN
elif cont.owner["COLOR_NUMBER"] == 4:
OBJ.color = COLOR_YELLOW
elif SPAWN_RANDOM == 1:
OBJ.color = COLOR_RANDOM
also the color you could just assign that to a variable as well
if SPAWN_DEFINED == 1:
if cont.owner["COLOR_NUMBER"] == 1:
color = COLOR_BLUE
elif cont.owner["COLOR_NUMBER"] == 2:
color = COLOR_RED
elif cont.owner["COLOR_NUMBER"] == 3:
color = COLOR_GREEN
elif cont.owner["COLOR_NUMBER"] == 4:
color = COLOR_YELLOW
elif SPAWN_RANDOM == 1:
color = COLOR_RANDOM
OBJ.color = color
#or so color exist set color
# if color:
# OBJ.color = color
And so on…
I would just put colors into a list and pick random from that, saves you coding time as well.
it looks better and saves resources.
This answer good enough?