Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Is there a way to loop around a sprite group?

Author
Message
Polaraul
9
Years of Service
User Offline
Joined: 13th Dec 2014
Location:
Posted: 27th Jan 2015 15:16 Edited at: 27th Jan 2015 15:32
I was hoping to be able to loop around the group id number to find out which sprites have been assigned to the group. Along the lines of:

for x = 0 to group.length
// this is sprite id[x] in group
next

Also, is there a way to assign text object to groups?
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 27th Jan 2015 21:28 Edited at: 27th Jan 2015 21:29
SetSpriteGroup & GetSpriteGroup ? normal used by physics.

AGK 108 (B)19 + AppGameKit V2 Alpha .. : Windows 8.1 Pro 64 Bit : AMD Radeon R7 265 : Mac mini OS X 10.10 (Yosemite)
Polaraul
9
Years of Service
User Offline
Joined: 13th Dec 2014
Location:
Posted: 27th Jan 2015 22:00 Edited at: 27th Jan 2015 22:09
@Markus Many thanks for the reply. I was hoping to avoid having to loop around sprites (which I think is what GetSpriteGroup() would entail). Perhaps though this may be the only way to achieve this.

Where sprites are assigned to a group programmatically, it would have been very useful to find out which sprites where in a certain group.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 27th Jan 2015 22:10
Yes, GetFirstSpriteInGroup and GetNextSpriteInGroup would be very handy commands!


Using AppGameKit V2 Tier 1
unlikely
12
Years of Service
User Offline
Joined: 5th Mar 2012
Location: Ohio, USA
Posted: 27th Jan 2015 22:59
You could create your own sprite number lists for sprite groups as you add them... shouldn't be much overhead?

Using AppGameKit v2 T1 + T2!
Systems: Primary: Mac OS X 10.10
Secondary: Windows 7
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 27th Jan 2015 23:26
long ago i made a request for a reference id in sprite struct used as any index.

AGK 108 (B)19 + AppGameKit V2 Alpha .. : Windows 8.1 Pro 64 Bit : AMD Radeon R7 265 : Mac mini OS X 10.10 (Yosemite)
Polaraul
9
Years of Service
User Offline
Joined: 13th Dec 2014
Location:
Posted: 28th Jan 2015 00:02
Many thanks for all the ideas and replies.

I think the group command is useful, but very under developed. It would also have been useful to see this extended to the other type of objects that can be created, such as text.
MASTER OF PUPPETS
19
Years of Service
User Offline
Joined: 9th Jun 2004
Location: Trapped Under Ice
Posted: 28th Jan 2015 02:51 Edited at: 28th Jan 2015 06:08
I hope I'm not missing the point as I often do. My personal approach makes this sort of thing easy to work around.

First, I create a 'UDT' for sprites.
Second, I create an array with the UDT as the TYPE.
Third, I declare A variable to keep track of how many Sprites I've created.

Like:



I create Functions for pretty much EVERYTHING, so to create/load Sprites, I would call a Function like:



And the function would look something like:



I could easily add "Group As Integer" to the UDT and with a FOR/NEXT, get a quantity of Sprites in a group, extract an ID, or anything else I wish.





I hope that makes sense, and that there aren't any errors. I've not tested this code.

|,,|, (Dead Knot Broken) ,|,,|
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 28th Jan 2015 09:14
@MASTER OF PUPPETS
loops waste time & cpu power,
btw u search for a group but not exist the for next at found.

AGK 108 (B)19 + AppGameKit V2 Alpha .. : Windows 8.1 Pro 64 Bit : AMD Radeon R7 265 : Mac mini OS X 10.10 (Yosemite)
MASTER OF PUPPETS
19
Years of Service
User Offline
Joined: 9th Jun 2004
Location: Trapped Under Ice
Posted: 28th Jan 2015 22:26
Whoops! I guess I should test code in the future, or wait until I'm not in a rush prior to posting anything.

Typically, I would put a for/next loop inside a conditional if/then to control when to loop instead of every frame. I would also put it in a function and use "exitfunction return_the_value" to avoid unnecessary loops.

*sent from a phone - all code should be considered nonfunctional *

|,,|, (Dead Knot Broken) ,|,,|
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 29th Jan 2015 08:45 Edited at: 29th Jan 2015 08:50
Quote: "loops waste time & cpu power"

True but not much. Anyway the OP said:
Quote: "I was hoping to be able to loop around the group id number to find out which sprites have been assigned to the group."


There is a slightly quicker way in V2:


EDIT: The built in group commands are a quick and dirty way to get the job done without building your own data system but as with most things the more complicated the goal is the more complicated the solution. Would be nice to have a few extra commands but to be honest the tools are there and are more flexible than you might think.

EDIT 2: Also this way you can add your sprite to multiple groups such as 'ground' and 'mud' or 'ground' and 'lava'!


Using AppGameKit V2 Tier 1
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 29th Jan 2015 10:03 Edited at: 29th Jan 2015 10:04
A couple of things to consider in the last example...

group.ground.insert(sprID)

I would use this outside of the main game loop, but not inside. Every now and then it will need to recreate the array to assign more memory to it, which may cause a small glitch in gameplay, especially for larger arrays. Paul mentioned in another thread it assigns 1.5x more each time to try and keep memory reallocation to a minimum.

index = group.ground.find(sprID)

This only works on sorted arrays. But to save an extra step, you can use

group.ground.insertsorted(sprID)

Again, all of these things are best used outside of teh main game loop, unless you are only dealing with small amounts of data, or if your game is not timing-dependent (e.g a word game).

Quidquid latine dictum sit, altum sonatur
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 29th Jan 2015 10:48
Quote: "This only works on sorted arrays. But to save an extra step, you can use"

That explains one of my bugs missed that important step...


Using AppGameKit V2 Tier 1
Polaraul
9
Years of Service
User Offline
Joined: 13th Dec 2014
Location:
Posted: 29th Jan 2015 13:25
Many thanks for all the help and suggestions.

My code needs to dynamically create a large amount of sprites, assign tweens to them, then destroy them when done. My plan was to loop through the sprite group to find the state of the tween. There may be a better way to achieve this, but I will implement my group approach and use a combination of the techniques put forward in this post.
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 29th Jan 2015 14:14 Edited at: 29th Jan 2015 14:15
In that case, using SpriteCount as a pointer to the last populated element in a secondary array...

1. Every time you apply a tween, add the sprite id to a secondary array at SpriteCount. (then inc SpriteCount by 1)
2. When a tween ends (loop with for n = 1 to [i]SpriteCount[/i]):
...a. Set this array element to the last populated element in the array (SpriteCount)
...b. Set the last populated element in the array to 0
...c. Reduce SpriteCount by 1.

With this method, you're only ever looping through sprites that have an active tween.

Quidquid latine dictum sit, altum sonatur

Login to post a reply

Server time is: 2024-04-19 06:12:44
Your offset time is: 2024-04-19 06:12:44