LUA Help: Adding Health

In this forum you will find and post information regarding the modding of Star Wars Battlefront 2. DO NOT POST MOD IDEAS/REQUESTS.

Moderator: Moderators

Post Reply
MasterSaitek009
Black Sun Slicer
Posts: 619
Joined: Wed Aug 23, 2006 4:10 pm

LUA Help: Adding Health

Post by MasterSaitek009 »

Code: Select all

OnObjectDamage( 
			function(object, damager)
				 if GetCharacterTeam(player) == DEF then
					if  GetCharacterClass(damager) == 2 then
						SetProperty (damager, "AddHealth", 20)
					end 
				end
			end 
				) 
So here is what I want to happen:

Unit damages another unit
Damager, if it is a certain certain class and it is the defender team, recieves 20 health

It should be simple but the above code is generating the below error:

Code: Select all

Message Severity: 2
.\Source\LuaCallbacks_Mission.cpp(635)
Entity "0" not found
What am I doing wrong?
:?

If you help me your name will be added to the readme of the map that this code goes in,
which should be released before Christmas.
Thanks,
-Saitek

And just for the record we need an LUA forum :P
User avatar
[RDH]Zerted
Gametoast Staff
Gametoast Staff
Posts: 2982
Joined: Sun Feb 26, 2006 7:36 am
Projects :: Bos Wars AI - a RTS game
Games I'm Playing :: SWBF2 and Bos Wars
xbox live or psn: No gamertag set
Location: USA
Contact:

Re: LUA Help: Adding Health

Post by [RDH]Zerted »

You do have some problems. First, what is player? Second, you are assuming the damager is still alive, he may not be (use nil checks). Third, you have to add health to the damager's unit, not the character itself. Use a nil check before the first time you use a GetCharacter function and another one on the unit you get from your missing GetCharacterUnit() call. Also, I believe adding a value to AddHealth will give that player a health regen, not increase the player's health by that amount.
MasterSaitek009
Black Sun Slicer
Posts: 619
Joined: Wed Aug 23, 2006 4:10 pm

Re: LUA Help: Adding Health

Post by MasterSaitek009 »

First, what is player?
Oops...
I was messing around with different ways of doing it I forgot to switch it back to the actual one

So would this work?

Code: Select all

OnObjectDamage(
         function(object, damager)
         if  damager != nil then
             if GetCharacterTeam(damager) == DEF then
               if  GetCharacterClass(damager) == 2 then
                 //So what would go here then?
                 // something like
                  if  damager != nil then
                 SetProperty (GetCharacterUnit(damager), "CurHealth",  CurHealth +20)
                  end
               end
            end
          end
         end
            ) 
:?
User avatar
[RDH]Zerted
Gametoast Staff
Gametoast Staff
Posts: 2982
Joined: Sun Feb 26, 2006 7:36 am
Projects :: Bos Wars AI - a RTS game
Games I'm Playing :: SWBF2 and Bos Wars
xbox live or psn: No gamertag set
Location: USA
Contact:

Re: LUA Help: Adding Health

Post by [RDH]Zerted »

More like

Code: Select all

OnObjectDamage(
         function(object, damager)

             --skip invalid players
             if damager == nil then return end

             --check player's team and class
             if GetCharacterTeam(damager) == DEF then
                 if GetCharacterClass(damager) == 2 then

                     --get the player's unit object
                     local unit = GetCharacterUnit(damager)

                     --skip dead units
                     if unit == nil then return end

                     --set the player's health to 80
                     SetProperty(unit, "CurHealth", 80)
                 end
             end
         end
            ) 
I don't know of a way to get the player's current health. Just saying CurHealth + 20 won't do it. I guess you could use IsCharacterAlive() instead of the nil unit check, but I've never done it that way.
User avatar
[RDH]Zerted
Gametoast Staff
Gametoast Staff
Posts: 2982
Joined: Sun Feb 26, 2006 7:36 am
Projects :: Bos Wars AI - a RTS game
Games I'm Playing :: SWBF2 and Bos Wars
xbox live or psn: No gamertag set
Location: USA
Contact:

Re: LUA Help: Adding Health

Post by [RDH]Zerted »

Good news, I did find method to get an object's health. Its GetObjectHealth(). Use it on the player's unit.

Retail Example:
SetProperty("turbineconsole", "CurHealth", GetObjectHealth("turbineconsole") + 1)
MasterSaitek009
Black Sun Slicer
Posts: 619
Joined: Wed Aug 23, 2006 4:10 pm

Re: LUA Help: Adding Health

Post by MasterSaitek009 »

So final trigger of:

Code: Select all

OnObjectDamage(
         function(object, damager)
             if damager == nil then return end
             if GetCharacterTeam(damager) == DEF then
                 if GetCharacterClass(damager) == 2 then
                     local unit = GetCharacterUnit(damager)
                     if unit == nil then return end
                     SetProperty(unit, "CurHealth", GetObjectHealth(unit) + 1)
                 end
             end
         end
            )
I'll test it out!
Thanks a lot Zerted :thumbs:
User avatar
[RDH]Zerted
Gametoast Staff
Gametoast Staff
Posts: 2982
Joined: Sun Feb 26, 2006 7:36 am
Projects :: Bos Wars AI - a RTS game
Games I'm Playing :: SWBF2 and Bos Wars
xbox live or psn: No gamertag set
Location: USA
Contact:

Re: LUA Help: Adding Health

Post by [RDH]Zerted »

That will increase the unit's health by 1. It might be hard to test an increase of 1. Also, there might be a problem if the unit's CurHealth goes over its MaxHealth. I never did that to see what happens.
Post Reply