Jump to content


Photo

Getting the sprite from a string


  • Please log in to reply
6 replies to this topic

#1 Ventwig

Ventwig

    GMC Member

  • New Member
  • 279 posts

Posted 18 February 2012 - 12:41 AM

So, I'm working on an RPG where the player gets 5 attacks per character, and then they command those attacks to make more.
Okay, it's confusing.


The problem is the sprites.
There will be many characters, and many attacks, so the thing is, I'm handling the attacks in one object.
The problem is this:

-I set up the attacks by naming them like this:
card1 = "fire"
card2 = "stab"
card3 = "blizzard"
etc.

then when I draw the sprite, i try:
draw_sprite("sprc_"+string(card1),0,50,300)
but then it just shows the first sprite i made.
the sprite i want is called sprc_fire

Help please?
  • 0

#2 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 17022 posts
  • Version:GM:Studio

Posted 18 February 2012 - 12:48 AM

You can do this using execute_string... BUT it is not recommended as apart from being terribly slow, it's also a function that is being phased out and removed from versions of GM. So to solve your problem, you need to store the sprites in an array and then have a variable that is set to the value to draw from the array. For example :

//CREATE EVENT
card[0]=spr_fire;
card[1]=spr_stab;
card[2]=spr_blizzard;

current_card=-1; //-1 means no card.


//SOME OTHER EVENT
//set the attack card...
current_card=2;


//DRAW EVENT
draw_sprite(card[current_card], 0, x, y);


That's how it should be done!
  • 0

#3 IceMetalPunk

IceMetalPunk

    InfiniteIMPerfection

  • Retired Staff
  • 9314 posts
  • Version:Unknown

Posted 18 February 2012 - 01:10 AM

Or, to maintain the string name->numerical index association: http://www.gmlscript...ipt/map_sprites

-IMP
  • 0

#4 Ventwig

Ventwig

    GMC Member

  • New Member
  • 279 posts

Posted 18 February 2012 - 02:27 AM

Or, to maintain the string name->numerical index association: http://www.gmlscript...ipt/map_sprites

-IMP


Thanks =)


I've implented it, but how exactly do i use it?
I've checked it out, but i dont understand. :blink:

Edited by Ventwig, 18 February 2012 - 02:32 AM.

  • 0

#5 IceMetalPunk

IceMetalPunk

    InfiniteIMPerfection

  • Retired Staff
  • 9314 posts
  • Version:Unknown

Posted 18 February 2012 - 07:06 AM

When you call that script (passing it a ds_map), it fills the map with name=>index pairs. So then you can do something like this (assuming the map is stored in global.spriteMap):

draw_sprite( ds_map_find_value(global.spriteMap, "sprc_" + string(card1)), 0, 50, 300 );

-IMP

Edited by IceMetalPunk, 18 February 2012 - 07:07 AM.

  • 1

#6 Ventwig

Ventwig

    GMC Member

  • New Member
  • 279 posts

Posted 18 February 2012 - 03:09 PM

Thanks IMP!
That's perfect!
  • 0

#7 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 17022 posts
  • Version:GM:Studio

Posted 18 February 2012 - 03:34 PM

Nice one IMP... I forgot about that script, and it really is quite useful!
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users