Jump to content

Lua Table Question


tommysticks

Recommended Posts

I'm trying to alter player XP using a table with this code:

for i, k in pairs(Perks) do
  getPlayer():getXp():AddXP(Perks.k, 10);
end

...and it's not working. When I print Perks.k it comes up nil, but if I print k alone it prints as the userdata from the Perks table, Sprinting as an example. And to check to see if this is the same data as Perks.Sprinting I did a quick "if k == Perks.Sprinting" and it came out true.

 

Any insight on this would be appreciated. @RoboMat if you're still around I figure the issue is probably apparent to you.

Link to comment
Share on other sites

Use Perks.FromString (or Perks.fromIndex if you're iterating numerically). I don't know why, but accessing Perks like a regular table & passing it to a Kahlua function results in errors.

for i, k in pairs(Perks) do
    local perk = Perks.FromString(i)
    if PerkFactory.getPerk(perk) ~= nil then
        getPlayer():getXp():AddXP(perk,10)
    end
end

Some of the "perks" (i.e Agility) are categories, so I'd recommend using PerkFactory.getPerk to ensure that the perk exists.

Edited by ethanwdp
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...