Using Java Script, how do i get Object Variables?

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…

Anyone got any ideas?

1 Like

There’s an extended variables extension. It’s mostly written in JavaScript. You can use the extension or learn from it.

https://wiki.gdevelop.io/gdevelop5/extensions/extended-variables/#extended-variables-support

This would check if “NewSprite” has a variable named “Apple”. I used angle as a test.

const obj = runtimeScene.getObjects("NewSprite");
if (obj[0].getVariables().has("Apple")){
    obj[0].setAngle(obj[0].getAngle()+10)
}
2 Likes

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.
image

Should the target be merged with the source, or be an exact copy?
https://docs.gdevelop.io/GDJS%20Runtime%20Documentation/classes/gdjs.Variable.html#copy

2 Likes

KEITH!!! YOU BEAUTIFUL PERSON YOU!!! <3

Thanks to your help, and a whole lot of explaining how safeguards worked and what to do to make sure stuff works… IT WORKED FIRST TRY!!

I have the new script that now stores object variables before culling and restores them after recreating the object!!

Iv tested it and everything works! The culling is working, the restoring variables work, the exclusion… im so happy right now…

and first try… all thanks to your help bud!!

You get full credits on this.

Ill be adding the new script on the main Post with your name added on top.

Ill be addint the new stuff on there soon, for now, time for a coffee and breakfast…

1 Like

Good to hear. I’m glad it works.

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.

1 Like

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 :slight_smile:

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.

1 Like

@Keith_1357 The new script is up on the thread!

It does so much more now! Its amazing!

Let me know if you want me to add your GDevelop ID on to the credits instead of just your forum name

1 Like

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.

1 Like

Fair enough :slight_smile: Ill take your name out then

1 Like