Page 1 of 1
Reference from a list for dynamic use?
Posted: Wed Nov 18, 2020 4:15 pm
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.
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)
Re: Reference from a list for dynamic use?
Posted: Wed Nov 18, 2020 6:10 pm
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)
Re: Reference from a list for dynamic use?
Posted: Wed Nov 18, 2020 8:40 pm
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).
Re: Reference from a list for dynamic use?
Posted: Wed Nov 18, 2020 9:54 pm
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.
Re: Reference from a list for dynamic use?
Posted: Thu Nov 19, 2020 5:31 pm
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).
Re: Reference from a list for dynamic use?
Posted: Thu Nov 19, 2020 5:53 pm
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).
Re: Reference from a list for dynamic use?
Posted: Fri Nov 20, 2020 12:59 pm
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.