Page 1 of 1

LUA Help: Adding Health

Posted: Thu Oct 04, 2007 11:12 pm
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

Re: LUA Help: Adding Health

Posted: Thu Oct 04, 2007 11:52 pm
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.

Re: LUA Help: Adding Health

Posted: Fri Oct 05, 2007 12:13 am
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
            ) 
:?

Re: LUA Help: Adding Health

Posted: Fri Oct 05, 2007 12:23 am
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.

Re: LUA Help: Adding Health

Posted: Fri Oct 05, 2007 5:41 pm
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)

Re: LUA Help: Adding Health

Posted: Fri Oct 05, 2007 7:06 pm
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:

Re: LUA Help: Adding Health

Posted: Fri Oct 05, 2007 11:18 pm
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.