Hello.
I’m not good at variables, so I’m having a problem to implement this expression.
I know it’s not correct, but I don’t know how to create it, access it and store a value inside it.
It seems there´s no option in events to access the code by just clicks and type names and we have to write the full expression including brackets, and that’s the point I’m lost.
Appreciate a help! Thanks
Can you give a little more detail on what you are trying to do?
I’m trying to check if the child exists. If not, I want create it and assing a value to it.
So you’re appending values to selection which makes selection an array and not a structure. I put it like this just to be close to the condition you had:
or
It won’t have a child called firstx because the children in arrays are called numbers. In the picture you can see the first index of selection, 0, is turned into a structure because we placed the child named firstx (an array) into it.
We could also place another child in selection.0 called secondx and make it an array also.
To make sure selection is an array this is one of the rare times you will have to premake the variable selection in the scene variable thingy and set it to an array. You won’t have to do anything else to it. Otherwise calling it selection.0 in events will make it structure with a child named 0 which is probably frowned upon and may break the game.
But I think what Phoenix meant is what are you trying to solve in your game by doing all this because there is probably an easier way.
Hello! At first, thanks for your reply.
I’m trying to make a swap tiles game and I need to store the first position tile clicked and then the second one. But in case the player regrets the first click he can click again on the same tile and cancel the first click. So I guess I need to create a variable only when the click is triggered, and delete the variable if player regrets, because I cannot change the value to any other. If so, the tiles will swap to that value. I still cannot see the diference between structure and array. How do I add a child to a structure? All options says I’m adding to an Array Variable.
I guess I achieved want I was trying.
But I needed to use a Global variable. Is that correct?
Didn’t find a way to use a scene variable.
Hey it looks great! Ok to find scene variables: open your conditions or actions and in the search bar type “lessc”. There used to be another way to find them but they renamed them all. Anyway then you can make them a scene variable if you want.
I’m stuck again.
When the player regrets and click again on the tile I set an instruction to remove the children (I have 4: (firstx, firsty,secondx and secondy), but it’s not removing the child firstx and it’s changing its value to 0.
After tween, the action should remove the 4 children.
But the child firstx persists.
IDK if the lines eqauls 1 and equals 2 are subevents but they probably need trigger once
Remember, with Gdevelop, just trying to read a variable will create the variable with the variable type’s default value, zero for numbers. The same seems to apply to the exists condition although for me it only created the variable without children.
I think it is removing firstx but what is happening is, somewhere in your events there is an action to read selection looking for firstx and since firstx doesn’t exist it will be created like Keith mentions above. You would have to add “if the child firstx exists” to whatever reference to firstx it is. For my projects I have never ever seen “if the child exists” condition create the child it’s looking for. That’s why I use it. But it can be easy to accidentally create the child somewhere else for instance by saying “modify the text of TextBox set to firstx” without specifying to check if firstx exists. So if you can’t figure out where in your events firstx is being created, you can go back to your other events that say if the child firstx exists do these things and change it to compare 2 numbers, variable selection.firstx does not equal 0.
Off topic there is an action to Clear Children from a structure at once
The only references I have to check if children exist are these, but I don’t think it is the problem because if so, the child firsty would be created as well.
Oooops!!! I think you’ve found the problem. I actually had an event changing the text to firstx to check if it was being created. I disabled the line and it seems to be working now! Thank you so much!!!
It seems, like you and Keith said, checking the existence don’t create the child, but trying to pass a not existent child to a text does it.
What’s the action to clear all at once, please?
Now that I checked and I thought I had fix them all, it doesn’t work anymore. I changed global to scene variables, but first child (firstx) is creating as number and the others as string. Why?
And it’s creating both, global and scene variables.
I think you need to continue to investigate why this isn’t working so you understand why and will know what to do the next time. That being said, I played with the concept and merged your idea with how I normally do swaps. I got a little carried away and almost created an entire game. All it needs is a shuffle. I like the use of the array. With a little work, it could be made to undo every single move.
Try me: click 2 tile to swap. Click the same tile or the empty scene to cancel a selection. Click undo to, well undo.
Source:
Mny thanks, I’ll have a look on that. But could you tell me why if I use the exact same code to create scene variable child and global variable child, whilst scene variable creates an array, global variable creates a structure instead?
Arrays and structures are similar. Gdevelop still has a problem with creating an array instead of a structure. Sometimes, you need to set a variable as an array in the variable setting screen to guarantee it’s an array. It shouldn’t matter if it’s global or scene. I need to go, so I can’t look at your events right now. I’ll check later unless you or someone else figure it out first.
If you have more events that use the array/structure, it would be helpful to post the current version.
ok! Thanks again! Have a nice time!
I think the problem is this part. It’s mixing arrays and structures. I think it’s corrupting things or changing the format.
Arrays are indexed like
A[1] or a[1].x
Structure are a.b or a[“b”] or a[“b”].c
I’m not sure what happens when you mix things. It usually resets the variable to the new format.
If you want it all to be an array then each line would be like the first. Add … to array although, I would use all of the same type unless you use an array of structures. Yes, it can be confusing.
You can have an array of structures like
Set a[0].x =4
Set a[0].y = 5
Set a[0].name = “fred”
Or you can build a structure variable and add that variable to the array
Temp.x = sprite.X()
Temp.y = sprite.Y()
Temp.name = “fred”
Add variable temp to array points
Many thanks Keith. I’m doing a lot of tests and analyzing Debugger trying to improve my understanding about variables.