What's wrong with this walk script?


import bge
from bge import logic


def walk(cont):
    own = cont.owner
    keyboard = logic.keyboard
    w_key = keyboard.events[bge.events.WKEY]
    a_key = keyboard.events[bge.events.AKEY]
    s_key = keyboard.events[bge.events.SKEY]
    d_key = keyboard.events[bge.events.DKEY]
    shift_key = keyboard.events[bge.events.LEFTSHIFTKEY]
    active = logic.KX_INPUT_ACTIVE
    curVel = own.getLinearVelocity
    
    #variables
    walkSpeed = 5.0
    
    if shift_key != active:
        if w_key == active:
            own.setLinearVelocity([curVel[0], walkSpeed, curVel[2]], True)

When I try to run game with this script, it outputs following:


line 20, in walk
TypeError: 'builtin_function_or_method' object is not subscriptable

What’s the cause and how to fix it?

Line 13:


curVel = own.getLinearVelocity(1)

So simple:D

For a more readable code. I will recommed to use a boolean instead of a number in getLinearVelocity. Also, it return a vector, They are accessible by using x, y and z.

So instead of use: curVel[0], you could use: curVel.x (0 = x, 1 = y, 2 = z)

In documentation they say it returns list.


And I checked by myself: