Iv been stuck on this for hours and am about to give up.
Iv gone trough the documentation and cant seem to find anything that works.
All i need is to help ChatGPT check if an object has variables before it gets deleted and if so which ones, so that it can store them to restore them back to the object later.
But no matter what documentation i feed it, i cant seem to find the right examples to help it along…
This isn’t fully tested but it’s mostly copied from the extended variables extension.
It needs testing and some safe guards.
If the NewSprite1 has a variable named “Apple” it copies all of the variables to a temp object (TempVariables) and then from TempVariables to the NewSprite2.
const Source = runtimeScene.getObjects("NewSprite1")[0];
const Target = runtimeScene.getObjects("NewSprite2")[0];
let TempVariables = {};
const targetVariables = Target.getVariables();
const sourceVariables = Source.getVariables()._variables.items;
if (Source.getVariables().has("Apple")) {
for (const variableName in sourceVariables) {
if (sourceVariables.hasOwnProperty(variableName)) {
gdjs.Variable.copy(sourceVariables[variableName], targetVariables.get(variableName), true);
}
for (const variableName in TempVariables) {
if (TempVariables.hasOwnProperty(variableName)) {
gdjs.Variable.copy(TempVariables[variableName], targetVariables.get(variableName), true);
}
}
}
}
The variable.copy method has an optional merge (boolean) parameter. I used true which merges, if the parameter is missing or false it would clear the target variables first.
Note: It might need more safe guards than I used. You have to plan ahead for weird things to happen. It’s better to catch the error than get some weird fatal error.
I did tell ChatGPT to do that, but the big thing that i was after was showing him how to get variables from objects by giving examples.
Once he knows how to do something he can then check and add everything else thats missing.
For now the script works amazingly, ill keep testing and as people give feedback we can always improve it
You really were a massive help!! I spent 3 hours last night just going trough documentation and working with ChatGPT to get it right but nothing, but because of your example, he knew exactly what to do, so he made it work first try.
I also made sure to tell him everything i knew from experience, like dont make checks for things that dont exist yet, that works fine for events but not for java, if you do that the engine crashes with an error.
I learned that from using FlexBox, when trying to use it on objects that werent there yet it just crashed it whole thing, dosent do that with anything else, only stuff using java directly.
No, it’s all yours. I appreciate it but it’s not needed. Since I don’t know the rest of the process, I also wouldn’t want people to try to contact me about it since I wouldn’t be able to help them.