[Solved] Different objects using the same code

Hi, I wanted to make a virtual pet game, but wanted to know how can I make many pets (cat, dog, wolf, etc…) Use the same code without copying the original code, pasting it, changing it to make it work for other pets, it will be painful and can make the game heavy/ laggy if I had over 10 pets or something.
(Because the same code will be pasted over 10 times too.)

I know that object groups exist. However, my code uses insta variables (for more personal movement if the same object existed on the screen more than once), object groups don’t seem to have insta variables, so that makes it even harder to know how to make it.

Just use a single object with different animations for every animal :wink:

That can work fine!
But if I tried adding the breeding feature, would that cause a problem or not? Because if all animals are the same object, then how can I define them as “cat”, “dog”, etc…
I just don’t want weird things to happen lol

you can use the instance variables even if the object is in a group. so groups haven’t instance variables, but the instances inside the group keep their own variables. so you can still use them.

(Sorry if the way i wrote it is a little confusing)
I know, but I meant using the object group to make all the objects function the same way.
An example is : In the range (3,5) seconds, an animal either walks or just stand still, but as I stated before, object groups don’t have their own insta variables, making it a little confusing to me, here is the code for the object (no group).
(As I said before, I made it like that so if there is more than one of the same object, they don’t all walk or go to the same direction, aka have a more personalized movement)

Update : Was able to fix it but in a really weird way, as stated, Object groups can’t have instance variables (their own variables), weirdly enough I just wanted to test out what would happen if I coded it as if it has one, it surprisingly worked exactly as I wanted!
So apparently, Object groups can have instance variables (non-global variables), all you have to do is just coding it in the event sheet like it has one and boom! works like how it should.


uhhh

As you can see, everyone of them walk in a different direction or stand still.
I still find it weird that it worked that way

1 Like

everyone except you call that instance variables :rofl: . this is what we have been talking about so far. anyway, the point is that the problem is solved.

Well here is something I learned, I was talking to someone that uses unity, they had no idea what were instance variables, so I did put it out like that because not everyone (mostly new people here) are familiar with the term tho.

and the Wiki is for newcomers to get to know these terms…

Object groups can’t have variables, but the objects inside groups can. Using conditions, you tell GDevelop which instances (belonging to the group) you want to target.

Maybe you should use an object timer instead of a scene timer, so they don’t all act at the same time :slight_smile:

1 Like