Jump to content

Making the NPC stop combat if player is below certain health: script help needed!


marinchev

Recommended Posts

SCRIPT DONE! Read last post for the complete code.

 

Ok, so I'm very new at GECK and NVSE script functions, and I'd like to make my npc stop combat after I hit them, if they drop me below a certain health (In this case using GetHealthPercentage).

 

So, apparently using the while cycle freezes the game for some reason, right after I hit the NPC. Here's the code:

scn SomeNPCBlablaCombatScript

ref refme
float targhp


begin OnStartCombat player

  set refme to GetSelf
  set targhp to 0.3

  while player.GetHealthPercentage > targhp
    if player.GetHealthPercentage <= targhp
      refme.stopcombat player
      break
    else
      continue
    endif
  loop


end

I've tried different scenarios with "while" (without break and continue), different blocktype (OnHit) too. But with all I've tried - the game still freezes and I can't help but think that using a cycle is wrong in an object script?! What? No way. Or at least in combat - maybe, IDK.

 

But if I leave it without a cycle, the game doesn't freeze. However, the NPC only checks the "if" block once - on the bloktype execution. After that it doesn't check, and the NPC just kills me.

 

I need to know if this can be achieved some other way. Thanks!

 

P.S. This thing I really need bad, because the NPC is made to kill very quickly - no time for Yields.

Edited by marinchev
Link to comment
Share on other sites

That's because you've created an infinite loop, the script engine stalled.

 

Try using a gamemode block, like this.

scn SomeNPCBlablaCombatScript

begin GameMode

  if player.GetHealthPercentage <= .3
      stopcombat player
  endif

end

A script on a ref means that ref is already implied so you don't need getself.

Link to comment
Share on other sites

That's because you've created an infinite loop, the script engine stalled.

 

Try using a gamemode block, like this.

scn SomeNPCBlablaCombatScript

begin GameMode

  if player.GetHealthPercentage <= .3
      stopcombat player
  endif

end

A script on a ref means that ref is already implied so you don't need getself.

Thanks a ton! I'll try what you suggested.

 

I really missed the fact that the script runs on the NPC, which suggests that getself is not necessary. Thank you - real neat optimization!

 

But one thing of notice - using gamemode seems kind of clumsy, since it will run ALL the time, not only in combat. Now I'm not saying it won't work - but when this is part of a larger project that uses a lot of scripts, such non-optimized code could (and most definitely would) hinder game performance (excluding higher-budget desktop systems). I'll look further into the matter (possibly avoid gamemode blocktype), since my goal is to run the script only during combat with the player.

 

About the infinite loop - yeah, didn't think about that... Shows what I know, alright. You think there's a way to run that script on the player? With an OnHit NPCref? In this case without using a cycle - just if/else block, since it will check on every hit? I don't know - is it possible to run the script on the player, not the NPC?

 

Lots of thanks, again! I do hope you could advise something about my last suggestion :smile: I'll tinker with it more myself, but any experienced opinion would be greatly appreciated.

Edited by marinchev
Link to comment
Share on other sites

Yeah, another small fact I missed is that conditional OnHit blocktype specifically works only on Creatures/NPC (object). So, to answer my own question - no, it wouldn't be possible to achieve my idea with a script on the player, since the player can only be referenced in that way by quest scripts.

 

Using the gamemode blocktype, suggested by RoyBatterian, everything worked out perfectly. Alhough not exactly the way I'd want, but still well enough. Thanks dude.

Edited by marinchev
Link to comment
Share on other sites

You're welcome, and the GameMode block will only run when the NPC is loaded. If you tick "No Low Level Processing" flag, the NPC won't be updated at all unless loaded.

Man, you rule. I always wondered what the hell that flag does! It never occurred to me that "Low Level" in this case refers to programming hierarchy, not the NPC's level xD

God dang &^!&@^ idiot I am, alright :D

 

Ok, now I'm gonna post the complete sequence here, case anyone else is interested in that mod!

 

First, we make a script of type object for our NPC to use. You can also integrate this code into an already-existing NPC script in the GECK, but depending on which blocktypes that NPC script contains, you may need to only add fragments of it, or the whole blocktype:

scn JulieCombatBreakerSCRIPT 

short breakonce 
begin GameMode

  if player.GetHealthPercentage <= 0.35 && GetCombatTarget == player && breakonce == 0
    stopcombat player
    set breakonce to 1
  endif


end
I've modified the code with a breakonce integer variable, which is set by the combat breaker to 1. This makes the StopCombat player a one-time event, upon your first battle with the NPC. The next time you attack, it won't stop combat until one of you is dead (normal combat behavior).
Also, the GetCombatTarget condition may seem silly at first, but in some situations the NPC might be in combat with another actor, and unless this condition is met, it will execute the script even then. It's very appropriate if you want the NPC to initiate another action after stopping combat with the player (especially dialogue).
Next up, select your custom made NPC, select the script you just created from the Script dropdown menu, and now the NPC will use it.
As suggested by RoyBatterian, tick "No Low Level Processing" flag, so the GameMode block won't be processed, unless your NPC has a reference, loaded in the world. But beware - unless this is your custom made npc, placing the flag on vanilla NPCs could screw with their scripts.
Edited by marinchev
Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...