Need help with Health Pack

This is the code that I wrote in the gamelogic simple text editor

healthpack = cont.sensors [“healthpack”]

if healthpack. positive and own [“health”] < 100
own [“charging”] = True

if own [“charging”] == True and if own [“health”] < 100
own [“health”] += 1

if own [“health”] >= 100
own [“charging”] = False
own [“health”] = 100

I added all of the actuators and sensors and this and it simply disappears without doing any effect. They way I see it, this is a fairy simple thing to accomplish, it’s not like I’m trying to make some big cool intricate animation or anything, compared to most gaming tasks, this should be simple. Please help!

Attachments


test.blend (5 MB)

please use code tags [noparse]


[/noparse] when posting snippets.

You do not need to write “if variable == True”. It already evaluates against True. No need to process it twice.

What is “Charging” supposed to do? Should that be a sort of “healing over time”?

Where do you determine how much health to restore?
How do you store the remaining health from the already picked up health pack?

Does it even execute your code?

Also, it seems the indents on your first couple lines after the main() definition are messed up. Make sure you’re using Tabs, not spaces.

First steps before writing a script in the text editor:
-Put the mouse over the text editor and hit ctrl+T, in the options there and UN-check “tabs as spaces”
-If there is any text in your script, highlight it all, and go to format > convert whitespace > to tabs. (save first, this crashes blender for me on rare occasions)

EDIT: I wrote all that, then noticed you had a blend available. I will grab it and see what is going on…

So yes. There are several things wrong with your script. BUT, regardless of that, your blend file crashes when it’s run, even when that script is disabled, so I cannot test the corrected script.

My suggestion: Trash this project, start over. Make a much smaller environment. A plane with your pacman (Imma call him pacman) and a healthpack.
Next, go brush up on basic python scripting. I could post a script with all the errors corrected, but I have a feeling that wouldn’t teach much. There are some real fundamental things that are just wrong in your script.
If you need a suggestion for that, I recommend going to youtube and looking up ‘thenewboston’. He has a ton of helpful video tutorials that goes from basic to pro :slight_smile: