#Creating a Trigger function to let me set a required XP amount using levels but...

1 messages · Page 1 of 1 (latest)

shy olive
#

Creating a Trigger function to let me set a required XP amount using levels but being read by the dummy scoreboard as experience points.

Currently the required level to do something in my data pack is set is 2045 points, which translates to 35 levels. But I want the ability to have a trigger function to be written as a command using levels but the function translates it into points before being stored in my dummy scoreboard to compare the player points to.

Example for trigger: /trigger set XP_Cost 20

Then the function translates that into 550 points.

I can provide my current code for the cost system if needed, but it's basically a dummy scoreboard with a #dummy being compared to the player's XP points via another scoreboard.

Sorry for the lengthy explanation, I wanted it to be fairly easy to understand my goal.

rare mango
#

So what you want to make is a function that take player xp levels and points as an input , turn it into only points and compare it with the cost score ?
The only thing I don't understand is why do you want it to be atrigger function ?

#

So for what I've understood :
To use a trigger "function", you first need to create a trigger scoreboard so players can interact with it.
Then, have a looping function (a tick function or a schedule loop) that checks if this score is set to a value greater than 0 for example. If player's score is greater than 0, it means player used /trigger scoreName set value. So you can run a function as all players having score > 0 in which you will do your maths and your comparison.

#

The maths part is a bit tricky because of the way xp points work

#

The formula to get xp points from levels can be found here https://minecraft.fandom.com/wiki/Experience?so=search#Leveling_up.
In game you can only work with integers so you need to scale those equation up to od math and scale them down when math is done.

Minecraft Wiki

Experience (EXP or XP for short) can be obtained by gathering experience orbs from mining, defeating mobs, breeding, trading, fishing, completing advancements,‌ and using grindstones and furnaces. Experience gained during a player's life affects the player's score on the death screen. While having no direct effect on the player character, it can...

rare mango
#

For example for medium levels (17-31), the equation is
points = 2.5 × level^2 – 40.5 × level + 360 =~ 1/2*(5level(level-16)) + 360
So in minecraft commands, it will be :

scoreboard players operation CalcXp Math = inputLevel Math
#points = level
scoreboard players remove CalcXp Math 16
#points = (level-16)
scoreboard players operation CalcXp Math *= inputLevel Math
#points = (level-16)*level
scoreboard players operation CalcXp Math *= .const5 Math
#points = (level-16)*level*5
scoreboard players operation CalcXp Math /= .const2 Math
#points = ((level-16)*level*5)/2
scoreboard players add CalcXp Math 360

You could also calculate each term of the sum and sum them up but that would take more commands to achieve.
Anyway, when doing math only with integers, you should always divide when your numbers are the biggest as the error will be small.

shy olive
#

You almost got it. The trigger is the change how much the cost is to compare with the player's scoreboard for their XP. So something like 3 scoreboards are present

One for the trigger to get the number in levels
One for the dummy to be compared to the player's XP in experience points
And one for how many levels the player has

The /trigger set xp_cost would only effect the scoreboard for the trigger, then math happens in a function and the points amount from that number get put into another scoreboard to compare against the player's points amount

rare mango
#

I think I don't get it....
Do you want any player to be able to modify what the cost is ?

shy olive
#

Technically just op, but yes, since you can't put /trigger commands behind op. I just won't tell the players about the /trigger commands so hopefully no one tampers

#

This is for a friend's only smp so I think I can trust my players not to mess with the cost

rare mango
#

Does this score need to be a trigger ? If not, you can use a dummy scoreboard so it's protected from non op players

#

Generally trigger scoreboards are used only when you want a non op player to trigger something, a bit like a /home command for example

shy olive
#

Is /function blocked with op? If so I could make a function to do the math after I set the level amount in a dummy scoreboard. Would that work?

rare mango
#

And if used like that, the value of the score does not matter

#

What do you mean by "blocked with op" ?

shy olive
#

Like no non-oped players can use it

rare mango
#

No they can't use it from the game. they cannot type /function

#

But it can be called as the palyer inside another function

#

For example, in the tick function, you can do execute as @a run function ...

shy olive
#

So I could possibly set the number for the dummy scoreboard in game as a level number(like 60) then run /function xp_calc to calculate it into points to be compared to the player's XP points count. Am I thinking this right?

rare mango
#

Yes exactly

#

What do you want to achieve ?

shy olive
#

I want an op to be able to set the XP cost to be able to buy an item instead of going into the data pack functions everytime and setting it that way I want to be able to write the XP cost as levels but due to how the scoreboard objective works I need it to be translated into points so it can be compared to the player's XP amount to ensure they can buy the item

rare mango
#

Ok so in game, any op player can use /scorebaord players set object1Cost <costScoreName> <cost for object1> then when the player tries to buy the object, use what I sent before to get their levels into exp points in a function. Then you just need to use execute if score playerXpPoints <scoreName> matches <objectCost> <costScoreName>

#

But how do you detct when a player tries to buy an object ?

shy olive
#

That bits already been done, I'm just detecting if they've got the item in their inventory and if they don't have enough XP, they get it removed and the item replaces itself

#

This is an item that players cannot obtain in survival

rare mango
#

Ok nice

#

So in your tick function you are doing something like execute as @a[having said item in inventory] run function try_buy_objectx ?

shy olive
#

Basically yeah! It has a tag too, just in case we do an event that includes it so they can get it from a basic give command

shy olive
#

For some reason the function is returning a number just a few experience above where it should be. I put in that it should be 32 levels and it outputs 1640 when it should be 1628. I'm not sure if this would be a problem in the long run, it's just strange

#

I do realize that the function was written for 17-31 but even putting in 20 it's giving me a higher number by 10 points.

#

Would it even be possible to write a function to account for any XP level? Or am I bound to choosing a certain amount

steep bone
#

Only read parts of this but if you want to turn XP into a score and a score into XP you can do so with a recursive function :D
I have one in my Book of Experience data pack, you can just download it and look through the files

https://modrinth.com/datapack/book-of-experience/version/v3.0.0

If you have any questions let me know

#

On phone right now so I can't just give you the function

shy olive
#

I'll look into that!

shy olive
#

I'm also looking for a way to insert the math output into an XP add command is that possible? Because obviously once the player buys the item I want to remove that exact amount of XP. But right now it always just remove 2045 points of experience and not whatever is the output of the math is

steep bone
#

Not directly

steep bone
shy olive
#

Ah. Thanks! Back to coding and copy pasting I go! Lol

shy olive
#

I hate to be so needy here but I'm not great with the scoreboards still, can someone help me out with writing the function to calculate levels to points from 0-16 and 32+ I have 17-31 thanks to Thomas from earlier, but I'm not sure how to do the math for the rest.

rare mango
shy olive
rare mango
rare mango
shy olive
rare mango
rare mango
shy olive
#

for my binary function, this is what I have, but its not working:
execute if score #dummy xp.dummy matches 512.. run xp add @s 512 points
execute if score #dummy xp.dummy matches 512.. run scoreboard players remove in math 512
execute if score #dummy xp.dummy matches 256.. run xp add @s 256 points
execute if score #dummy xp.dummy matches 256.. run scoreboard players remove in math 256
execute if score #dummy xp.dummy matches 128.. run xp add @s 128 points
execute if score #dummy xp.dummy matches 128.. run scoreboard players remove in math 128
execute if score #dummy xp.dummy matches 64.. run xp add @s 64 points
execute if score #dummy xp.dummy matches 64.. run scoreboard players remove in math 64
execute if score #dummy xp.dummy matches 32.. run xp add @s 32 points
execute if score #dummy xp.dummy matches 32.. run scoreboard players remove in math 32
execute if score #dummy xp.dummy matches 16.. run xp add @s 16 points
execute if score #dummy xp.dummy matches 16.. run scoreboard players remove in math 16
execute if score #dummy xp.dummy matches 8.. run xp add @s 8 points
execute if score #dummy xp.dummy matches 8.. run scoreboard players remove in math 8
execute if score #dummy xp.dummy matches 4.. run xp add @s 4 points
execute if score #dummy xp.dummy matches 4.. run scoreboard players remove in math 4
execute if score #dummy xp.dummy matches 2.. run xp add @s 2 points
execute if score #dummy xp.dummy matches 2.. run scoreboard players remove in math 2
execute if score #dummy xp.dummy matches 1.. run xp add @s 1 points
execute if score #dummy xp.dummy matches 1.. run scoreboard players remove in math 1

execute if score #dummy xp.dummy matches 1.. run function smp:setup/xp_binary

#

the #dummy is storing the players previous levels

#

and xp_binary is the name of this function

#

actually, I dont think I've made a scoreboard for math. let me try that

#

I realized my error with the scoreboards, but its still not working. the updated code replaces "in math" with the dummy player and scoreboard

#

I had the math flipped for my calculation of how much to return... my bad lol

rare mango
#

You may need to use more binary stuff because you can only go up to 1023 xp points for now

#

And that's only level 27

#

Maybe going up to 2^15 would be nice, if your friends are not big grinders as it would cover first 100 levels

shy olive
#

thats not a bad idea

rare mango
#

and 2^17 for roughly 200 levels

shy olive
#

also right now, i'm not figuring out how to calculate 0-16, I have:

scoreboard players operation CalcXp Math = inputLevel Math
scoreboard players operation .const12 Math *= CalcXp Math
scoreboard players operation CalcXp Math += inputLevel Math
scoreboard players operation CalcXp Math /= .const2 Math

This however, is not giving me what I want.

The equation I tried going with is 1/2(level+12*level)

#

also since the xp_binary code is set to run again if the player doesnt have 0 xp the code will run regardless of how many levels the player has. however, I think the math from the equations are messing with it.

I tested the xp_binary function by itself by giving my dummy player enough xp for 32 lvls and when I ran the function, it gave them all to me correctly

rare mango
#

For 0-16, equation is points = level^2 + 6*level so by factoring by level you got points = level*(level+6)

#

So the code is like :
calcPoints = inputLevel
calcPoints add 6
calcPoints *= inputLevel

shy olive
#

I've realized that my subtraction method is incomplete. Right now to calculate the amount of xp to give back to the player i'm subtracting dummy from calcxp, and thats not working, its doing the opposite. what should I do to replace this so I can subtract dummy when it is the smaller numeber. the reverse works fine but the following code, obviously tries subtracting from the math output instead of dummy:

execute if score dummy xp.dummy <= CalcXp Math run scoreboard players operation CalcXp Math -= dummy xp.dummy
#

I also believe that the two functions handling this subtraction are being ran multiple times, how might I avoid that?

#

for some reason if I start the transaction with 200 points, it will consistantly give me 5 points back when the cost is set to 55. I should get 145 points. I think this is because of the function looping after activated until I no longer have the item in my inventory

#

just did a test with a say command and that doesnt seem to be happening... I'm not sure what's going on with it but its not returning what it should

#

found out that the problem was the xp quary command was giving me the wrong number

shy olive
#

alright. hopefully this is the last bit of help i'll need on this. I'm working on the equation for 32+ and so far I have:
1/2(9level^2-325level)+2220

this is literally just multiplying by 2 and then dividing by two. is there anything else I need to do to this?

so far the results from this are not reliable and it has only worked on level 40 lol

rare mango
#

The equation is ok for me, the problem may come from how you implemented it with commands

#

should be like this : xpPoints = input level ; xpPoints* 9 ; xpPoints-325; xpPoints * inputLevel; xpPoints / 2 ; xp Points + 2220

#

By the way thank you, I found how to get the exact amount with what you sent. I wanted to factorize too much...

shy olive
#

Oh! The code worked! I was trying the equation in a calculator and must have been inputting wrong. I'm glad I could help, and thanks for helping me so much!