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 / Best method to link the type ID with the sprite ID

Author
Message
Richard_6
7
Years of Service
User Offline
Joined: 3rd Feb 2017
Location:
Posted: 17th Apr 2017 02:58 Edited at: 17th Apr 2017 03:03
I’m trying different methods to link the type ID with the Sprite ID. The reason: once I click on the sprite I need to know which type item is that so I can insert in other type or remove.


Method 1:

In this method, as InventoryPartCardSprite is a variable the Sprite ID is auto, so it will start at 1001, 10002, etc. so once i use GetSpriteHit(x,y) I’m not able to know which type I clicked.

Method 2:

In this method, I created the integer SpriteID inside the type, but still, It starts with 1000, 1001, 1002 and my type IDs follows the array sequence (0,1,2,etc.) . I would be able to get the type ID trough a loop, but not sure if its a fast method.

Method 3:

Here I can link the sprite ID with the type ID, however it won’t accept 0 so I would have to use i+1 and I’m afraid it will get confuse with time.


I’m sure you can come up with something better
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 17th Apr 2017 03:42 Edited at: 17th Apr 2017 03:43
Maybe use the 1000, 1001 method and just subtract 1000 from the ID and use that as an index

you could maybe code the type in the filename like type_card.png and then use
getspriteimageid() and getimagefilename() to retrieve the filename
Richard_6
7
Years of Service
User Offline
Joined: 3rd Feb 2017
Location:
Posted: 17th Apr 2017 03:56 Edited at: 17th Apr 2017 03:58
Hi blink0k, thanks for the insights.

Subttraction works well in the first for-next. Let's say the first loop ended on 1005. Next time I will run the loop again it will start on 1006 and it won't match.

getimagefilename() is a good alternative, but we will have to go through a loop to find the ID right? It's similar to method number 2 where I store the spriteID in the type, unless the image file would be the same of the ID, but then it will be hard to reuse.
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 17th Apr 2017 04:11
Doesn't getspritehit(x,y) return the sprite ID? So that gives you the id and from that you can get the filename and then the type
Richard_6
7
Years of Service
User Offline
Joined: 3rd Feb 2017
Location:
Posted: 17th Apr 2017 04:20 Edited at: 17th Apr 2017 04:32
yes, but I will have to go through a loop inside the type to find it right?

Something like this, I guess
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 17th Apr 2017 04:54 Edited at: 17th Apr 2017 04:54
Not if you make the type in the filename a number then you can use that as the index like
11_ace.png
10_king.png
etc
so just parse out the number before the "_" and you have an index
PSY
Developer
8
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 17th Apr 2017 05:06 Edited at: 17th Apr 2017 05:14
Why don't you just use method 3 and start at array position 1 instead of 0 like:




BTW, running through a type loop even with hundreds of cards won't be a speed problem...
PSY LABS Games
Coders don't die, they just gosub without return
Richard_6
7
Years of Service
User Offline
Joined: 3rd Feb 2017
Location:
Posted: 17th Apr 2017 05:50 Edited at: 17th Apr 2017 05:50
Thank you for this new idea, blink0k. I haven't thought about it.

Psy, the reason I'm not starting with 1 is because I don't want to jump the 0 . I'm afraid with time I might get confused but it is a possibility. Thank you.

In this meantime I found a built-in function called "find". I couldn't make it work with types, though. Anyone has ever tried?

blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 17th Apr 2017 06:36 Edited at: 17th Apr 2017 06:36
Quote: ".sort() will always sort in ascending order and .find(item) will only work on arrays that are in ascending order. If .find(item) cannot find the item you are looking for it will return "-1". You can sort arrays of Integers, Float, Strings, and Types. When sorting types the first variable of the type will be used to compare elements."


You need to make sure the array is in ascending order. Other than that it should be ok
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 17th Apr 2017 08:18
maybe
Get/SetSpriteGroup
AGK (Steam) V2017.03.31 : Windows 10 Pro 64 Bit : AMD (17.2.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 17th Apr 2017 09:39
Quote: "BTW, running through a type loop even with hundreds of cards won't be a speed problem..."


This. The code below illustrates. On my machine, I can write details of 100,000 cards in 0.05 seconds. I can search all 100,000 of them (worst case scenario) in 0.08 seconds.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 17th Apr 2017 13:36 Edited at: 17th Apr 2017 13:38
I'm not entirely sure what you're even trying to do. What are you ultimately trying to do? Just get the index in an array which corresponds to a given sprite when it's clicked?

Quote: "the reason I'm not starting with 1 is because I don't want to jump the 0 . I'm afraid with time I might get confused but it is a possibility. "


But by doing 0 to length you're essentially making your loop length+1. That to me would be confusing. Best to stick to either 1 to length or 0 to length-1.


Matching sprite ID's to the index of array creates a sort of parallel array, which is bad practice in my opinion. If anything changes in the order of the sprite numbers or array they will no longer match up. It's too easy to lose data integrity. Store the sprite ID in a Typed array and loop through it to find the proper one you want.


Here's a quick example of building a deck of cards, how to shuffle the deck, and how to select a card on screen.


"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
PSY
Developer
8
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 17th Apr 2017 15:30 Edited at: 17th Apr 2017 15:30
Quote: "Quote: "the reason I'm not starting with 1 is because I don't want to jump the 0 . I'm afraid with time I might get confused but it is a possibility. "


But by doing 0 to length you're essentially making your loop length+1. That to me would be confusing. Best to stick to either 1 to length or 0 to length-1."


Exactly.
1 to length is far less confusing than 0 to length-1

First card should be number 1, not 0
PSY LABS Games
Coders don't die, they just gosub without return
Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 17th Apr 2017 15:47

when you create a sprite (or whatever media resource) you can have a array of Sprite Indexes to Owner Indexes..

quick mock,


PlayBASIC To HTML5/WEB - Convert PlayBASIC To Machine Code
tmu
7
Years of Service
User Offline
Joined: 2nd Feb 2017
Location:
Posted: 17th Apr 2017 20:03
Here is a solution for you. Use a map datatype and map your sprite id's to its metadata. If you destroy a sprite, remove its id from the map. Oh, wait, there are no maps in AGK.

Are you actually re-creating your sprites for some reason? If not, just create them once and index the metadata array with some subtraction of the sprite id as discussed by others in this thread. Like 1001 becomes index 0, 1002 index 1 and so on.

If you really need to delete and recreate your sprites, maybe your could just reuse the numbers? Not sure what AppGameKit does if you try to reuse a sprite id. Or if you have only a hundred or so sprites just search the list would be fine. I guess find() does some kind of binary search, so if you just insert stuffs in your array with auto-generated id's it should be always sorted anyways. Because the autogenerated id numbers always increase. Of course if you do really intensive sprite re-creation your array will grow huge and eat memory.

nz0
AGK Developer
17
Years of Service
User Offline
Joined: 13th Jun 2007
Location: Cheshire,UK
Posted: 18th Apr 2017 00:51
..or just create UDTs and store your own links.
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 18th Apr 2017 01:05 Edited at: 18th Apr 2017 01:06
I run into this issue a lot. Yes it's easy to say that a lookup will be quick but if you add all the other processing on top it can become an issue, especially if you're doing the table search multiple times
It's an issue that could easily be fixed by allowing the programmer to add arbitrary data to a sprite like so;
Quote: "SpriteId = CreateSprite(ImageID, ArbitaryData)"

Then you could have a function to return that arbitary data
Quote: "ArbitaryData = GetSpriteData(SpriteId)"

I constantly run into this issue and being able to store data in a sprite would completely solve the problem (I think DBP had this functionality too)
Richard_6
7
Years of Service
User Offline
Joined: 3rd Feb 2017
Location:
Posted: 18th Apr 2017 04:30 Edited at: 18th Apr 2017 05:11
Wow, thanks a lot everyone. So many examples. It will be hard to merge all concepts now

@Markus: yes, I'm using get/set spritegroup specially when I have a bunch of sprites.

@Phaelax: yes, ultimately I think storing the sprite ID in a type its the best option. Now you're saying I feel more confident . However I will study a bit more your code in order to incorporate in the code I'm currently working on.
Right now, I'm using this:


@Kevin Picone: interesting concept, thanks.

@tmu: can you show an example of a map datatype? Not sure if I'm aware of it (at least with this name).

About the for-next with 0 or 1, I think I'm doing something wrong or its the method that I'm using.

Just a simple example:



I get 1, so I understand its 0 and 1. That's why I'm working with for i=0 to length. Am I missing something, I guess?

Login to post a reply

Server time is: 2024-11-24 16:28:16
Your offset time is: 2024-11-24 16:28:16