Results 1 to 6 of 6

Thread: [question] array of record

  1. #1
    Join Date
    Oct 2007
    Posts
    184
    Mentioned
    7 Post(s)
    Quoted
    60 Post(s)

    Default [question] array of record

    Having a bit of trouble here. I'm trying to dynamically populate an array of loot objects to make searching for them easier.

    Loot record:
    Code:
    loot = record
        name: String;       //stores variables that help find loot
        overText: TStringArray;    //mouseovertext of loot
        colours: T2DColorData;        //colours of loot
      end;
    when I initiate the objects of loot record, I set pointer to 0 ( x := 0) then try to add them to the array like this
    Code:
    dragonBones.init('Dragon Bones', ['ake', 'dragon', 'bones'], [[12698835, 5, [2, [0.93, 0.45, 0.00]]], [12106698, 5, [2, [0.31, 0.64, 0.00]]]]);
          lootArray[x] := dragonBones;
          inc(x);
    then I want to search for them with this:
    Code:
    for x to high(lootArray) do
      begin
        if players[currentPlayer].booleans[_DEBUG] then
          writeLn('Looking for ',lootArray[x].name, ' ...');
        if lootArray[x].find(false) then
        begin
          if players[currentPlayer].booleans[_DEBUG] then
            writeLn('Found ',lootArray[x].name, ' !!');
          clickedLoot := true;
          break;
        end;
      end;
    I believe I'm not populating the array correctly. Could anyone chime in with some advice? Much appreciated

  2. #2
    Join Date
    May 2012
    Location
    Glorious Nippon
    Posts
    1,001
    Mentioned
    50 Post(s)
    Quoted
    500 Post(s)

    Default

    Quote Originally Posted by klamor View Post
    ...
    How is 'lootarray' defined? What's its length?

  3. #3
    Join Date
    Oct 2007
    Posts
    184
    Mentioned
    7 Post(s)
    Quoted
    60 Post(s)

    Default

    Quote Originally Posted by Citrus View Post
    How is 'lootarray' defined? What's its length?
    Code:
    lootArray: array of loot;
    how do I define it with a dynamic size? or do I need to change it every time I add more loot items?

  4. #4
    Join Date
    May 2012
    Location
    Glorious Nippon
    Posts
    1,001
    Mentioned
    50 Post(s)
    Quoted
    500 Post(s)

    Default

    Quote Originally Posted by klamor View Post
    Code:
    lootArray: array of loot;
    how do I define it with a dynamic size? or do I need to change it every time I add more loot items?
    The size is dynamic, meaning it can change. It doesn't magically change when you try to access indices that don't exist. It only changes when you tell it to change.
    You can use SetLength(), or the '+' operator:
    Simba Code:
    SetLength(SomeArray, Length(SomeArray) + 1);
    SomeArray[High(SomeArray)] := NewElement;
    //or
    SomeArray := SomeArray + NewElement;
    If you're using Simba 1.2 there might be more options available (ask slacky).

    Just a little note to make your code more up to standards: your custom types should look something like this:
    Simba Code:
    type TLoot = record ... end;
    type TLootArray = Array of TLoot;

  5. #5
    Join Date
    Oct 2007
    Posts
    184
    Mentioned
    7 Post(s)
    Quoted
    60 Post(s)

    Default

    what's the need for it to be a new type rather than a simple var?

  6. #6
    Join Date
    May 2012
    Location
    Glorious Nippon
    Posts
    1,001
    Mentioned
    50 Post(s)
    Quoted
    500 Post(s)

    Default

    Quote Originally Posted by klamor View Post
    what's the need for it to be a new type rather than a simple var?
    There's no need AFAIK, just like there is no need to use intelligible variable names or indentation. It just helps with readability and organization. imo

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •