melindil / FTSE

Fallout Tactics Scripting Engine
MIT License
7 stars 1 forks source link

Not possible to modify experience and reputation #2

Closed 10Dozen closed 1 month ago

10Dozen commented 3 years ago

I am running this code to update player's experience and rep:

function OnLongTick(e)
    if e:GetName() == world:GetPlayer():GetName() then
        bonus = { reputation=5 }
        e:ApplyBonus(bonus, true)

        bonus = { experience=1000 }
        e:ApplyBonus(bonus, true)
    end
end

In log i see

2021/07/27 13:35:25.384 Rex applying bonus
2021/07/27 13:35:25.384 Editing offset 0x2d to 5
2021/07/27 13:35:25.384 Editing offset 0x29 to 1000

but no changes in game UI. Tested on Tutorial - Basic scenario.

Looks like 'Traits' section (in FT Tools) is not editable.

Also I look for something like a hook to edit amount of gained expirience before actually applying it (and causing level-up event), so editing gained expirience is not what i really need. Is it possible?

melindil commented 1 month ago

In the short term, you might be able to use the new vtable function support to do this (requires version 0.55a). It's a bit clunky, so long term I'll add functions that do this under the covers.

To add XP for an entity (for all examples below, assume the entity is in a variable named 'ent'), use vtable function 370 (GrantXP), second parameter is amount of XP to give:

ent:CallVtable(370, 1000)

To add reputation, use vtable function 215 (AddToReputation):

ent:CallVtable(215, 100)

You can also set a hook for the GrantXP vtable function, this should give you the ability to see when XP is added (unless the game has alternate code to grant XP in specific cases):

function VtableHookGrantXP(ent, amount)
  -- edit amount here as you need
  amount = 1000

  -- call original vtable function to add the new XP amount
  -- or skip the call to not grant any XP
  ent:CallOrigVtable(370, amount)
end

function OnStart()
  hookexecutor:InstallVtableHook("Actor",370, VtableHookGrantXP)
end

Edit: Fixed a missing parameter in InstallVtableHook call.

melindil commented 1 month ago

Quick notes:

  1. The ApplyBonus method doesn't work here for the same reason bonuses, weapons, etc. can't increase perks, traits, etc. The bonus values are added to the "temporary" attribute table, for which only SPECIAL, skills, and resistances are properly copied.
  2. In most cases, we will prefer to use a built-in game function (usually a vtable function), rather than manipulating fields directly in the entity data. This is necessary in order to have any side effects of stat changes properly applied. For example, in the case of XP gain, a side effect is the possibility of increasing in level when XP reaches a certain amount. By using the built in function, we don't need to add a bunch of code to handle these side effects ourselves.
melindil commented 1 month ago

Feature included in 0.56a.