Page 1 of 1

copying a structure to a variable with a type that was extended from?

Posted: Thu Nov 14, 2024 6:34 pm
by emperor-limitless
hi
so I just realized pb has extending, which is nice, however I wonder, is it possible to have something like

Code: Select all

Structure State
  Name$
  Update.i
  Enter.i
  Exit.i
  OnFocus.i
  OnUnfocus.i
  OnEscape.i
EndStructure

Structure Zift Extends State
  Meow$
EndStructure

Procedure Testing()
  thing.State
  test.Zift
  test\Name$ = "this will totally not work I am sure"
  thing = test
EndProcedure
]
this obviously doesn't compile, but I'm wondering if there's some way to make it work? I would have done interfaces, but interfaces only support functions, not properties.
I am aware of the pure basic's community's dislike of object oriented programming, which is why I was plesently surprised when I realized extend exists, though the final step will be this one :), I don't probably need anything else
basically, what I'm trying to do is to have a general State structure, those things like update etc are sorta like methods but done through pointers, I think it will work? Not sure, but every different state will be able to extend the main state, put it's own variables, then I'll make a small function for each structure that gives the pointers for each one etc
is this possible?
also random curiosity, does extend also give the extended structure the interface declaration if one was done? Like through the data section, or does that need to be done seperately?

Re: copying a structure to a variable with a type that was extended from?

Posted: Thu Nov 14, 2024 7:45 pm
by emperor-limitless
sorry for double posting, but if you think this will annoy people because it sounds like an oop question/ it' not currently possible and will never be, then please do suggest some different ideas. I'm primarily going to do this kind of stuff for games so I am open for different ideas, I'm just not smart enough to think of them yet :(

Re: copying a structure to a variable with a type that was extended from?

Posted: Thu Nov 14, 2024 8:17 pm
by STARGÅTE
You can use CopyStructure()
Strictly speaking: CopyStructure(test, thing, Zift)

Re: copying a structure to a variable with a type that was extended from?

Posted: Fri Nov 15, 2024 12:33 am
by mk-soft
STARGÅTE wrote: Thu Nov 14, 2024 8:17 pm You can use CopyStructure()
Strictly speaking: CopyStructure(test, thing, Zift)
No, with CopyStructure the variables must have the same structure. Otherwise there will be an error or crash.
So assign the values individually here. See PB Help

Re: copying a structure to a variable with a type that was extended from?

Posted: Fri Nov 15, 2024 3:36 am
by emperor-limitless
mk-soft wrote: Fri Nov 15, 2024 12:33 am
STARGÅTE wrote: Thu Nov 14, 2024 8:17 pm You can use CopyStructure()
Strictly speaking: CopyStructure(test, thing, Zift)
No, with CopyStructure the variables must have the same structure. Otherwise there will be an error or crash.
So assign the values individually here. See PB Help
I'm... Confused a bit here, check the pb help where? I checked it and besides the normal = copying I hadn't found anything discussing this?

Re: copying a structure to a variable with a type that was extended from?

Posted: Fri Nov 15, 2024 3:59 am
by Kulrom
CopyStructure make it.
I use СopyStructure in my OOP realisation.
This function copied memory from pointer1 to pointer2.
if the second structure is extended from the first, then everything will succeed, and all fields of the parent structure will be inherited.

Re: copying a structure to a variable with a type that was extended from?

Posted: Fri Nov 15, 2024 11:00 am
by Fred
You can use CopyStructure() without issue:

Code: Select all

CopyStructure(test, thing, State)
There is no check about source and dest, so be sure you're using it properly or it can leads to wierd crashs (if you use a larger source structure than the destination one).

Re: copying a structure to a variable with a type that was extended from?

Posted: Fri Nov 15, 2024 6:10 pm
by mk-soft
It is important to know that the ‘Extends’ structure is placed before the new structure. This is the only way inheritance can work

Code: Select all


Structure structBase
  BaseName.s
  Description.s
  BaseValue.i
EndStructure

Structure structData Extends structBase
  Name.s
  Value.i
EndStructure

Structure structBigData Extends structData
  x.i
  y.i
  dx.i
  dy.i
EndStructure

Define MyBase.structBase
MyBase\BaseName = "Name of Base"
MyBase\Description = "Description of Base"
MyBase\BaseValue = 100

Define MyData.structData
CopyStructure(@MyBase, @MyData, structBase)

MyData\Name = "Name of Data"
MyData\Value = 200

ShowVariableViewer()

CallDebugger