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

Just starting out? Need help? Post your questions and find answers here.
emperor-limitless
User
User
Posts: 11
Joined: Tue Nov 12, 2024 5:14 pm

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

Post 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?
emperor-limitless
User
User
Posts: 11
Joined: Tue Nov 12, 2024 5:14 pm

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

Post 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 :(
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

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

Post by STARGÅTE »

You can use CopyStructure()
Strictly speaking: CopyStructure(test, thing, Zift)
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
mk-soft
Always Here
Always Here
Posts: 6207
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

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

Post 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
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
emperor-limitless
User
User
Posts: 11
Joined: Tue Nov 12, 2024 5:14 pm

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

Post 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?
Kulrom
User
User
Posts: 16
Joined: Thu Sep 07, 2023 6:07 am

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

Post 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.
I love programming languages that start with the letter "P":
Python, Pascal and ... PureBasic! :)
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

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

Post 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).
User avatar
mk-soft
Always Here
Always Here
Posts: 6207
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

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

Post 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
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
Post Reply