How to set all objects in all rooms to have hp = maxhp?

daeun
17 Jan 2022, 10:03Yeah, I am making another stupid rpg game, but because I did not learn about coding, I seems to have hit a wall.
So, in my rpg game, whenever players kills an enemy, the player automatically regains hp to maxhp which is what I intended and is not going to change. But players can abuse this healing mechanic, players go to big boss enemy to fight until boss is half life, then go to a faraway room to fight a small monster and the player regains hp to maxhp, then he comes back to the boss and kills him in a very underhanded method.
Of course, I did fixed this cheat in my first released game, but I did lots of programs, like hiding other monsters when players starts fighting, creating attributes like players are already in combat which might stops it from attacking other monsters or when players are not in combat goes into fight, the enemies regains back to maxhp, yup, all these are tons of work as evidence from opening out my previous game creation page.
So what I want is actually a simple code which I have no coding experience about on.
When the players kills any enemies or small monsters, the player automatically regains hp to maxhp, at this moment, I need a script that set all objects in all rooms to have hp = maxhp.
Another advanced coding question:
It is awkward for players to heal to maxhp when he kills an enemy in a room with two other enemies, the fight is not finished, but yet somehow the players are healing, this is not very thematic. I have no idea how to effectively tackle this problem, even if you do not have a script but you have a sketch answer, I would like to hear about it.
mrangel
17 Jan 2022, 10:23I need a script that set all objects in all rooms to have hp = maxhp.
foreach (obj, AllObjects()) {
if (HasInt (obj, "maxhp")) {
obj.hp = obj.maxhp
}
}
It is awkward for players to heal to maxhp when he kills an enemy in a room with two other enemies, the fight is not finished, but yet somehow the players are healing, this is not very thematic. I have no idea how to effectively tackle this problem, even if you do not have a script but you have a sketch answer, I would like to hear about it.
Why not have the healing script only run when the last monster in the room is killed. So for example:
monsters_found = false
// This loops, setting "monster" to every visible object with a "hp" attribute
foreach (monster, FilterByNotAttribute(ScopeVisibleNotHeld(), "hp", null)) {
if (monster.hp > 0 and not monster = game.pov) {
monsters_found = true
}
}
if (not monsters_found) {
msg ("You won the battle!")
game.pov.hp = game.pov.maxhp
// put the other code here if you want to do anything else when a battle ends
}

daeun
17 Jan 2022, 13:19Just dumbing it down for the future me to understand it easier.
https://docs.textadventures.co.uk/quest/scripts/foreach.html
https://docs.textadventures.co.uk/quest/functions/filterbynotattribute.html
https://docs.textadventures.co.uk/quest/advanced_scope.html
https://docs.textadventures.co.uk/quest/changing_the_player_object.html
The first script is self-explanatory, I will move to the second code.
By default, game.monsterfound=0 (False)
foreach (monster(Any name will do as it is a variable))
FilterByNotAttribute(ScopeVisibleNotHeld(), "hp", null)
ScopeVisibleNotHeld() generally means the extent of an area, because it is not held, it means objects in the room instead of in the inventory.
By putting "null", the FilterByNotAttribute now works as though it is FilterByAttribute which makes much more sense, I guess we are not using FilterByAttribute because we need to insert the value of hp which is different for all of my monsters, so by using "null", the code just find all objects with the attribute "hp" regardless of value.
if (monster.hp > 0 and not monster = game.pov) {
monsters_found = true }
This is straightforward, the variable that can be replaced with any name (monster) indicates all the objects found with our code and that if they have more than 0 hp, it would means that game.monsterfound=1 (True).
not monster = game.pov simply means that the code ignores the player, this is very valid point since the player does have hp as well unless we cheat by giving the player another attribute name like healthpoints instead of hp.
The rest is self-explanatory too.
Thanks to mrangel for solving my questions in just 20 minutes after I posted them.
mrangel
17 Jan 2022, 14:25By putting "null", the FilterByNotAttribute now works as though it is FilterByAttribute which makes much more sense, I guess we are not using FilterByAttribute because we need to insert the value of hp which is different for all of my monsters, so by using "null", the code just find all objects with the attribute "hp" regardless of value.
Yeah… null
is used as a special value when you try to examine an attribute that doesn't exist. So finding all objects that don't have null gets all visible objects which have an hp
. That's one thing that people often seem to have a problem understanding :) So glad you could follow it.