Reference from a list for dynamic use?

Just starting out? Need help? Post your questions and find answers here.
Joubarbe
Enthusiast
Enthusiast
Posts: 703
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Reference from a list for dynamic use?

Post by Joubarbe »

So I’m not sure exactly what PB does when I pass a list for DeleteElement(). I’d like to delete elements without knowing the list.

Code: Select all

Procedure DeleteComponent(compo_list(), *element_to_delete)
...
EndProcedure
This would probably need to be a Macro, then I would have a reference of my list somewhere.

Code: Select all

*element_to_delete\*list()
I’ve tried a combination of macros and prototypes without success. At some point, I always have to write explicitly the name of the list.

EDIT:

Here is my code:

Code: Select all

ForEach \components()
  Select \components()\name
    Case "Placeable"
      ChangeCurrentElement(Placeable::placeables(), \components())
      DeleteElement(Placeable::placeables())
  EndSelect
Next
I want to get rid of that Select/EndSelect block that is going to be very tedious at some point. What I’d like to have is:

Code: Select all

      ChangeCurrentElement(\components()\listRef, \components())
      DeleteElement(\components()\listRef)
Olli
Addict
Addict
Posts: 1203
Joined: Wed May 27, 2020 12:26 pm

Re: Reference from a list for dynamic use?

Post by Olli »

Joubarbe wrote:I’d like to delete elements without knowing the list.
This wish is a little bit confusing...

There is two context :

1) delete (or modify, insert, etc...) an element whatever the list is, just matching the data type (string for strings list, integer for integers list, structured datas for structured datas list, etc...)

In this way, you have to store a list inside a structured buffer.

Code: Select all

Structure MyList
   List E.S()
EndStructure

Define *X.MyList = AllocateStructure(*X, MyList)
; this above is equivalent to NewList()

Procedure HandleList(*This.MyList)
; here, an element or a list is accessed by *This\E()

; i.e.
AddElement(*This\E() )
*This\E() = "Hello"

EndProcedure

; here is a call
HandleList(*X)
2) Second context : delete a detailed element which is the same in several lists. In this way, all the lists which must have a element to be removed have to be argumented

Code: Select all

Remove("Test", List1(), List2() )
; call a user fonction named Remove() to remove all the occurences "Test" in List1 and in List2.
This implies a procedure must search a specific string (ie : "Test") and remove the matching element.
This implies also a strict quantity of lists to be treatened.

Could you give more details about your wish? (1st or 2nd context, maybe a third context)
Joubarbe
Enthusiast
Enthusiast
Posts: 703
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: Reference from a list for dynamic use?

Post by Joubarbe »

I have an Entity-Component system, and I need to remove the "instanciated" components attached to an entity when I delete this entity. So it would be first context. Problem is still the same though: in your example, I would need to know the structure, because each component has a different one. I actually would be very surprised if there is a way to have a "reference of a list".

"not knowing the list": in other words, I have a pointer of a specific component, pointed at a list element, then I want to be able to remove this element in a dynamic way, because an entity can have different components, hence several component lists (because different structures).
User avatar
mk-soft
Always Here
Always Here
Posts: 6209
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Reference from a list for dynamic use?

Post by mk-soft »

It is not possible to pass lists to procedures that have different structures.

Alternatice create a list with pointers. This list can contain the pointers created with AllocateStructure. This can then be deleted in the procedure using FreeStructure and DeleteElement of the pointer.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Olli
Addict
Addict
Posts: 1203
Joined: Wed May 27, 2020 12:26 pm

Re: Reference from a list for dynamic use?

Post by Olli »

@mk-soft

yes, he could do it if :

1) the names of the element structures and the names of the nested list structures follow a right syntax, by having a common structure name prefix.

2) (in dynamical structure management) every element have a root list pointor. If there is only one list nesting structure per element structure type, so no need to add root list pointor.

ie :

Code: Select all

Structure Struc1
 monday.I
 tuesday.I
EndStructure

Structure Struc2
 monday.S
 tuesday.S
EndStructure
Above,
prefix is << Struc >>

In this way, through a set of macros, this wish is able to exist. It will be just unable to decide to name freely the structures which nest the lists. (other alternative, where each element type has its own list).
Joubarbe
Enthusiast
Enthusiast
Posts: 703
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: Reference from a list for dynamic use?

Post by Joubarbe »

I thought of that, but then if you want to dynamically get the name of anything, you would get it through a string. Then in the Macro, you would have FreeStructure("MyStructure") instead of FreeStructure(MyStructure).
Olli
Addict
Addict
Posts: 1203
Joined: Wed May 27, 2020 12:26 pm

Re: Reference from a list for dynamic use?

Post by Olli »

I do not understand the rule with double-quote : it is not required. Give a little example.

To bypass the problem of type, use the tip of mk-soft :

Code: Select all

Structure ListSet
   List A.StrucA()
   List B.StructureB()
   List C.OtherStructure()
EndStructure
But if you do not know, at start, the quantity of structures which will be managed, use the tip I described earlier, with a rule of structure name prefix and a use of macros (create, get, insert, remove, destroy).

The strength technical limit you have to keep in mind (I suppose mk-soft thank initially, it was your question), is once your program is compiled, you cannot add an other type of structure without adding all the management functions : a new dll should contain the five functions (create, get, etc...) for each new type of element structure.
Post Reply