Jump to content
The Dark Mod Forums
Sign in to follow this  
games-furious

Doom 3 i cant get getHealth( ) of actor by script?

Recommended Posts

I,m trying getHealth () from enemy with script but without sucess. If possible help me


t = sys.getTime();
sys.print(t);
if (t > 5)
{
//Code for spawn enemies Wherever you wish on the map
float killed = 0.0;

sys.wait(1.8);


entity new_monster = sys.spawn("monster_zombie_fat"); // example monster_zombie_fat or any class enemy name
new_monster.setKey("name", "monster_zombie_fat_3");
new_monster.setOrigin('-136 -120 11');

if ($monster_zombie_fat_3.getHealth( ) < 1){ // Not Working cant get value of health
killed += 1;
string myStr = new_monster.getKey("name"); // Retrieve the value of the "name" spawnarg of the player entity
myStr = "The name of zombie_fat is " + myStr + "killed = " + killed + "\n";
sys.println(myStr); // Print the text to the console
}
}
Edited by games-furious

Share this post


Link to post
Share on other sites

As I read this, if it runs in the first 5 seconds after map load it does nothing; if it runs later, it spawns a zombie, immediately checks whether it has health, and does nothing unless the zombie has spawned with no health. Is that what you wanted, or are you trying to check for when the zombie has been killed?


Some things I'm repeatedly thinking about...

 

- louder scream when you're dying

Share this post


Link to post
Share on other sites

As I read this, if it runs in the first 5 seconds after map load it does nothing; if it runs later, it spawns a zombie, immediately checks whether it has health, and does nothing unless the zombie has spawned with no health. Is that what you wanted, or are you trying to check for when the zombie has been killed?

look i need know a health value of zombie spawned by script then if got 0 write to console zombie Killed 1, 2, 3....

this zombie have a default health 125 when spawned

 

but the event float getHealth( ) is not working dont get error on console but dont get value lol.

Edited by games-furious

Share this post


Link to post
Share on other sites

The monster health is saved in his def file so you can get the value using something like &entity.getFloatKey("health");

 

Or:

float mhealth = 0.0;
entity monster = sys.getEntity("monster_name");

if(monster){
     mhealth = monster.getFloatKey("health");
}

if( mhealth <= 0.0){
    println("Monster DEAD!!");
}
Edited by HMart

Share this post


Link to post
Share on other sites

The monster health is saved in his def file so you can get the value using something like &entity.getFloatKey("health");

thanks for reply i go try this first.

Just you got this getFloatKey("health") from monster_.....def ?

 

Or:

 

float mhealth = 0.0;entity monster = sys.getEntity("monster_name");if(monster){     mhealth = monster.getFloatKey("health");}if( mhealth <= 0.0){    println("Monster DEAD!!");}
Edited by games-furious

Share this post


Link to post
Share on other sites

The monster health is saved in his def file so you can get the value using something like &entity.getFloatKey("health");

 

 

 

Or:

 

float mhealth = 0.0;entity monster = sys.getEntity("monster_name");if(monster){     mhealth = monster.getFloatKey("health");}if( mhealth <= 0.0){    println("Monster DEAD!!");}
dont work for mhealth got every time 0 print Monster Dead! with zombie live i see dont get real value o health Edited by games-furious

Share this post


Link to post
Share on other sites

How do you run your script? As far as I know, you need a function that calls the script. If you can set an "on death: run script" for the zombie, you can skip the condition with the health check and simply use the remaining part that counts and prints the "killed" value.

Share this post


Link to post
Share on other sites

How do you run your script? As far as I know, you need a function that calls the script. If you can set an "on death: run script" for the zombie, you can skip the condition with the health check and simply use the remaining part that counts and prints the "killed" value.

i run script by trigger multiply key/val call / function_name and with key/val wait / 20 condition then if triggered, spawn class name, the zombies dont exist in map like monster_zombie_fat_1, monster_zombie_fat_2,... using this method for performance using script to spawn.I need real value of health for count real zombie killed's

Edited by games-furious

Share this post


Link to post
Share on other sites

dont work for mhealth got every time 0 print Monster Dead! with zombie live i see dont get real value o health

 

Are you sure the line monster = sys.getEntity("monster_name"); returned a valid entity? For that code to work the monster has to already be spawned in the game.

 

If there's no valid entity or valid health value, the code example i gave initializes mhealth to zero ( afaik script engine does that even if you don't), so because of the way the if block makes the test it will be always true.

 

Btw truth be told I never really worked with AI before, i'm not sure if the health value in the def file is a static value or a value that gets updated each time the monster is hurt, so i'm not really sure if doing it the way I posted is the correct way.

 

 

Also I looked at the c++ AI source code for the script function getHealth(); and afaik it should work. Behind the scenes it returns the health variable defined in all entities classes in c++.

/*
=====================
idAI::Event_SetHealth
=====================
*/
void idAI::Event_SetHealth( float newHealth ) {
	health = newHealth;
	fl.takedamage = true;
	if ( health > 0 ) {
		AI_DEAD = false;
	} else {
		AI_DEAD = true;
	}
}

/*
=====================
idAI::Event_GetHealth
=====================
*/
void idAI::Event_GetHealth( void ) {
	idThread::ReturnFloat( health );
}

So if it is not working you must be doing something wrong, perhaps you are spawning the monster in the wrong way? You could try seeing how idsoftwared scripted their monsters by looking at their scripts, is the way I learned many stuff about this engine.

 

Try this code.

t = sys.getTime();
sys.print(t);
if (t > 5)
{
  //Code for spawn enemies Wherever you wish on the map 
  float killed = 0.0;
  
  sys.wait(1.8);
 
  entity new_monster;
  sys.setSpawnArg("name", "monster_zombie_fat_3"); // set spawnarg before spawning the entity
  new_monster = sys.spawn("monster_zombie_fat");
  new_monster.setOrigin('-136 -120 11');
  
  if (new_monster && (new_monster.getHealth( ) < 1)){
         killed += 1;
         string myStr = new_monster.getKey("name");
         myStr = "The name of zombie_fat is " + myStr + "killed = "  + killed +  "\n";
         sys.println(myStr);   // Print the text to the console
   }
}
Edited by HMart

Share this post


Link to post
Share on other sites

Try this, since getHealth() only works with AI or the player:

 
ai monster_AI = new_monster;
 
float health = monster_AI.getHealth();
 
  • Like 1

Share this post


Link to post
Share on other sites

 

 

Are you sure the line monster = sys.getEntity("monster_name"); returned a valid entity? For that code to work the monster has to already be spawned in the game.

 

If there's no valid entity or valid health value, the code example i gave initializes mhealth to zero ( afaik script engine does that even if you don't), so because of the way the if block makes the test it will be always true.

 

Btw truth be told I never really worked with AI before, i'm not sure if the health value in the def file is a static value or a value that gets updated each time the monster is hurt, so i'm not really sure if doing it the way I posted is the correct way.

 

 

Also I looked at the c++ AI source code for the script function getHealth(); and afaik it should work. Behind the scenes it returns the health variable defined in all entities classes in c++.

/*
=====================
idAI::Event_SetHealth
=====================
*/
void idAI::Event_SetHealth( float newHealth ) {
	health = newHealth;
	fl.takedamage = true;
	if ( health > 0 ) {
		AI_DEAD = false;
	} else {
		AI_DEAD = true;
	}
}

/*
=====================
idAI::Event_GetHealth
=====================
*/
void idAI::Event_GetHealth( void ) {
	idThread::ReturnFloat( health );
}

So if it is not working you must be doing something wrong, perhaps you are spawning the monster in the wrong way? You could try seeing how idsoftwared scripted their monsters by looking at their scripts, is the way I learned many stuff about this engine.

 

Try this code.

t = sys.getTime();
sys.print(t);
if (t > 5)
{
  //Code for spawn enemies Wherever you wish on the map 
  float killed = 0.0;
  
  sys.wait(1.8);
 
  entity new_monster;
  sys.setSpawnArg("name", "monster_zombie_fat_3"); // set spawnarg before spawning the entity
  new_monster = sys.spawn("monster_zombie_fat");
  new_monster.setOrigin('-136 -120 11');
  
  if (new_monster && (new_monster.getHealth( ) < 1)){
         killed += 1;
         string myStr = new_monster.getKey("name");
         myStr = "The name of zombie_fat is " + myStr + "killed = "  + killed +  "\n";
         sys.println(myStr);   // Print the text to the console
   }
}

your code dont work get type mismatch error for ---> if (new_monster && (new_monster.getHealth( ) < 1)) // then a put just if (new_monster.getHealth( ) and nothing happning

 

everytime the value is 0 dont get by run time

Share this post


Link to post
Share on other sites

 

 

 

Are you sure the line monster = sys.getEntity("monster_name"); returned a valid entity? For that code to work the monster has to already be spawned in the game.

 

If there's no valid entity or valid health value, the code example i gave initializes mhealth to zero ( afaik script engine does that even if you don't), so because of the way the if block makes the test it will be always true.

 

Btw truth be told I never really worked with AI before, i'm not sure if the health value in the def file is a static value or a value that gets updated each time the monster is hurt, so i'm not really sure if doing it the way I posted is the correct way.

 

 

Also I looked at the c++ AI source code for the script function getHealth(); and afaik it should work. Behind the scenes it returns the health variable defined in all entities classes in c++.

/*
=====================
idAI::Event_SetHealth
=====================
*/
void idAI::Event_SetHealth( float newHealth ) {
	health = newHealth;
	fl.takedamage = true;
	if ( health > 0 ) {
		AI_DEAD = false;
	} else {
		AI_DEAD = true;
	}
}

/*
=====================
idAI::Event_GetHealth
=====================
*/
void idAI::Event_GetHealth( void ) {
	idThread::ReturnFloat( health );
}

So if it is not working you must be doing something wrong, perhaps you are spawning the monster in the wrong way? You could try seeing how idsoftwared scripted their monsters by looking at their scripts, is the way I learned many stuff about this engine.

 

Try this code.

t = sys.getTime();
sys.print(t);
if (t > 5)
{
  //Code for spawn enemies Wherever you wish on the map 
  float killed = 0.0;
  
  sys.wait(1.8);
 
  entity new_monster;
  sys.setSpawnArg("name", "monster_zombie_fat_3"); // set spawnarg before spawning the entity
  new_monster = sys.spawn("monster_zombie_fat");
  new_monster.setOrigin('-136 -120 11');
  
  if (new_monster && (new_monster.getHealth( ) < 1)){
         killed += 1;
         string myStr = new_monster.getKey("name");
         myStr = "The name of zombie_fat is " + myStr + "killed = "  + killed +  "\n";
         sys.println(myStr);   // Print the text to the console
   }
}

your code dont work get type mismatch error for ---> if (new_monster && (new_monster.getHealth( ) < 1)) // then a put just if (new_monster.getHealth( ) and nothing happning

 

everytime the value is 0 dont get by run time

 

 

with this code in main ( ) or call triger function i got the first value of health 100 but he dont uptade decrease when zombie hited. THE problem now s how update value in runtime?

 

void main()
  {
 
   sys.waitFrame();
   /*  float killed = 0;
   float mhealth;
   
   
   
   sys.println("zombie health  " + mhealth + "\n");   
  
   sys.wait(20);
    
      
   entity new_monsterz = sys.getEntity("monster_zombie_fat_3");
   mhealth = new_monsterz.getFloatKey("health");
   sys.println("zombie health  " + mhealth + "\n");
   if (mhealth > 50.0){
sys.println("new_monsterz DEAD!");
}
   
   if (mhealth > 50.0){
killed += 1;
     string myStr = new_monsterz.getKey("name");   // Retrieve the value of the "name" spawnarg of the player entity
         myStr = "The zombie_fat " + myStr + "is killed = " + killed + "\n";
         sys.println(myStr);   // Print the text to the console
    }  */
   
    sys.wait(10);
    entity new_monster;
    sys.setSpawnArg("name", "monster_zombie_fat_3"); // set spawnarg before spawning the entity
    new_monster = sys.spawn("monster_zombie_fat"); // example monster_zombie_fat or any class enemy name
    new_monster.setOrigin('-136 -120 11');
    float zombie_health = new_monster.getFloatKey("health");
    sys.println("health is " + zombie_health + "\n");
    float killed = 0;
    if (zombie_health <= 0){
          killed += 1;
          string myStr = new_monster.getKey("name");
          myStr = "The name of zombie_fat is " + myStr + " killed = "  + killed +  " HEALTH is " + zombie_health +  "\n";
          sys.println(myStr);   // Print the text to the console
     }
 
 
}

 

Edited by games-furious

Share this post


Link to post
Share on other sites

THIS CODE WORK but only with sys.wait(10) i need kill zombie before time wait 10 or i lost time to write a values to variables.

 

But another problem is killed update only from 0 to 1 everytime. The function is restarted by trigger multiple then every values reseted. I need storage this values

for used in game in this case de killed variable

 

Need may be storage in cfg for get value of variable killed but how make this?

Edited by games-furious

Share this post


Link to post
Share on other sites
void kill_player() // is for enemy single player in this case
{

//sys.waitFrame();
float t;
float killed;

//$player1.setKey("health", "0.0");
//float killed = 0;
t = sys.getTime();
sys.print(t);
//float zombie_health = new_monster.getFloatKey("health");
//sys.println("health is " + zombie_health + "\n");
//sys.println("health by getHealth is " + $monster_zombie_fat_3.getHealth() + "\n");
if (t > 5)
{
//Code for spawn enemies Wherever you wish on the map


entity new_monster;
sys.setSpawnArg("name", "monster_zombie_fat_3"); // set spawnarg before spawning the entity
new_monster = sys.spawn("monster_zombie_fat"); // example monster_zombie_fat or any class enemy name
new_monster.setOrigin('-136 -120 11');

//sys.println("health is " + zombie_health + "\n");
//sys.println("health by getHealth is " + $monster_zombie_fat_3.getHealth() + "\n");

entity spawn_effects = sys.spawn("func_fx");
spawn_effects.setOrigin('-136 -120 11');
spawn_effects.startFx("fx/teleporter");
//float health = $monster_zombie_fat_3.getHealth();

sys.wait(10); // not good have a time limit for kill zombie or never will print value to console


if ($monster_zombie_fat_3.getHealth() <= 0){
killed += 1; // ERROR ONLY UPTADE FROM 0 TO 1 everytime i triggered function call
string myStr = new_monster.getKey("name");
myStr = "The name of zombie_fat is " + myStr + " killed = " + killed + " HEALTH is " + $monster_zombie_fat_3.getHealth() + "\n";
sys.println(myStr); // Print the text to the console
}

}

}

Share this post


Link to post
Share on other sites

 

Try this, since getHealth() only works with AI or the player:

 
ai monster_AI = new_monster;
 
float health = monster_AI.getHealth();
 

 

Hum thanks for the info, that is good to know, I code on c++ for the most part so some scripting things are lost to me.

 

 

 

 

your code dont work get type mismatch error for ---> if (new_monster && (new_monster.getHealth( ) < 1)) // then a put just if (new_monster.getHealth( ) and nothing happning

 

My bad I code on my idtech 4 game 99.9% of the time in c++, i've been pretty much neglecting Doom script for years now (I can do more stuff in c++ anyway), so I just forget how script works sometimes, sorry for that.

 

That error happens because entity type is not the same as a float type and that is totally right. In c++ you can cast a type to another but script is more restrict, for ease of use.

 

It should have been:

if (new_monster) // or even if(new_monster != $null_entity)
}
      if(new_monster.getHealth( ) < 1))
      {
         killed += 1;
         string myStr = new_monster.getKey("name");
         myStr = "The name of zombie_fat is " + myStr + "killed = "  + killed +  "\n";
         sys.println(myStr);   // Print the text to the console
      }
}

Share this post


Link to post
Share on other sites

but this is solved thanks the problem now is how increment and save value in var float killed.Like i said i using a trigger multiple for restart function then all values except time by sys.getTime() the value update everytine sure.

Hum thanks for the info, that is good to know, I code on c++ for the most part so some scripting things are lost to me.

 

 

 

 

My bad I code on my idtech 4 game 99.9% of the time in c++, i've been pretty much neglecting Doom script for years now (I can do more stuff in c++ anyway), so I just forget how script works sometimes, sorry for that.

 

That error happens because entity type is not the same as a float type and that is totally right. In c++ you can cast a type to another but script is more restrict, for ease of use.

 

It should have been:

 

if (new_monster) // or even if(new_monster != $null_entity)}      if(new_monster.getHealth( ) < 1))      {         killed += 1;         string myStr = new_monster.getKey("name");         myStr = "The name of zombie_fat is " + myStr + "killed = "  + killed +  "\n";         sys.println(myStr);   // Print the text to the console      }}
but this is solved thanks the problem now is how increment and save value in var float killed.Like i said i using a trigger multiple for restart function then all values reseted, except time var float t by sys.getTime() the value update everytine sure. Edited by games-furious

Share this post


Link to post
Share on other sites

I find this may be if possible use global variable then killed can be update everytime when void function started and health < = 0 look image but editor can compile #include <iostream> ???? or if anybody have a sugestion how use global variable with scripts please post.thanks

post-38514-0-13526400-1558384312_thumb.png

Edited by games-furious

Share this post


Link to post
Share on other sites

I find this may be if possible use global variable then killed can be update everytime when void function started and health < = 0 look image but editor can compile #include <iostream> ???? or if anybody have a sugestion how use global variable with scripts please post.thanks

 

SOLVED! I just put variable out of Void then it work like Global Map Variable very good

Now i can SUM monsters for make one map for ARCADE style for kill monsters in limit of TIME if example kills 20 monsters in 5 minutes you won if you dont you lose kill player and go to restart current level ;)

float killed;  // here variable work like Global Map variable then will increment ++ inside void player( ) when monster killed
 
void kill_player()
{
  code
  ......
  ..........
  ...............
  code
 
}

thanks for all replies here help me a lot =D

 

 

Edited by games-furious
  • Like 1

Share this post


Link to post
Share on other sites

No problem glad to help.

 

Btw sorry if you already know this but Imo global variables are "evil", they can cause all sort of very hard to debug bugs, because they are accessible anywhere in the code (in the same namespace or compilation unit...c++ jargon), even outside the file they are written, so they can clash with variables defined in other script files (if the script is included in some other script) and cause havoc but they do work for stuff like that.

If you don't want that to happen, you can use for map scripts:

namespace my_map{
//put map code here
}

For object scripts use old c preprocessor technique

#ifndef __MYCODE__
#define __MYCODE__

Put your code here

#endif // __MYCODE__

Edited by HMart

Share this post


Link to post
Share on other sites

No problem glad to help.

 

Btw sorry if you already know this but Imo global variables are "evil", they can cause all sort of very hard to debug bugs, because they are accessible anywhere in the code (in the same namespace or compilation unit...c++ jargon), even outside the file they are written, so they can clash with variables defined in other script files (if the script is included in some other script) and cause havoc but they do work for stuff like that.

 

If you don't want that to happen, you can use for map scripts:

 

namespace my_map{

//put map code here

}

 

For object scripts use old c preprocessor technique

 

#ifndef __MYCODE__

#define __MYCODE__

 

Put your code here

 

#endif // __MYCODE__

in this case you tell for i write like this?

 

namespace my_map{
  //put map code here
  float killed;
    
     void kill_player()
     {
      code
         ......
          ..........
            ...............
      code
 
     }

}

Share this post


Link to post
Share on other sites

in this case you tell for i write like this?

 

 

namespace my_map{
  //put map code here
  float killed;
    
     void kill_player()
     {
      code
         ......
          ..........
            ...............
      code
 
     }

}

got error unknow trigger function call

Share this post


Link to post
Share on other sites

in this case you tell for i write like this?

 

namespace my_map{
  //put map code here
  float killed;
    
     void kill_player()
     {
      code
         ......
          ..........
            ...............
      code
 
     }

}

 

Yes if you read the idtech 4 wiki https://modwiki.dhewm3.org/Scripting_basics

 

this is what it says about script namespaces

 

Namespaces

 

These Declare a block which can include functions and variables, avoiding name conflicts. (similar to C++ namespaces)

These are used in Doom 3 to isolate a map script’s functions and variables from the others.

To get access to a member of a namespace, the following syntax is used:

 

namespace::member

 

The main.script and attendant event.script are part of the default, global namespace.

 

got error unknow trigger function call

Have you included your map script inside the doom_main.script? Also see above how to access functions inside a namespace, I would also recommend that you read about OOP (object oriented programing) because doom script follows c++ OOP rules.

Edited by HMart

Share this post


Link to post
Share on other sites

Yes if you read the idtech 4 wiki https://modwiki.dhewm3.org/Scripting_basics

 

this is what it says about script namespaces

 

 

 

 

 

 

Have you included your map script inside the doom_main.script? Also see above how to access functions inside a namespace, I would also recommend that you read about OOP (object oriented programing) because doom script follows c++ OOP rules.

 

no not added my map inside doom_main.script yet. i include many scripts inside my map.script working like threads this scripts not working like mods but only for my map. Edited by games-furious

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...