Page 1 of 1

How do I: Use MissionLua to modify Unit Stats live?

Posted: Sat Apr 07, 2007 9:52 pm
by Ace_Azzameen_5
How do I use Lua script, upon an event, (i.e. flag pick up) to change the statistics of a particular unit? I tried looking at Zerted 's holocron mode but I couldn't interpret what I needed by myself.

It seems that the code was choosing targets of functions in one spot of the code and putting them into strings or something, then referring to them later, and I couldn't keep track of it.

My ultimate goal is to spawn pick up weapons, in theory it is extremely simple knowing that all the required engine capabilities have successfully been exploited in Archer's Ferret Master alone ([size = 7] Oddly enough, he didn't implement weapons pickups[/size])

Basically, you take a flag odf, change the msh to a gun msh, spawn the flag in a map, and once the flag is picked up, an the onflagpickup event is triggered, the unit that picked up the weapon is read-in, and then its stats are changed to give it a fitth weapon or replace an existing one.

Any code experts see anything wrong with this?

Re: How do I: Use MissionLua to modify Unit Stats live?

Posted: Sun Apr 08, 2007 1:12 pm
by [RDH]Zerted
A possible problem:
If you are going to use flags as the base model for your weapons each unit will only be able to carry one (I think), you have to remove the 'Reset flag - carried to long', and you have to decide what happens when the unit dies (flags get dropped then reset, what do you want the weapon to do?).

I think it would be better to model the weapons after the health/ammo packs.

Code: Select all

--
-- When a player pickups a flag object
--
OnFlagPickUp(
    function(flagPtr, carrierObj)
        print("Someone picked up a flag")

        --get the weapon name
        local weaponName = GetEntityName(flagPtr)
        if( weaponName == nil ) then
            print("FlagPickup: Error: Unknown entity.  Can this even happen?")
            return
        end
        print("The flag is really a " .. weaponName )
    
        --get the unit who picked up the weapon
        local unit = GetCharacterUnit(carrierObj)
        if( unit == nil ) then
            print("FlagPickup: Error: No one did pick up something.  Can this even happen?")
            return
        end
        print("The player who picked up the " .. weaponName .. " is " .. unit)

        --give the unit the weapon
        GiveWeapon( weaponName, unit)
        print("Weapon pickup complete: Unit: " .. unit .. " Weapon: " .. weaponName )
    end
)--end of OnFlagPickUp()

--
-- Gives the given weapon to the given unit
--
local GiveWeapon = function( name, unit )
    --check input for errors
    if( name == nil) then
        print("GiveWeapon(): Error: Called with no weapon name")
        return
    elseif( unit == nil ) then
        print("GiveWeapon(): Warning: Called with no unit.  Unit already died?")
        return
    end

    --handle the different weapons
    if( name == "Super Pistol" ) then
        --set unit's weapon 1 to the super pistol
        SetProperty( unit, "WeaponName1", "cus_weapon_drop_suppistol" )
        SetProperty( unit, "WeaponAmmo1", "0" ) --set umlimited ammo
    elseif( name == "Tacks" ) then
        --set unit's weapon 3 to the tack weapon
        SetProperty( unit, "WeaponName3", "cus_weapon_drop_tacks" )
        SetProperty( unit, "WeaponAmmo3", "500" ) --set 500 ammo
    elseif( name == "Penceil" ) then
        --set unit's weapon 1 to the penceil weapon
        SetProperty( unit, "WeaponName1", "cus_weapon_drop_penciel" )
        SetProperty( unit, "WeaponAmmo1", "0" ) --set unlimited penceil jabs/ammo
    elseif( name == "A-Bomb" ) then
        --set unit's weapon 4 to the A-Bomb
        SetProperty( unit, "WeaponName4", "cus_weapon_drop_abomb" )
        SetProperty( unit, "WeaponAmmo4", "1" ) --only give one bomb
  --elseif( name == "New Weapon 5" ) then
      --SetProperty( unit, <weapon location>, <weapon odf> )
  --elseif( name == "New Weapon 6" ) then
      --SetProperty( unit, <weapon location>, <weapon odf> )
  --etc...
    else
        print("GiveWeapon(): Error: Unknown weapon type: " .. name )
    end
end--end of GiveWeapon function
I included a bunch of error handling. All the printf() statements are for debug output.

The code is simple. OnFlagPickUp() is called by the game when a unit pick-ups a flag. First you determine which weapon was picked-up, then you determine which unit did the picking. Next, a function is called which sets the unit's properties depending on the weapon (gives the weapon to the unit)

The properties are changed on the player's unit, not the player's character, so any changes are lost when the player respawns.

Note: I have only tried changing the weapons on a unit class (ex: all snipers) not a single unit. I have not tested the SetProperty() on a unit. I have tested the SetClassProperty() on a unit class:

Code: Select all

--successful attempt at changing all rifleman's rifles to a wookiee's bow caster with unlimited ammo
SetClassProperty("all_inf_rifleman", "WeaponName1", "all_weap_inf_bowcaster" )
SetClassProperty("all_inf_rifleman", "WeaponAmmo1", "0" )
( If someone would get the wiki working, I would post a bunch of code snippets like the GiveWeapon() )

RE: Re: How do I: Use MissionLua to modify Unit Stats live?

Posted: Sun Apr 08, 2007 11:21 pm
by Ace_Azzameen_5
About problem with flag, well, maybe I chose the wrong class. But Archer had objects in his map and would disappear when you walked into them, but they would not be carried like a flag or your holocron. They just triggered an event and once all 8 were collected, a class was unlocked, well, added.