Creating an infinite table?

Is there a way to create a table of values that is infinite?

ie

1 = “2”
2 = “5”
3 = “9”
so on and so forth

without causing an inf loop and messing everything up

or would i need to put a cap on the max

A table of infinite values requires a computer with infinite storage space – neither of which is realistically plausible.

Can you try to explain what you’re wanting in a little more detail?

leveling
i want to create a table of the required experience to get to the next level based on an equation

the said table needs to be able to update based on the same equation if it changes at all (variable changed) without having to manually change it

the answer is probably looking me right in the face XD so hyped up on caffeine the little man in my head is to busy scrambling my brain instead of working with me…

If you can define your growth as a function, you can do something like this:


function GetRequiredXP( target_level )
  return target_level * 70 + target_level ^ 2 * 3.5 + 30
end

So instead of trying to calculate all possible values, you just calculate them as you need it - this way your maximum is when you would overflow a double instead of when you get tired of filling a table.

what about defining the required experience in the same spot i am storing the current experiance
so have to sql values
totalexp = 234
exptolvl = 250

You’d do something like


local needed = GetRequiredXP( ply:GetLevel() + 1 ) - ply:GetXP()

Assuming that’s what you mean. You get the XP needed at the player’s next level and subtract their current XP, which gives you the amount they need to get to hit the next level.

would also like to be able to reset my experience to 0 after every lvl (so levels dont change if equation is changed)

Unless I’m forgetting something, doing that would be as trivial as modifying the above code to also subtract the XP needed for the player’s current level.


local needed = GetRequiredXP( ply:GetLevel() + 1 ) - GetRequiredXP( ply:GetLevel() ) - ply:GetXP()

E.g.:

  • Player is level 4.
  • Level 5 requires 500 XP
  • Level 4 requires 300 XP
  • Player has 25 XP (if you reset to 0 after each level).

500 - 300 - 25 = 175 XP needed to hit level 5, since the difference between level 4 and 5 is 200 XP.

You can change the underlying algorithm used to calculate the experience points at a particular level without having to change the snippet that I wrote. You can make leveling easier or harder and you wouldn’t have to change anything besides the algorithm.

so lvl 4 player has 0 exp

in order to get to lvl 5 they need 6 exp
when they get 6 exp
they lvl up
in order to lvl up again they need 8 exp
they currently have 0

the reason i want this is lets say someone decides later that it is to easy to lvl so they make it harder, i dont want the lvls to go down becouse i have “skillpoints” based on the levels that they can insert into “skills”

The player starts with 0 xp and earns it by doing whatever.

If their xp >= needed xp ( level + 1 ) then
increase the level by 1
increase points and whatever else by however many
set xp = 0

No need to try to track the difference between the last level and the current level.

think i’ve got it, thanks you guys were a huge help

figured i would post my solution
[lua]
function inv_exptolvl (ply)
local lvl = ply.lvl
return math.pow(lvl,1.5)
end

function inv_levelup (ply)
if ply.exper >= inv_exptolvl(ply) then
ply.lvl = ply.lvl + 1
ply.sp = ply.sp + 10
ply.exper = ply.exper - inv_exptolvl(ply)
inv_levelup (ply)
end
end
[/lua]

Wont this cause the player to go into the negative with XP ?
Since you are increasing his lvl before you decrease his XP.

the level is increased
ply.lvl = ply.lvl + 1

then the experience required to level is subtracted from the experience (incase the player gained more exp then was required to lvl)
ply.exper = ply.exper - inv_exptolvl(ply)

then it checks again to make sure they didnt lvl up 2 times at once
inv_levelup (ply)

on the second check if their experience is not high enough to level then it stops the player should never level up without gaining the experience needed to level up so the lowest experience they should have is 0

This is actually possible with metatables as long as the value you want can be calculated based off the key you lookup. But you already got a good solution so yeah.

any information on where to find a good use/ description of meta tables? i have yet to find any real informative information on them

still dont understand it XD, lua is my first language sadly and im still mostly new to it (reason i decided to make a gamemode so that i can expand on that knowledge)

I need to see usage in a practical scenario in order to understand something like lua

FindMetaTable

Here’s an example of an “infinite” table containing the fibonacci sequence.



-- note: we don't add anything to the 't' table, it merely acts as a "backing" table
-- for the metatable
local t = {}

local meta = {}
function meta:__index(key)
	if key <= 2 then
		return 1
	end
	return self[key - 1] + self[key - 2]
end

setmetatable(t, meta)

print(t[6])


For your purpose you should use a normal function. Metatables are fancy but fancy is usually a bad thing in programming, because it makes understanding code harder.

Also FindMetaTable has nothing to do with Lua metatables.