Storing health in a variable

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!
Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
Post Reply
shokteenik
Posts: 2
Joined: Wed Nov 11, 2020 7:01 am

Storing health in a variable

Post by shokteenik »

Hello,

I've just started getting into Doom mapping and scripting. I'm making a level where you enter a virtual reality environment for a time, then return back to the "real" game world. For this I need to store the health of the player as it was before he enters the virtual reality.

This is my script:

Code: Select all

world int 1:storedhealth;
world bool 1:InCyberSpace;

script 801 (void)

{

storedhealth = GetActorProperty(0, APROP_Health);

Teleport_NoFog(0, 1, 690);
InCyberSpace = true;

setplayerproperty (420, 1, 16);

SetActorProperty(420, APROP_Health, 300);
GiveInventory("CyberWeapon", 1);
SetWeapon("CyberWeapon");


while ( InCyberSpace == true ) {

if (GetActorProperty(420, APROP_Health) < 2 )
   {
     InCyberSpace = false;     
     ACS_Execute (802, 0);
   }

   else

   { delay(1); }

 }
}


script 802 (void)

{

Teleport_NoFog(0, 1, 691);
 
TakeInventory("CyberWeapon", 1);
InCyberSpace = false;
setplayerproperty (420, 0, 16);
SetActorProperty(420, APROP_Health, storedhealth);
}
However when I do this, the last line just kills the player, giving him zero health it seems.

What am I doing wrong here?
Last edited by shokteenik on Wed Nov 11, 2020 3:20 pm, edited 1 time in total.
User avatar
Player701
 
 
Posts: 1710
Joined: Wed May 13, 2009 3:15 am
Graphics Processor: nVidia with Vulkan support
Contact:

Re: Storing health in a variable

Post by Player701 »

Why are you using TID 420 in your code? You probably have to use 0 everywhere to refer to the activator of the script (in your case that would be the player).

Also, I'm not sure this code is going to prevent death. It could be that when you call SetActorProperty, the player is already dead. I don't think setting their health to a positive value is going to resurrect them. So if it still doesn't work right, try setting PROP_BUDDHA via SetPlayerProperty before entering cyberspace, and un-set it after leaving.
shokteenik
Posts: 2
Joined: Wed Nov 11, 2020 7:01 am

Re: Storing health in a variable

Post by shokteenik »

Player701 wrote:Why are you using TID 420 in your code? You probably have to use 0 everywhere to refer to the activator of the script (in your case that would be the player).
0 doesn't always seem to work in all cases since the script seems to "forget" who the activator is, like when giving health. I followed the instructions from here and that fixed that problem. When the player enters the virtual world, he receives 300 health, so that part works.
Player701 wrote:Also, I'm not sure this code is going to prevent death. It could be that when you call SetActorProperty, the player is already dead. I don't think setting their health to a positive value is going to resurrect them. So if it still doesn't work right, try setting PROP_BUDDHA via SetPlayerProperty before entering cyberspace, and un-set it after leaving.
The player is already given PROP_BUDDHA via this line: "setplayerproperty (420, 1, 16);" (EDIT: Which I apparently forgot to include in my example script, sorry, fixed it now.)

He can't die in the virtual world, but the code checks if his health drops to 1, and then he returns to the "real" world, upon which PROP_BUDDHA is removed. He can't be dead when he returns. This all works just fine, but I want the player to return to the original health value he had before entering the virtual world, and I'm trying to store that information in a global variable called "storedhealth".

The problem seems to be I can't get the global variable to work properly with "SetActorProperty(420, APROP_Health, storedhealth);". I don't know how the global variables work exactly but for some reason the value keeps changing between scripts and when used with SetActorProperty returns nothing, which ends up being interpreted as 0 health and that kills the player. I'm pretty stumped what's going on here.
User avatar
Player701
 
 
Posts: 1710
Joined: Wed May 13, 2009 3:15 am
Graphics Processor: nVidia with Vulkan support
Contact:

Re: Storing health in a variable

Post by Player701 »

OK, now I see what the problem is. You're using two world scope variables that both have an index of 1. Which means they are probably sharing the same value (as far as type conversion allows), and when InCyberSpace is set to false, storedhealth gets reset to 0 because its value is read from the same place.

To resolve this, use different indices for your world variables:

Code: Select all

world int 1:storedhealth;
world bool 2:InCyberSpace;
Note that world scope is only required when you want to persist your values across multiple levels in a hub. If you only have a single level, you should use map scope instead. Map variables do not require any indices in their definitions:

Code: Select all

int storedhealth;
bool InCyberSpace;
Post Reply

Return to “Scripting”