GLBasic forum

Main forum => GLBasic - en => Topic started by: mentalthink on 2012-Mar-14

Title: Getting name of Sprite
Post by: mentalthink on 2012-Mar-14
HI anyway to take the name Real, or the variable name of a Sprite whit sprcoll.... somthing like this:

If sprcoll(spr_Changing, x,y , _
               my_Bullet,x,y)

return spr_Changing // in this case I have somthing like  a number 65000 ... but I nees or the loaded name, like when we use loadpsprite or the name of the ID variable whit GENSPRITE... it´s possible, or I only can get the automatically associated number ID...

Thanks in advance...
Title: Re: Getting name of Sprite
Post by: Darmakwolf on 2012-Mar-14
I could be wrong, but you may be able to do something like this:

Code (glbasic) Select
global spr_bullet = 400

loadsprite "bullet.png",400

while true
drawsprite spr_bullet,320,240
showscreen
wend
Title: Re: Getting name of Sprite
Post by: MrTAToad on 2012-Mar-14
You would have to do name mapping yourself - I've got a TMap routine (which would need a slight update to fix a bug) somewhere that would let you map a name to a sprite ID and then use the name to get the ID value.

It would be quick as it uses binary sorting.
Title: Re: Getting name of Sprite
Post by: mentalthink on 2012-Mar-14
HI Darmawolf... thanks but not it´s this...

I put the example, my english it´s too poor...

gensprite spr_Test_M
Loadsprite "spr_Test_M" + ".png" , spr_Test_M

///in some part of code
if sprcoll(spr_Bullet,..,.. _
              spr_Test_M)

        return the ID (not the number), of spr_Test_M
endif

In this case it´s easy and don´t have too much sense... but the spr_Test_M, change... not it´s always the same

Thanks again for your reply, but not it´s I need.
Title: Re: Getting name of Sprite
Post by: mentalthink on 2012-Mar-14
HI MRTatoad thanks for your reply... I take a look you tell me...
Title: Re: Getting name of Sprite
Post by: Slydog on 2012-Mar-14
You could use this TYPE I just made up:
Code (glbasic) Select
TYPE TSprite
id% // Sprite id, used by GLBasic sprite commands
name$ // Name to refer with this sprite

FUNCTION Load: path$, name$
self.name$ = name$
IF DOESFILEEXIST(path$)
self.id = GENSPRITE()
LOADSPRITE path$, self.id
ELSE
self.id = -1
DEBUG "** File NOT FOUND!: [" + path$ + "]\n"
ENDIF
ENDFUNCTION
ENDTYPE


Example code:
Code (glbasic) Select
GLOBAL sp_changing AS TSprite
GLOBAL sp_bullet AS TSprite

sp_changing.Load("spr_Test_M.png", "spr_Test_M")
sp_bullet.Load("bullet.png", "bullet")

IF SPRCOLL(sp_changing.id,x,y, sp_bullet.id,x,y)
DEBUG "Sprite named: [" + sp_changing.name$ + "] collided with sprite named: [" + sp_bullet.name$ + "]\n"
ENDIF

Title: Re: Getting name of Sprite
Post by: mentalthink on 2012-Mar-14
HI Slydog, thanks in advance... I try tomorrow... Thanks...
Title: Re: Getting name of Sprite
Post by: MrTAToad on 2012-Mar-14
This is my Map routine (http://www.glbasic.com/forum/index.php?topic=5170.msg40068#msg40068)

The search function needs to be replaced with this though (as there was a case where an item wasn't being found) :

Code (glbasic) Select
FUNCTION search%:key$
LOCAL up%,down%,mid%

up%=0
down%=BOUNDS(self.list[],0)
WHILE up%<down%
mid%=up%+(down%-up%)/2
IF self.list[mid%].key$>key$
down%=mid% // MAX(mid%-1,up%)
ELSEIF self.list[mid%].key$<key$
up%=mid%+1 // MIN(down%,mid%+1)
ELSE
self.list[mid%].used%=TRUE
RETURN mid% // Found
ENDIF
WEND

RETURN self.INVALID%
ENDFUNCTION
Title: Re: Getting name of Sprite
Post by: mentalthink on 2012-Mar-14
Ok, Friends, tomorrow a look carefully, and comment...

Thanks in advance...

The fact I need this it´s for make whit collisions the proper sound in the correct sprite... and I think it´s more quikly than have a lot of name in array, and make a For running into the array... perhaps don´t works this idea... but I try it...

Thanks again for all replies...

Iván J.
Title: Re: Getting name of Sprite
Post by: Slydog on 2012-Mar-15
Ah, now I know why you wanted the name!
If triggering sound effects is the reason, why not bypass the name altogether and associate a sound id with the sprite instead?:
Code (glbasic) Select
TYPE TSprite
id% // Sprite id, used by GLBasic sprite commands
id_sound% // Sound id for this sprite

FUNCTION Load: path$, id_sound%=-1 // Optional sound id, or you can assign later, see example
self.id_sound = id_sound
IF DOESFILEEXIST(path$)
self.id = GENSPRITE()
LOADSPRITE path$, self.id
ELSE
self.id = -1
DEBUG "** File NOT FOUND!: [" + path$ + "]\n"
ENDIF
ENDFUNCTION
ENDTYPE


Then the sample code would change to:
Code (glbasic) Select
GLOBAL sp_changing AS TSprite
GLOBAL sp_bullet AS TSprite

sp_changing.Load("spr_Test_M.png")
sp_bullet.Load("bullet.png")

sp_bullet.id_sound = GENSOUND()
LOADSOUND "sound.wav", sp_bullet.id_sound, 1

IF SPRCOLL(sp_changing.id,x,y, sp_bullet.id,x,y)
PLAYSOUND(sp_bullet.id_sound, 0, 1)
DEBUG "Sprite id: [" + sp_changing.id + "] collided with sprite: [" + sp_bullet.id + "]\n"
ENDIF


You could assign a sound for the main sprite too, such as for walking etc.
Or, the sprite TYPE could allow for more than one sound id, such as one for collisions, one that is always playing, etc.

So then the sample could be updated to (if you add 'id_snd_collision' to the TYPE):
Code (glbasic) Select
PLAYSOUND(sp_bullet.id_snd_collision, 0, 1)
Title: Re: Getting name of Sprite
Post by: hardyx on 2012-Mar-15
When you load the sprites, declare a string array like spr_names$[] and put the name of the sprite with dimpush. You must to use numbers like 0,1,2,3,... or use a variable with the initial sprite you want to use. The sprite IDS you load will be SPR_INIC+0, SPR_INIC+1,.... Then when you get the ID,  take the name with: spr_names$[SPR_INIC - returnID].
Title: Re: Getting name of Sprite
Post by: mentalthink on 2012-Mar-15
Hi Slydog a lot of thanks... well I have to write something on my code  :P, thanks in advance...

@hardyx, yes you have reason, about put into an array, but I don´t want use array, so if I have to run in it, in every collision, the game can drop the frames down a lo of.. it´s shot´em up, and I have a great bunch of shots at the same time...

Agains thanks to all, for your replies...