Recent

Author Topic: Dynamic arrays concatenation, insertion and deletion of elements  (Read 1034 times)

lainz

  • Hero Member
  • *****
  • Posts: 3800
  • Leandro Diaz
Re: Dynamic arrays concatenation, insertion and deletion of elements
« Reply #15 on: October 03, 2020, 04:43:53 am »
1) What would be an %u201Cideal%u201D, but real life example of using these dynamic arrays features?

For example, when writing a game you use TInventory to store player's items.

Code: Pascal  [Select][+][-]
  1. const
  2.   MaxItemID = 7;
  3.   HPBig     = 1; // big healing potion
  4.   HPSmall   = 2; // small healing potion
  5.   MPBig     = 3; // big mana potion
  6.   MPSmall   = 4; // small mana potion
  7.   Sw        = 5; // common sword
  8.   SwSmall   = 6; // small sword
  9.   SwLong    = 7; // long sword
  10.  
  11. type
  12.   TInventory = array of Byte;
  13.  
  14. var
  15.   Inventory = TInventory;
  16.    
  17. // Give some bonus items if player finish a quest
  18. procedure BonusItems;
  19. begin
  20.   Inventory := Inventory + [HPSmall, MPSmall] + [Random(MaxItemID)+1];
  21. end;

It just quickly written example. For better coding I probably will use enum type for the items' IDs.

I think it depends on the size of the game inventory.
There are games with thousands of items.

I think arrays are useful when doing intensive computing that needs to run fast. For anything else there are lists that are easier to use (using arrays or not internally).
https://lainz.github.io/
Download LazPaint https://lazpaint.github.io/
Download BGRABitmap and BGRAControls https://github.com/bgrabitmap

Handoko

  • Hero Member
  • *****
  • Posts: 3902
  • My goal: build my own game engine using Lazarus
Re: Dynamic arrays concatenation, insertion and deletion of elements
« Reply #16 on: October 03, 2020, 05:17:32 am »
Yes, you're right. For most cases we should use list but for beginners who just start to learn building games, array is good enough for simple games like Pacman. That will make them better understand how data being stored and manipulated in memory. Lists are more practical and easier but they miss the chance of learning the low-level process.

 

TinyPortal © 2005-2018