0
$\begingroup$

At the moment I am working on several scripts which use Numpy arrays. An example of one of my arrays is:

y = np.array(([75], [82], [93]), dtype = float)

Then I also have a game property called "y1" and I want to assign it a value from the Numpy array. The game property is a float number, and I have tried the following code but it does not seem to work:

own["y1"] = y[1]

Why does it not work? And how can I assign the game property to have any specific value from the Numpy array?

$\endgroup$
1
$\begingroup$

I figured out the problem, and the answer to said problem, and it is so much simpler than I thought.

When you have a Numpy array such as:

y = np.array(([75], [82], [93]), dtype = float)

The array is actually a matrix of numbers, and the above array is a matrix of size 3x1. Therefore, in order to set a game property (or any other variable if you so choose), you must pass in two numbers to specify the row and the column of the value you desire.

This means that instead of having;

own["y1"] = y[1]

In order to make the property equal "82" as the above code is trying to do, you should instead have:

own["y1"] = y[1, 0]

This new code will then specify the second row and the first column as the desired value.

Another example: If you have the numpy array;

y = np.array(([26,72,54], [46,82,37], [36,67,81]), dtype = float)

You can select the value "36" by specifying the third row and the first column, as done here:

own["y1"] = y[2, 0]

Hopefully this makes sense, and you can understand its implementation through the above examples.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.