[SOLVED]Swap variable values?

How do I…

I wanna set variable A to = var B, and var B to have the value A

What is the expected result

basically im working on item swapping thingy so when i set the 1st variable, and the second they should “swap” their values? maybe. Im using local variables to store their values but it still doesnt work

What is the actual result

Sometimes it just gets set to 0 or doesnt work at all, really buggy

Related screenshots


swap[0] stores the first picked slot’s values, and swap[1] holds thhe values of the second slot.
__SlotBasedInventory[0].hotbar[slot number here].name will return the name of the item in the slot, while
__SlotBasedInventory[0].hotbar[slot number here].quantity will return the quantity (which you can also get by: SlotBasedInventory::SlotQuantity("hotbar",slot number here))

Any idea why this doesnt work?

Project files (optional)

Insert a minimal game showing your issue in a .zip or .rar.

image
this is the “hotbar” it’s basically array between 0 to 8 where each child is struct var containing .name and .quantity

That’s the problem. Local variables only exist for the scope of the event they’re declared in.

What I mean by that is the local variable only exists in the red section below. Once the event processing falls outside of that, the variable is destroyed and doesn’t exist.

Every time GDevelop reaches the event that the local variables are declared in, the local variable is created and set to whatever their default value is. In your case, it an empty array.


Your solution is to use a scene variable instead.

1 Like

well, i had the it similar to the screenshot u provided but i guess using scene variables is better most of the time.

Anyways i managed to get it working but thanks for replying :slight_smile:

Hello
To permute 2 variables, we must use a … third variable which will contain by example the A variable value.

A+
Xierra54

1 Like

It can be done with just 2 variables and an XOR used 3 times. Very useful in the days of 8 bit machines and 4K demos when memory was a premium, but not so much now-a-days:

say A = 51 and B = 32.

A = A xor B
A => 19

then

B = A xor B
B => 51

and finally

A = A xor B
A => 32
1 Like

Ah! You unmasked me …

A+

there is already an action for this in the extension:)

1 Like

Yes, of course but is-it well necessary tu use an extension for that?

A+
Xierra54

1 Like

oh im sorry i dont think you understand.

Kravataf is using my inventory extension, i have an action that swaps items already implemented

2 Likes

Ah, Ok.
I am going looking for.

A+
Xierra54

1 Like