Make sure you start you code with <code=lua>, not just <code> (square brackets, not <> signs.) This adds line numbers, which is SUPER helpful if I want to tell you there's an issue on line 19. Which there is. At least, I think it's line 19, but there aren't any line numbers, so....
Also, preface. You're clearly just leaning, and trying to take other people's code and make it work and don't know 100% what's going on yet, and that's okay. This actually seems like a pretty great code, especially for a beginner. You've just made a few beginner mistakes. But I think the ideas behind the code, that there should be an enemy that wakes up and starts playing miniboss music when he's first hit, and that once he takes some damage, he is replaced by an unarmored version, is a good concept. I assume the code for this basically works? It seems like it mostly should.
But, there's a couple issues and I'm going to give you a lot of information, hopefully it will help you learn and the next script you make, will be better because you'll know why you're making certain choices (not just "because it works").
So,
I don't think "sleeping" should be a savegame value- for one thing, right now every Iron Knuckle in the game shares the same value if they're sleeping or not so they'd all wake up at once XD. That's just a mess. What you want is a local variable.
For example, "local sprite" and "local movement" are two local variables called sprite and movement. They're declared at the top of the script, but they don't have any information in them yet. Some code will put some information in this variable later in the script, and it will affect only this script. Local variables are local to the "scope" of wherever you create them. So, since they're declared just in the enemy script, they're only a part of the enemy script. Every enemy can have a local variable called "number_of_pineapples" and if you give a specific enemy six pineapples when he bumps into a pineapple tree, that will just give him six pineapples. If you have an enemy with code like
enemy:on_bumping_into_tree()
game:set_value("number_of_pineapples", 6)
end
Then EVERY enemy of that breed would get six pineapples. You'd want:
local pineapples
enemy:on_bumping_into_tree()
pineapples = pineapples+6
end
Then the pineapples variable is just accessible by this script, not ANY script looking at game:get_value("pineapples")
So I think you'd want "sleeping" to be a similar way, where each instance of the script running has a local sleeping variable. Does that all make sense?
And speaking of local variables, you have these lines:
local sprite
local sprite = enemy:create_sprite("enemies/" .. enemy:get_breed())
This is redundant. It's like saying "I'm going to create a local variable, and it's going to be called sprite. I'm going to create a local variable, and it's going to be called sprite, and it's going to contain [the sprite of this enemy breed]". You definitely shouldn't be declaring variables of any type twice.
Better than that would be:
local sprite
sprite = enemy:create_sprite("enemies/" .. enemy:get_breed())
But that's still redundant.
The best option would be
local sprite = enemy:create_sprite("enemies/" .. enemy:get_breed())
Moving on down, your line "enemy:get_damage()" is getting the value you just set with "enemy:set_damage()", but... it's not doing anything at all with that value? You just get it and then ignore it and move on. It's like if in the middle of a paragraph I just wrote something unrelated. 12. And then kept going and never talked about it again.
I still can't figure out why self:set_pushed_back_when_hurt(false) needs to be self and not enemy. Maybe this is a bug? Someone smarter than me will have to help you there : /
Now, tell me if I'm wrong, but does sleeping = 1 mean it's asleep and sleeping = 2 mean it's awake?
If that's the case, you should use a boolean variable. Booleans can only be true or false (or in lua, nil, which acts a lot like false but basically means nobody ever bothered assigning it a value).
Basically, instead of saying something like
local sleeping = 1
You would say
local sleeping = true
Then you could have statements like
if sleeping == true then
movement = sol.movement.create("target")
end
True/False is a LOT easier to understand than 1 or 2 for something that can only be true or false, like "is this enemy asleep?"
Finally, you have this mess here:
if game:get_value("Sleeping") == 2
then movement = sol.movement.create("target")
end
if game:get_value("Sleeping") == 2
then movement:set_target(hero)
end
if game:get_value("Sleeping") == 2
then movement:set_speed(15)
end
if game:get_value("Sleeping") == 2
then movement:start(enemy)
end
if game:get_value("Sleeping") == 2
then sprite:set_animation("walking")
end
pleasepleasepleasepleaseplease write that like this:
if game:get_value("Sleeping") == 2 then
movement = sol.movement.create("target")
movement:set_target(hero)
movement:set_speed(15)
movement:start(enemy)
sprite:set_animation("walking")
end
Your way technically isn't wrong, but... it's very wrong.
I happen to have just started taking a computer coding course and the importance of writing code other people can understand is something we covered today. So we were encouraged to share our code and read other people's code, so this is practically studying for me, lol.