How to transfer damage taken from a class to another?

You need help? It's here!
LegendaryAgent
Member
Posts: 80
Joined: Thu 29 Sep , 2011 12:26 am
Contact:

How to transfer damage taken from a class to another?

Post by LegendaryAgent » Tue 04 Oct , 2011 6:50 pm

Hey guys its me again, what im trying to do right now is to make 2 classes which are going to be some monsters, 1 of that class is an extension to another with the only difference beying the way the function

Code: Select all

TakeDamage(int Damage, Pawn instigatedBy, Vector hitlocation, Vector momentum, class<DamageType> damageType)
works.

What i want to do is, everytime the second class takes damage, instead of getting itself damaged to transfer the damage taken to the first class, i have attempted something like this in the second class:

Code: Select all

function TakeDamage(int Damage, Pawn instigatedBy, Vector hitlocation, Vector momentum, class<DamageType> damageType)
{
FirstClass.Health-=Damage;
return;

 super.TakeDamage(Damage,instigatedBy,hitlocation,momentum,damageType);
}

but by doing this even the second class doesnt get damaged it doesnt subtract health sucessfully from the first class, is there any simple way to achieve this?

PS: these 2 classes are unique in the game so no duplicates will be available.

User avatar
Azarael
UT2004 Administrator
Posts: 5365
Joined: Thu 11 Feb , 2010 10:52 pm

Re: How to transfer damage taken from a class to another?

Post by Azarael » Tue 04 Oct , 2011 7:16 pm

How are these monsters linked? Is one spawning the other when it's created? You need the second monster to have a reference to the first one, then call TakeDamage on the first one using FirstClassRefHere.TakeDamage(params).

return followed by a call to the superclass version of the function is pointless - remove both of those.

Assuming the first and second monsters are designed to work in pairs, have the first monster spawn the second in some function, then using that function, set a Monster variable in the second monster to be the first monster:

Code: Select all

function SpawnChildMonster()
{
local Monster M;
M = Spawn(monstershithere);
if (M != None)
M.FirstMonster = self;
else Destroy(); //couldn't spawn secondary monster
}
If you're trying to make all active instances of FirstClass take damage when an instance of SecondClass takes damage, you're either going to have to iterate or keep a list somewhere of all instances of FirstClass that are created, then traverse that list in SecondClass' TakeDamage function.

LegendaryAgent
Member
Posts: 80
Joined: Thu 29 Sep , 2011 12:26 am
Contact:

Re: How to transfer damage taken from a class to another?

Post by LegendaryAgent » Tue 04 Oct , 2011 7:58 pm

Azarael wrote:How are these monsters linked? Is one spawning the other when it's created? You need the second monster to have a reference to the first one, then call TakeDamage on the first one using FirstClassRefHere.TakeDamage(params).

return followed by a call to the superclass version of the function is pointless - remove both of those.

Assuming the first and second monsters are designed to work in pairs, have the first monster spawn the second in some function, then using that function, set a Monster variable in the second monster to be the first monster:

Code: Select all

function SpawnChildMonster()
{
local Monster M;
M = Spawn(monstershithere);
if (M != None)
M.FirstMonster = self;
else Destroy(); //couldn't spawn secondary monster
}
If you're trying to make all active instances of FirstClass take damage when an instance of SecondClass takes damage, you're either going to have to iterate or keep a list somewhere of all instances of FirstClass that are created, then traverse that list in SecondClass' TakeDamage function.

Yes the first monster spawns the second on postbeginplay, the second monster is an extension of the first one with only difference beying that the second monster will not spawn another monster and that it itself cant be damaged to be killed i want it to redirect the damage to the first monster instead and when the first monster hp reaches 0 the secondary monster should also get busted.
For all instances of first class to be given damage is not necessary, i just thought this was easier.

LegendaryAgent
Member
Posts: 80
Joined: Thu 29 Sep , 2011 12:26 am
Contact:

Re: How to transfer damage taken from a class to another?

Post by LegendaryAgent » Tue 04 Oct , 2011 8:26 pm

alright here is what i have:

Code: Select all

function PostBeginPlay()
{
	Super.PostBeginPlay();
	if( MonsterController(Controller)!=None )
		MonsterController(Controller).CombatStyle = CombatStyle;

	getbackup();
}

function getbackup()
{
	local vector X, Y, Z, SpawnOffSet;
    local Monster M;

	if(Controller != None)
	{
		GetAxes(Rotation,X,Y,Z);
		SpawnOffSet = X;
        M=Spawn(class'DreamsCombineBackup1A',self,,location + 200 * SpawnOffSet + (Y) + (3 * Z),Rotation);
        if (M != None)
        {
        M.FirstMonster = self;
        if(Invasion(Level.Game) != None)
          Invasion(Level.Game).NumMonsters++;
        }
        else Destroy(); //couldn't spawn secondary monster
	}
}
Now how do i use this?

EDIT: ugh i dont understand the m.firstmonster = self; :S what is it for exactly?

User avatar
Azarael
UT2004 Administrator
Posts: 5365
Joined: Thu 11 Feb , 2010 10:52 pm

Re: How to transfer damage taken from a class to another?

Post by Azarael » Tue 04 Oct , 2011 8:58 pm

Having looked into this further, you can just use the Owner variable, as the child monster is owned by the parent monster. So the code would be like so:

function getbackup()
{
local vector X, Y, Z, SpawnOffSet;
local Monster M;

if(Controller != None)
{
GetAxes(Rotation,X,Y,Z);
SpawnOffSet = X;
M=Spawn(class'DreamsCombineBackup1A',self,,location + 200 * SpawnOffSet + (Y) + (3 * Z),Rotation);
if (M != None)
{
if(Invasion(Level.Game) != None)
Invasion(Level.Game).NumMonsters++;
}
else Destroy();
}
}

Then, in your child class:

function TakeDamage (lots of params here)
{
if (Owner != None)
Owner.TakeDamage(lots of params here);
else <insert here your method of choice for terminating this monster, be it takedamage, destroy, died, whatever>
}

LegendaryAgent
Member
Posts: 80
Joined: Thu 29 Sep , 2011 12:26 am
Contact:

Re: How to transfer damage taken from a class to another?

Post by LegendaryAgent » Tue 04 Oct , 2011 9:46 pm

Azarael wrote:Having looked into this further, you can just use the Owner variable, as the child monster is owned by the parent monster. So the code would be like so:

function getbackup()
{
local vector X, Y, Z, SpawnOffSet;
local Monster M;

if(Controller != None)
{
GetAxes(Rotation,X,Y,Z);
SpawnOffSet = X;
M=Spawn(class'DreamsCombineBackup1A',self,,location + 200 * SpawnOffSet + (Y) + (3 * Z),Rotation);
if (M != None)
{
if(Invasion(Level.Game) != None)
Invasion(Level.Game).NumMonsters++;
}
else Destroy();
}
}

Then, in your child class:

function TakeDamage (lots of params here)
{
if (Owner != None)
Owner.TakeDamage(lots of params here);
else <insert here your method of choice for terminating this monster, be it takedamage, destroy, died, whatever>
}

as usual thanks alot for ur replies, there are 2 problems right now,
lets call secondaryclass by its name: DreamsCombineBackup1A
and the primaryclass aswell: DreamsBossHunter1A

i have tried this in the DreamsCombineBackup1A:

Code: Select all

function TakeDamage(int Damage, Pawn instigatedBy, Vector hitlocation, Vector momentum, class<DamageType> damageType)
{
 if (Owner != None)
 {
 Owner.TakeDamage(Damage,instigatedBy,hitlocation,momentum,damageType); //doesnt work because i dont know what are the right parameters to put in here
 return; //you have said theres no need for return, but if i dont use return the monster will get damaged to its own hp
 }

 super.TakeDamage(Damage,instigatedBy,hitlocation,momentum,damageType);
}
If i use this 2 problem happen:
1) the dreamsbosscombine1A doesnt register taking any damage
2) even if i kill the dreamsbosscombine1A directly the "if (Owner != None)" still returns double negative (as if it still detects its owner).

Im sorry to bother you like this, but my experience with this type of syntax and right parameters used in uscript is rly small, could u help me on this once more? i usually learn from examples like these and in future i learn how to apply them as i already have examples on how to xD

Thanks as usual in advance!

User avatar
Azarael
UT2004 Administrator
Posts: 5365
Joined: Thu 11 Feb , 2010 10:52 pm

Re: How to transfer damage taken from a class to another?

Post by Azarael » Tue 04 Oct , 2011 10:02 pm

Remove both return and the superclass call (Super.TakeDamage). As for the actual problem... you may have to try the first method I suggested (that is, creating a Monster variable in Combine which is set in getbackup to be the Boss monster (M.FirstMonster = self) and then modify the code to use FirstMonster.TakeDamage instead of Owner.TakeDamage.

LegendaryAgent
Member
Posts: 80
Joined: Thu 29 Sep , 2011 12:26 am
Contact:

Re: How to transfer damage taken from a class to another?

Post by LegendaryAgent » Tue 04 Oct , 2011 10:39 pm

Azarael wrote:Remove both return and the superclass call (Super.TakeDamage). As for the actual problem... you may have to try the first method I suggested (that is, creating a Monster variable in Combine which is set in getbackup to be the Boss monster (M.FirstMonster = self) and then modify the code to use FirstMonster.TakeDamage instead of Owner.TakeDamage.

Alright this is what i have right now:

DreamsBossHunter1A (this is the main)

Code: Select all


var Monster FirstMonster;

function PostBeginPlay()
{
	Super.PostBeginPlay();
	if( MonsterController(Controller)!=None )
		MonsterController(Controller).CombatStyle = CombatStyle;

	getbackup();
}

function getbackup()
{
  local vector X, Y, Z, SpawnOffSet;
  local Monster M;

  FirstMonster=self;

  if(Controller != None)
    {
    GetAxes(Rotation,X,Y,Z);
    SpawnOffSet = X;
    M=Spawn(class'DreamsCombineBackup1A',self,,location + 200 * SpawnOffSet + (Y) + (3 * Z),Rotation);
    if (M != None)
      {
        if(Invasion(Level.Game) != None)
          Invasion(Level.Game).NumMonsters++;
      }
    else Destroy();
    }
}
DreamsCombineBackup1A (this is the secondary);

Code: Select all

function PostBeginPlay()
{
	Super.PostBeginPlay();
	if( MonsterController(Controller)!=None )
		MonsterController(Controller).CombatStyle = CombatStyle;
}

function getbackup()
{
//nothing
}

function TakeDamage(int Damage, Pawn instigatedBy, Vector hitlocation, Vector momentum, class<DamageType> damageType)
{
 if (FirstMonster != None)
 {
 FirstMonster.TakeDamage(Damage,instigatedBy,hitlocation,momentum,damageType);
 }
}
i have no errors but when i shoot the secondary boss, the main boss still doesnt get damaged, did i declare the first monster right?

Thanks as usual for the continuous patience

User avatar
Azarael
UT2004 Administrator
Posts: 5365
Joined: Thu 11 Feb , 2010 10:52 pm

Re: How to transfer damage taken from a class to another?

Post by Azarael » Tue 04 Oct , 2011 10:51 pm

var FirstMonster should be in Combine, not Boss. Amend the line which currently says FirstMonster = self; to say M.FirstMonster = self; and move it to after M=Spawn...

LegendaryAgent
Member
Posts: 80
Joined: Thu 29 Sep , 2011 12:26 am
Contact:

Re: How to transfer damage taken from a class to another?

Post by LegendaryAgent » Wed 05 Oct , 2011 12:06 am

Azarael wrote:var FirstMonster should be in Combine, not Boss. Amend the line which currently says FirstMonster = self; to say M.FirstMonster = self; and move it to after M=Spawn...
EDIT: Oh wait i think i get it, ill edit this post soon

Edited:
Do you mean like this?

DreamsBossHunter1A

Code: Select all

function getbackup()
{
  local vector X, Y, Z, SpawnOffSet;
  local Monster M;

  if(Controller != None)
    {
    GetAxes(Rotation,X,Y,Z);
    SpawnOffSet = X;
    M=Spawn(class'DreamsCombineBackup1A',self,,location + 200 * SpawnOffSet + (Y) + (3 * Z),Rotation);
    M.FirstMonster=self;
    if (M != None)
      {
        if(Invasion(Level.Game) != None)
          Invasion(Level.Game).NumMonsters++;
      }
    else Destroy();
    }
}
DreamsCombineBackup1A

Code: Select all

var Monster FirstMonster;

function getbackup()
{
//nothing
}

function TakeDamage(int Damage, Pawn instigatedBy, Vector hitlocation, Vector momentum, class<DamageType> damageType)
{
 if (FirstMonster != None)
 {
 FirstMonster.TakeDamage(Damage,instigatedBy,hitlocation,momentum,damageType);
 }
}
if i do it like that i get this:
DreamsBossHunter1A.uc(181) : Error, Unrecognized member 'FirstMonster' in class 'Monster'

Post Reply

Who is online

Users browsing this forum: No registered users and 30 guests