#DynamicProperty
1 messages · Page 1 of 1 (latest)
DynamicProperty
Is this for an itemStack that is in a container or an entity's inventory? If so you should be using the get/set dynamic property methods on the container slot instead of getting the item. Otherwise you need to manually replace the item in the container.
No, it is just a Dynamic Property whose value is an Array, by replacing part of the Array with something else and then printing it, so it was printed after replacing the part, but when I printed it in another function, it was printed before the replacement.
Perhaps showing code might help.
Ok , wait
const nowClanDp = world.getDynamicProperty("nowClan")
let clanrole;
const inClan = world.getDynamicProperty(playerName + "_inClan");
if (inClan) {
clanrole = nowClanDp.split("/")[1];
}
const NewClanRoleDp = nowClanDp?.replace(clanrole, "Minato");
world.setDynamicProperty("nowClan", NewClanRoleDp);
When I print the dynamic property "nowClan" right after the code, it is printed after the replacement, but when I print it in another function, it is printed before the replacement.
Sorry, I'm confused in what you're actually trying to do.
I only changed part of the Dp, and I saved it, but when I call it in another function, the old one is called.
Technically, this code is written inside the function?
Yes
So your problem is when you call it, it works fine as you expect. And then when you call it again from another function it works differently?
Yes exactly
When you call the function the first time, it works as intended. But the second you call it, it will run the code again redoing all stuff again. So this might be the reason why it won't work as you intend to do.
Maybe provide more info on what you're actually trying to do.
No, I have already modified more than one Dp and called it in another function and no problem occurred
Perhaps the problem is not within that function only.
I didn't understand what you mean
I mean the problem could also be from another code affecting it.
I don't think so, I just updated the Dp and called it a second time
Couldn't tell the problem if we can't even see it. We can't debug something we don't even know what to debug.
The snippet isn't enough to tell the actual problem.
I cannot send you the original code because it is long and contains other unrelated things, but I will write code similar to what you did.
@formal cloud
function FirstFunction(player) {
//Create the Dp in the first function
//
//
const MyDp = "MyDp";
const Array = ["One", "Tow", "Three"]
world.setDynamicProperty(player.name + "_PlayerDp", MyDp)
world.setDynamicProperty(MyDp, player.name + "/" + Array + "/");
}
function SecondFunction(player) {
//Change the Dp value in the second function
//
const Playerdp = world.getDynamicProperty(player.name + "_PlayerDp");
const Worlddp = world.getDynamicProperty(Playerdp);
const lastName = Worlddp.split("/")[0];
const newdp = Worlddp.replace(lastName, "Test");
world.setDynamicProperty(Playerdp, newdp);
}
function ThirdFunction(player) {
//Call the Dp after the change
//
const pDp = world.getDynamicProperty(player.name + "_PlayerDp")
player.sendMessage(world.getDynamicProperty(pdp).split("/")[0]); //It is supposed to print "Test", but it prints the name of the player (player.name), that is, before the change
};
I test your code it print "Test"
I call it in this order
firstFunction(player);
secondFunction(player);
thirdFunction(player);
I analyze how your code, sems nothing wrong, but your DP system is very confusing and hard to understand, idk where and how do you use it. Maybe try to store your DP into JSON string, this allows you to easily manage DP and easier to understand too.
This is how I analyze your code
/* Assume "Steve" is the player name
* The comments show how the string will look like for each line
*/
function FirstFunction(player) {
//Create the Dp in the first function
const MyDp = "MyDp";
const Array = ["One", "Two", "Three"]
world.setDynamicProperty(player.name + "_PlayerDp", MyDp) // id: "Steve_PlayerDp", value: "MyDp"
world.setDynamicProperty(MyDp, player.name + "/" + Array + "/"); // id: "myDP" value: "Steve/One,Two,Three value"
}
function SecondFunction(player) {
//Change the Dp value in the second function
const Playerdp = world.getDynamicProperty(player.name + "_PlayerDp"); // return "myDp"
const Worlddp = world.getDynamicProperty(Playerdp); // return "Steve/One,Two,Three/"
const lastName = Worlddp.split("/")[0]; // return "Steve"
const newdp = Worlddp.replace(lastName, "Test"); // return "Test"
world.setDynamicProperty(Playerdp, newdp); // id: "MyDp", value: "Test"
}
function ThirdFunction(player) {
//Call the Dp after the change
const pDp = world.getDynamicProperty(player.name + "_PlayerDp"); // return "MyDp"
//It is supposed to print "Test", but it prints the name of the player (player.name), that is, before the change
player.sendMessage(world.getDynamicProperty(pDp).split("/")[0]); // "Test/One,Two,Three/" -> ["Test", "One,Two,Three", ""] -> index 0 is "Test"
};
Brah, I tried it and printed MOHAMEDa9 (player.name)