Can A Sprite be an Array?
-
Weird question but I try to explain myself.
I want to code a garden simulation with space for up to 50 entities.
I made a Sprite animation of a dandelion in different states of growth.
So far so good I got into some trouble with handling Sprite handles.
I made an Array called Entities[50] wich contains an entity struct with Intel like status, species, and coords.
And I made an Array called Sprites[50] that's associated with the entity slots.
For I = 1 to 50 loop
Sprites[I] = Createsprite()Repeat
When I try to setspriteimage(Sprites[Freeslot] ) In an algorithm that looks for a free slot and saves the index number of the first free slot it finds in a global variable called Freeslot.
The Interpreter says that Sprites[Freeslot] is not a Sprite. And here I am confused. Because there should be a whole Array of Sprites.
-
You certainly can store a sprite handle in an array so that should work. Are you sure that FreeSlot is set to a value in the range 1 to 50?
-
The search algorithm works as follows:
For I = 1 to 50 loop
If Entities[I]. Status = 0 then saveslot() break endif
Repeat
Like this the index number of the free slot should be stored in the Freeslot variable.
Saveslot() does just that. But maybe I should do a Freeslot == I instead.
That aside I don't get why I can't refer to that Array as a Sprite handle
-
Did you store something in Entities[0]? Because the array index starts at 0.
And the for loop parameters will be 0 to 50. And it will return you values with the indexes 0 to 49. -
@spikey that could actually be it. Thanks for the reminder I will try that out later and tell you the results.
-
It seems that this was indeed the problem.
Array indexes start at 0. If I skip the 0 slot I can't Acess it as a Sprite.I fixed the numbers and it worked as intended.