I’m making a virtual musical instrument (ocarina). I have 13 instances of an object, and when the cursor is on a particular one of these and mouse button is down, I want to get a sound played (which is working with instance variables) but I would also like to change the colour of a selected set of the instances, and the set would be different depending on which instance is clicked on. Changing colour of the instance clicked on works through change Tint, but how to choose and access the other instances? I suppose some structure or array could work, but I have no idea how to use them and haven’t found any resources that would make sense to me. How can I set up an array so that the instances I want would be recognised? Help much appreciated!
I’d recommend using two variables: on each instance you put (via the instance properties panel) an ID
variable with a unique name for each.
Then, add a scene structure TintMap
and for each instance, add an item that has as key the ID of the instance and as item an array variable. Add in the arrays the IDs of all the buttons to tint.
Note: It would be more intuitive to use an object variable for the array, but the for each variable event sadly only works with scene variables…
Then, in the events, add a “for each child variable” event, put as variable to iterate on TintMap[Object.VariableString(ID)]
, and tell it to store the value in TintID
. Put a subcondition compare text of an object variable
, with as variable the objects ID variable and as value VariableString(TintID)
. Finally put your tint action.
This works thanks to object selection: you will repeat some events for all items of the array and store the item in the variable TintID. Then, by checking if an object has this value as ID variable, you select the object that does so, meaning the next actions will only apply on it.
Thanks for your reply, but I’m afraid I don’t quite get it. I have put instance variables like A,B,C… What is a scene structure and how do I add an item that has as key the ID of the instance and as item an array variable? What is the specific action I should use? Is there then somewhere I can add like [“A,B,C”] for the instances I want to tint? Could I use numbers instead, as I might then be able to add/subtract one at a time? Or can I? Or can just one at a time be removed, as like with instance number 1 I want all to be tinted, with instance number 2 all except number 1, and so on. Is there a way I don’t need to specify them all every time? And then reset when some other instance is clicked? Sorry, I have no experience with arrays.
So with this mechanics should it then be: “For every child in TintMap[Object.VariableString(ID)], store the child in variable TintID, the child name in (optional) and do:” Should I store the child name somewhere? And then under this, I compare the text of an object variable and then Change tint, right?
Would it be easier to just make different objects instead? I might also intend to use these instances/objects so the player can give answers to questions; does this have an impact on whether to choose objects or instances? Not that I know how to handle objects in this way either, other than putting them into object groups, but I would like some more elegant way. Or is it possible to tint just a part of a bigger sprite in the background and having the instances transparent? Or part of a tiled sprite or something?
You can take advantage of the “group” of instances. Create “group A” with all the instances that must change color when the event occurs. Then the “group B” … “group C” etc. Each instance can also be present in multiple groups.
When the event occurs, the “group” concerned causes all the instances in it to change color. Remember, then, to return the “group” to the initial color at the end of the event.
I thought that was just for objects, not for instances of the same object?
You’ll want to learn about those, they are fairly important. If you do not understand them you will not be able to achieve your goal and many other things. You can learn about them by searching for “GDevelop structure variables” on Google and the wiki.
That’s pretty much what i explained above
I don’t see a reason why not, if you doubt it costs you nothing to just try it out and see.
I mean if you want to tint specific instances you have to tell at some point what are the ones you want to tint
You could but the effort to create the events for that would probably be bigger than just putting in all the IDs for each instance.
That has nothing to do with arrays, if you want to reset just tint the objects white (= removing the tint) in a condition less event before doing the tinting.
You tell me, as you could read I have not said anything about that, as it is irrelevant to solving the problem, but if you need it to solve something else you have not talked about then do whatever you want
that’s what I said indeed.
Don’t ask me, ask yourself. To me, as someone who knows variables, not using them would just bloat my objects and events list and make it harder for myself. If you do not know and are not willing to learn about those, then using separate objects might be easier for you.
Groups have nothing to do with instances. Using them would not solve the problem.
I’m willing to learn, but the problem is I have searched through Google, wiki and this forum, but I can’t find a single resource that would clearly explain what to do. All I can find is some answers to questions asked by someone who seems to already know how to use structures in some way. I read somewhere that structures are used just as ordinary variables. Does that mean I could just put [“A,B,C”] in as a variable string? Can you refer to some beginner friendly resource, please?