November 29, 2020, 05:18:10 PM

Author Topic: I have a question about Lists  (Read 235 times)

Offline takis76

  • Full Member
  • ***
  • Posts: 111
I have a question about Lists
« on: October 13, 2020, 05:22:44 PM »
Hello, my friends,

I have one question about lists.
In my game, I have one list that holds the items that are dropped on the floor.

It was initialized like this:
Code: [Select]
Global Floor_Items_List:TList = CreateList()

This list contains item objects what were created with a type:

Code: [Select]
Type My_Dropped_items

Field Level:Byte
Field X:Byte
Field Y:Byte
Field Quadrant:Byte
Field Item_String:String

EndType
Global Dropped_Items:My_dropped_items = New My_dropped_items

In the game there are pressure platforms of the floor that you drop items on it and these platforms are getting triggered with the items
that has the same position x and y on my map.

The pressure platforms are data type objects too:

Code: [Select]
Type My_PPlatform

Field X:Byte
Field Y:Byte
Field State:Byte '1: Up 2: Down

Field Triggered_By_Any_Item:Byte 'If any item be placed on it will be triggered
Field Trigerred_By_Item_Category:Byte 'If only a specific category of an item will be placed on


End Type
Global PPlatform:My_PPlatform[max_levels + 1, Max_PPlatforms + 1]
For All_Levels = 1 To Max_Levels
For All_PPlatforms = 1 To Max_PPlatforms
PPlatform[All_Levels, All_PPlatforms] = New My_PPlatform
Next
Next

The items that placed on the floor and on the platforms are strings with 3 digits. "000" - "999" then the first digit is the category of the item and the other 2 are the number of existing items in each category.
For example, all the weapons are in category 1 and there are 99 different weapons in this category.

IF I like to add 1 weapon on the Dropped_items list I use this code.

Code: [Select]
'Create a new item object
Dropped_Items = New My_Dropped_items
Dropped_Items.level = Party.Level
Dropped_Items.X = Party.X
Dropped_Items.Y = Party.Y
Dropped_Items.quadrant = 1
Dropped_Items.Item_String = "101"
ListAddLast(Floor_Items_List, Dropped_Items)

The variable "Party" is another datatype of the location of the party at the time you dropped the item and the quadrant is the quarter area of the square.
The Item_String is the ID number "101" is a sword.

When I am placing items on my platforms, in fact, I place my items on the floor but I compare the position X and Y of the platform with the position
X and Y of the Dropped_Items.

Code: [Select]
For Dropped_Items = EachIn Floor_Items_List

If Dropped_Items.X = PPlatform[Party.Level, selected_PPlatform].X and Dropped_Items.Y = PPlatform[Party.Level, selected_PPlatform].Y Then
        'Do something
End If

Next

The Pressure Platforms can be triggered by the Party and By Items that placed on them. By some time, only a specific item can trigger these platforms to create a riddle or a special adventure for the game.

For example, if I like the specific Pressure Platform not get triggered by any item but only with items that belong into category 1. "1xx" of 99 weapons.

But there is my problem. If on the platform there is a correct and wrong item too, or the correct one with another 10 wrong items, then the platform is not get triggered, only if I remove all items wrong items and leave the correct one "101" the platform will get triggered. For example, my platform will contain the item "101", "401" and "701"
If I will add all, the platform is no triggering, and if I will remove the items "401" and "701" and only the item "101" remains the platform triggers.

The problem is, my code sees only the last item inside the list, and for this example is the item "101" if I will add the Item "101" on the platform will work.
If I will add the item "401" then the platform will not work because the list sees the last item.
Is it possible to scan all items and see if among them the correct item exists in the stuck? regardless if the list has many different items.
Just to see if among of the item the correct item exists then the platform will work. To make my platform not work I just add a return command and I am not running the press platform code that is after the return.

The command:

Code: [Select]
ListContains(Floor_Items_List, "401")

Doesn't seems to work. The item "401" is the wrong item.

  • The first image presents both items on the platform.
  • The second image presents the error, if I will remove the Beaker = "401" from the platform and only the sword = "101" is on the platform
    the platform works.

This code sees only the last item:
Code: [Select]
For Dropped_Items = EachIn Floor_Items_List

If Dropped_Items.X = PPlatform[Party.Level, selected_PPlatform].X and Dropped_Items.Y = PPlatform[Party.Level, selected_PPlatform].Y Then
If Dropped_Items.Item_String <> "101" Then Return
End If

Next

Thank you very much :)

Offline Kronos

  • Jr. Member
  • **
  • Posts: 36
Re: I have a question about Lists
« Reply #1 on: October 13, 2020, 09:57:08 PM »
Get rid of this. Global Dropped_Items:My_dropped_items = New My_dropped_items

Not sure that you should use Dropped_Items as both the name of the object that you are creating and the iterator in your for eachin loops. The last object in your list will constantly change as you iterate through the list.

ListContains(Floor_Items_List, "401") is looking for a string object in your list but as it only contains My_Dropped_Items objects it will always return false.

I might be wrong, havent used Blitzmax in a while.

Offline Matty

  • Hero Member
  • *****
  • Posts: 1254
    • MattiesGames
Re: I have a question about Lists
« Reply #2 on: October 13, 2020, 10:05:03 PM »
I believe the logic you want is this (with pseudo code not actual code)

Code: [Select]
boolean found = false
string searchtype = "101"
for droppeditems = each droppeditem
   if droppeditem.x = platformitem.x and droppeditem.y = platformitem.y and droppeditem.type = searchtype then
      found = true
      exit for loop
  endif
next
if found then
    do what you need to when if it was found.
endif

Offline takis76

  • Full Member
  • ***
  • Posts: 111
Re: I have a question about Lists
« Reply #3 on: October 13, 2020, 10:57:23 PM »
I think I am using something that searches if there is the correct item category on the platform and finds it when the correct item is alone. The last one.

The whole function is this:

Code: [Select]
Function Push_Presure_Platform_items(selected_PPlatform:Byte)

'This function will executed only if a specific Item Category is met
If PPlatform[Party.Level, selected_PPlatform].Trigerred_By_Item_Category = 1 and PPlatform[Party.Level, selected_PPlatform].Triggered_By_Any_Item = 0 Then

For Dropped_Items = EachIn Floor_Items_List

If Dropped_Items.X = PPlatform[Party.Level, selected_PPlatform].X and Dropped_Items.Y = PPlatform[Party.Level, selected_PPlatform].Y Then

'Check the Item category
Check_Item_Category = Mid(Dropped_Items.Item_String, 1, 1)
If PPlatform[Party.Level, selected_PPlatform].Set_Item_Trigerring_Category <> Check_Item_Category then Return

End If

Next

EndIf

'Push the Platform
PPlatform[Party.Level, selected_PPlatform].State = 2
PPlatform[Party.Level, selected_PPlatform].Just_Pressed_items = 1
PPlatform[Party.Level, selected_PPlatform].Just_Released_items = 0
PPlatform[Party.Level, selected_PPlatform].Item_Placed = 1

'Play the stone sound
If PPlatform[Party.Level, selected_PPlatform].Sound_Played_Down = 0 and PPlatform[Party.Level, selected_PPlatform].Sound_Played_Up = 1 then
PlaySound(PPlatform_Down, GetPooledChannel("PPlatform[" + Party.Level + "," + PPlatform[Party.Level, selected_PPlatform].X + "," + PPlatform[Party.Level, selected_PPlatform].Y + "]"))
PPlatform[Party.Level, selected_PPlatform].Sound_Played_Down = 1
PPlatform[Party.Level, selected_PPlatform].Sound_Played_Up = 0

'Execute the Platform Operations
Presure_Platform_Operations(selected_PPlatform)
Endif

End Function

The PPlatform[Party.Level, selected_PPlatform].Set_Item_Trigerring_Category holds the value "1"
The Platform has the Item "101".
The Check_Item_Category = Mid(Dropped_Items.Item_String, 1, 1) cuts the first string digit only from "101" and holds the "1"
The If PPlatform[Party.Level, selected_PPlatform].Set_Item_Trigerring_Category <> Check_Item_Category then Return compares both values
if they are not equal to return and ignore the rest of the function. But it works in the last item only.

Offline takis76

  • Full Member
  • ***
  • Posts: 111
Re: I have a question about Lists
« Reply #4 on: October 13, 2020, 11:03:36 PM »
@Kronos
Quote
Get rid of this. Global Dropped_Items:My_dropped_items = New My_dropped_items
What you mean to get rid of this? Is this code wrong? What needs to be changed there?

Offline takis76

  • Full Member
  • ***
  • Posts: 111
Re: I have a question about Lists
« Reply #5 on: October 13, 2020, 11:14:38 PM »
I changed the:

Code: [Select]
Global Dropped_Items:My_dropped_items = New My_dropped_items

To

Code: [Select]
Global Dropped_Items:My_dropped_items

What is the difference?
But I do not see any difference in the result.

Offline Kronos

  • Jr. Member
  • **
  • Posts: 36
Re: I have a question about Lists
« Reply #6 on: October 13, 2020, 11:34:43 PM »
@Kronos
Quote
Get rid of this. Global Dropped_Items:My_dropped_items = New My_dropped_items
What you mean to get rid of this? Is this code wrong? What needs to be changed there?

Just seems a bit dangerous having it as a global as you appear to be using it in different places for different purposes. Better to create a locals  in the places that you need, preferably with unique names.

Offline takis76

  • Full Member
  • ***
  • Posts: 111
Re: I have a question about Lists
« Reply #7 on: October 13, 2020, 11:41:48 PM »
If I will make it Local I will not able to see it in the whole program scope. The Dropped_Items is the global items data type that needs to be visible in all functions.

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal