(SOLVED) Searhcing struct arrays by value

Started by WHAM, Tue 01/03/2011 19:56:46

Previous topic - Next topic

WHAM

Here is what I have made so far.

Code: ags

// GlobalScript.ash

  struct Firearm {
    String name;
    String description;
    int graphic;
    // Lots of other variables be labeled here
  }

// There are also structs that generate similiar melee weapons and special weapons in separate struct arrays



Code: ags

// GlobalScript.asc

// Create firearms
  Firearm firearms[10];

// called when the game starts, before the first room is loaded
function game_start() 
{
  // .32 REVOLVER
  firearms[0].name = ".32 Revolver";
  firearms[0].description = "A small .32 revolver, the standard firearm of the 1920's";
  firearms[0].graphic = 56; // The sprite that contains the graphic for this item
}




What I need to do: A character has a string variable "righthand", that contains the name of the weapon the character has in his/her right hand. If the variable "righthand" contains the value ".32 Revolver", the game should search through all the weapons in the game, and when it finds the matching name, it needs to be able to save the firearms[ # ] number to a variable and use that to draw the other variables stored in that struct into GUI labels and button graphics.

Any suggestions on how to best do this?
Wrongthinker and anticitizen one. Utterly untrustworthy. Pending removal to memory hole.

Calin Leafshade

it would really be a better idea to get 'righthand' to save the integer index value of the weapon rather than it's name.

Khris

#2
Why use a String variable? Why not store the int directly?

You could even create an enum:

Code: ags
// header

enum FirearmType {
  32Revolver = 0,
  Uzi = 1,
  ...
}

// game start:
function game_start() 
{
  // .32 REVOLVER
  firearms[32Revolver].name = ".32 Revolver";
  firearms[32Revolver].description = "A small .32 revolver, the standard firearm of the 1920's";
  firearms[32Revolver].graphic = 56; // The sprite that contains the graphic for this item
}



// set firearm:

  player_rightarm = 32Revolver;

// display equipped weapon:

Display(String.Format("You have equipped the %s.", firearms[player_rightarm].name));


That way you won't have to deal with the index numbers anymore, too.


To answer your question:

Code: ags
int IndexOfWeaponNamed(String name) {

  int i;
  while (i < TOTAL_AMOUNT_OF_WEAPONS) {
    if (name == firearm[i].name) return i;
    i++;
  }
  return -1;  // not found
}

WHAM

#3
I've already sort of written a LOT of weapons with this system, and other scraps of code already reference the array numbers, so I won't move onto enums at least yet.

The solution you posted works like a charm, as always. Thanks!

The reason the righthand variable stores the name, is that it must search several arrays of structs, and in that case I would also need to store the struct type I am searching from somewhere.

EDIT: Bleugh! I'm going to rewrite most of the stuff anyway, now that I've learned some new tricks, so I'll probably merge all the different structs into a single one and add a "type" variable inside to make different types of items (melee weapon, firearm, healing item etc). Makes for simpler searching and gives me a chance to simplify and make things more uniform and sensible.

I'm still not sure about the enum -thing, but I'll take a closer look into it as well, to see if it better suits the requirements of the game.
Wrongthinker and anticitizen one. Utterly untrustworthy. Pending removal to memory hole.

WHAM

Quick added question: how much text can a string contain? Is there a limit to number of characters allowed?
If there is, what is the best way to store large amounts of text so that it can be called when needed? A separate function that contains several string?

My idea is to have a large label that would contain an entire page full of text and have the potential to scroll, in order to contain even more text.
Wrongthinker and anticitizen one. Utterly untrustworthy. Pending removal to memory hole.

monkey0506

#5
The String type is restricted only by physical memory (i.e., the end-user's RAM). Each String takes up 4 bytes plus 1 byte per character. So a String with about 268 million characters would require the user to have 256 MB of available RAM. :=

This type of restriction by physical memory gives the String type a "virtual" limitless length.

Oh, and also, it should be noted that the String.Format command can only handle strings up to..I think 2048 characters, or something like that. You probably won't need to use it for anything longer than that (most users won't anyway), but if you do ever encounter such a scenario then the workaround is to simply use String.Format with shorter strings and then use String.Append (which does not have this restriction). Why do I know this? Coz I've used the String type for vectorization of data several times and CJ actually upped the limit because of me! 8)

WHAM

Great! This means I should have no further need to worry about this when designing my next project. =)

Thank you!
Wrongthinker and anticitizen one. Utterly untrustworthy. Pending removal to memory hole.

SMF spam blocked by CleanTalk