My first python script in UPBGE wooo!

Yooooooo!!! I am so happy! I just made my first fully working script, it’s extremely basic and even a child could understand that, but I am happy with what I made!

So, this script can be used to change r, g, b, and alpha values of the script, its very simple, here, have a look at it

from bge import logic

C = logic.getCurrentController()
O = C.owner

red = C.sensors['red']
green = C.sensors['green']
blue = C.sensors['blue']
alpha = C.sensors['alpha']
red1 = C.sensors['red1']
green1 = C.sensors['green1']
blue1 = C.sensors['blue1']
alpha1 = C.sensors['alpha1']

if red.positive:
    O['red'] += 0.1
    
if green.positive:
    O['green'] += 0.1
    
if blue.positive:
    O['blue'] += 0.1

if alpha.positive:
    O['alpha'] += 0.1
    
if red1.positive:
    O['red'] -= 0.1
    
if green1.positive:
    O['green'] -= 0.1
    
if blue1.positive:
    O['blue'] -= 0.1

if alpha1.positive:
    O['alpha'] -= 0.1
    
if O['red'] > 2:
    O['red'] = 2    
    
if O['green'] > 2:
    O['green'] = 2  
    
if O['blue'] > 2:
    O['blue'] = 2
    
if O['alpha'] > 2:
    O['alpha'] = 2 
    
if O['red'] < 0:
    O['red'] = 0    
    
if O['green'] < 0:
    O['green'] = 0  
    
if O['blue'] < 0:
    O['blue'] = 0
    
if O['alpha'] < 0:
    O['alpha'] = 0     
    
r = O['red']
g = O['green']
b = O['blue']
a = O['alpha']

O.color = [r,g,b,a]

Yeah yeah I know that I could have used a keyboard module or something instead of these many sensors, but I like this method. And I would like to thank my friend, blender games 3d for helping me to learn everything I know now! And thank you all guys for your support!

4 Likes

Congratulations! : D

Scripting opens almost all doors in terms of gameplay!
I wish you the best of luck with future projects!
Looking forward to seeing them! Ø__^

1 Like

Haha thanks man!

More scripts coming soon!

little advice , avoid “if” and loops when it’s possible to avoid having too much long code. Having an easy code to read is a good ally to debug

red = C.sensors['red'].positive
green = C.sensors['green'].positive
blue = C.sensors['blue'].positive
alpha = C.sensors['alpha'].positive
red1 = C.sensors['red1'].positive
green1 = C.sensors['green1'].positive
blue1 = C.sensors['blue1'].positive
alpha1 = C.sensors['alpha1'].positive

O['red'] =  O['red']  + ( red * 0.1  -  red1 * 0.1 ) *  ( 0 <  O['red'] <= 2 )
...
O['alpha'] =  O['alpha']  + ( alpha * 0.1  -  alpha1 * 0.1 ) *  ( 0 <  O['alpha'] <= 2 )



O.color = [O['red'],O['green'],O['blue'],O['alpha']]
1 Like

i disagree, thats much less readable. loops are actually no different from normal code, i dont know why everyone hate on them.

and for the love of all things good, DONT USE SINGLE LETTER VARIABLES.

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

for key in ["red", "green", "blue", "alpha"]:
    up = cont.sensors[key].positive
    dn = cont.sensors[key+"1"].positive
    if up:
        owner[key] += 0.1
    if dn:
        owner[key] -= 0.1

    if owner[key] < 0:
        owner[key] = 0
    if owner[key] > 2:
        owner[key] = 2

I was just showing him that an assignement can be done in 1 line even if there’s some more additionnal conditions.

You have already 4 " if" and 1 loop for this particular simple case. If Ansh wants to tune his values red (0-2) but blue (3-5) and call “green1” green2 … +0.2 instead of 0.1 then you good to add another load of “if” to deal with all the new particular cases and the loop on properties ( a good one) won’t work anymore

please, i respect your work very much and you have probably more knowledge that I in Python, but don’t be like those people trying to show “they know best” it’s very irritating. (i didn’t use single letter variables)

1 Like

its not me, its a python PEP https://www.python.org/dev/peps/pep-0008/

1 Like

if this is only being run once, then convert it into a single if statement, instead of 16
same with this piece…:

You can use 2 or even 1 if statement.

Simply by using elif, you reduce the processing time by tons.

so for example:

    if up:
        owner[key] += 0.1
    elif dn:
        owner[key] -= 0.1

    if owner[key] < 0:
        owner[key] = 0
   elif owner[key] > 2:
        owner[key] = 2

Now you converted 4 into 2 statements/checks. Or…

    if up:
        owner[key] += 0.1
    elif dn:
        owner[key] -= 0.1
    elif owner[key] < 0:
        owner[key] = 0
    elif owner[key] > 2:
        owner[key] = 2

And now you got 1 if statement/check. So depending if all keys can be pressed and used at the same time, you can lower the workload for those statements.

If you press up, normally you won’t press down, so adding an el(short for else) works wonders because it does not need to check the down key while up is pressed. (same for the orhet keys/functions).

2 letters (if not a word) is also not good, what does DN mean? yeah down, but dn does not explicit say that it means down, maybe it means dual numbers, or whatever…

Nice to show someone how it can be, but this is just a complete mess, faster yes, but for a scripter (in a team) this would be the biggest nightmare.

1 Like

your usual moderation … :expressionless:

value = value + (update) * (condition)

i hope it’s more clear for your little brain now :wink:

1 Like