Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Allegro strings

This thread is locked; no one can reply to it. rss feed Print
Allegro strings
Lance5057
Member #12,864
May 2011

I'm having a small issue. When I send my UTF-8 string to a function it puts stuff in it no problem but as soon as the function closes and goes back the string is empty. It's being passed by pointer so nothing is being returned.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Lance5057
Member #12,864
May 2011

#SelectExpand
1//Calling 2ShowToolTip(Inventory[0],tip); 3 4//Function 5#include "BL.h" 6 7void ShowToolTip(Item item,ALLEGRO_USTR* tip) 8{ 9 tip=al_ustr_new(""); 10 if(item.type==1) 11 { 12 13 switch(item.prefix) 14 { 15 case NOPREFIX: break; 16 case ABLE: al_ustr_append_cstr(tip,"Able "); 17 18 case VINED: al_ustr_append_cstr(tip,"Vined "); 19 } 20 21 switch(item.multiplier) 22 { 23 case CARDBOARD: al_ustr_append_cstr(tip,"CardBoard "); 24 } 25 26 switch(item.ID) 27 { 28 case 1: al_ustr_append_cstr(tip,"Sword "); 29 } 30 31 if(item.suffix>NOSUFFIX) 32 { 33 al_ustr_append_cstr(tip,"of the "); 34 } 35 36 switch(item.suffix) 37 { 38 case NOSUFFIX: break; 39 40 case RAT: al_ustr_append_cstr(tip,"Rat"); 41 case FOX: al_ustr_append_cstr(tip,"Fox"); 42 case WOLF: al_ustr_append_cstr(tip,"Wolf"); 43 case BULL: al_ustr_append_cstr(tip,"Bull"); 44 case BEAR: al_ustr_append_cstr(tip,"Bear"); 45 46 } 47 } 48}

Fishcake
Member #8,704
June 2007
avatar

I commonly do this mistake. :P In this case, the variable "tip" is only local to this function. Meaning that this line:

tip=al_ustr_new("");

does not change the value of "tip" which you've passed when you called this function:

ShowToolTip(Inventory[0],tip);

You should change the function to something like this:

#SelectExpand
1void ShowToolTip(Item item,ALLEGRO_USTR** result)
2{ 4 if(item.type==1) 5 { 6 7 switch(item.prefix) 8 { 9 case NOPREFIX: break; 10 case ABLE: al_ustr_append_cstr(tip,"Able "); 11 12 case VINED: al_ustr_append_cstr(tip,"Vined "); 13 } 14 15 switch(item.multiplier) 16 { 17 case CARDBOARD: al_ustr_append_cstr(tip,"CardBoard "); 18 } 19 20 switch(item.ID) 21 { 22 case 1: al_ustr_append_cstr(tip,"Sword "); 23 } 24 25 if(item.suffix>NOSUFFIX) 26 { 27 al_ustr_append_cstr(tip,"of the "); 28 } 29 30 switch(item.suffix) 31 { 32 case NOSUFFIX: break; 33 34 case RAT: al_ustr_append_cstr(tip,"Rat"); 35 case FOX: al_ustr_append_cstr(tip,"Fox"); 36 case WOLF: al_ustr_append_cstr(tip,"Wolf"); 37 case BULL: al_ustr_append_cstr(tip,"Bull"); 38 case BEAR: al_ustr_append_cstr(tip,"Bear"); 39 40 } 41 } 42
43 *result = tip;
44}

Calling the function:

ShowToolTip(Inventory[0], &tip);

Lance5057
Member #12,864
May 2011

Ahh hey that does make sense. Thanks Fish

Matthew Leverton
Supreme Loser
January 1999
avatar

This is simpler:

ALLEGRO_USTR *ShowToolTip(Item item)
{
  ALLEGRO_USTR *tip=al_ustr_new("");
  // ...
  return tip;
}

ALLEGRO_USTR *tip = ShowToolTip(item); // maybe it should be called GetToolTip()

Also, you are passing the item by value. You may want to pass its pointer (or by reference if using C++).

Go to: