October 25, 2021, 11:54:28

Author Topic: How to create inheritance?  (Read 787 times)

Offline zelda64bit

  • Jr. Member
  • **
  • Posts: 54
How to create inheritance?
« on: August 08, 2021, 15:10:47 »
I give an example and explain what I want to achieve.

Code: [Select]
SuperStrict
AutoMidHandle(True) 'centra origen en el medio

Type Tjugador
Field x:Int
Field y:Int
Field rotacion:Int
Field imagen:TImage = LoadImage("00.png")

Function Create:Tjugador()
Local jugador:Tjugador = New Tjugador
SeedRnd(MilliSecs()) 
jugador.x = Rand(32, 608)
jugador.y = Rand(100, 450)
Return jugador
EndFunction

Method pintar()
rotacion:+ 1
SetScale(0.5,0.5)
SetRotation(rotacion)
DrawImage(imagen,x,y)
EndMethod

EndType

Global objetos:Int

Graphics(640,480)


Global lista_jugador:TList = CreateList()

While Not KeyDown(key_escape)
SetScale(2,2)
SetRotation(0)
DrawText("frames: "+GraphicsHertz(),100,32)
DrawText("objetos: "+objetos,320,32)

If(GraphicsHertz() > 58) Then
Local jugador:Tjugador = Tjugador.Create()
ListAddLast(lista_jugador,jugador)
objetos:+1
EndIf

For Local j:Tjugador = EachIn lista_jugador
j.pintar()
Next

Flip()
Cls()
EndWhile


In the example I create an object and add it to a list, then I loop through the list and show it. If I have to do this on all the objects I create it can get heavy having to create a list for each object and loop through each object. As you could do all this in a parent class and that by inheriting from this class, you would inherit all its properties and no longer have to create more lists and loops to display objects. I don't know if I've explained myself well.

Offline Derron

  • Hero Member
  • *****
  • Posts: 3674
Re: How to create inheritance?
« Reply #1 on: August 08, 2021, 19:14:02 »
I do not really understand what you exactly plan to achieve.

Code: [Select]
TGameObject
> Method Update()
> Method Draw()

TShip extends TGameObject

TPlanet extends TGameObject


List:TList
List.addLast(new TPlanet)
List.addLast(new TShip)

'all
For local o:TGameObject = EachIn list
  o.Update
Next

'ignore all but the ships (or extending from ship)
For local s:TShip = EachIn list
  s.Update()
Next


Maybe this already helps you with the issue you think you have with inheritance:
https://blitzmax.org/docs/en/tutorials/oop_tutorial/#inheritance


bye
Ron

Offline zelda64bit

  • Jr. Member
  • **
  • Posts: 54
Re: How to create inheritance?
« Reply #2 on: August 08, 2021, 20:43:27 »
When you create an object you have to add it to a list and then loop through that list. What I want to do is do this same thing in a class and then have others inherit from that class so I don't have to do that thing of creating a list and going through it. It is what unity does internally, that when you create an object you can show it without adding it to a list to show it or delete it.

Offline Derron

  • Hero Member
  • *****
  • Posts: 3674
Re: How to create inheritance?
« Reply #3 on: August 09, 2021, 08:39:06 »
somewhere you have to call specific functions ... OR ... you provide an "event" in which the systems hook in on first use (eg adding the first game object)

Code: BlitzMax
  1. Type TGameObject
  2.   global elements:TList = new TList
  3.  
  4.   Method New()
  5.      elements.AddLast(self)
  6.   End Method
  7.  
  8.   Method Update()
  9.     print "Update Base"
  10.   End Method
  11.  
  12.   Method Render()
  13.     print "Render Base"
  14.   End Method
  15.  
  16.   Function UpdateAll()
  17.     For local o:TGameObject = EachIn elements
  18.       o.Update()
  19.     Next
  20.   End Function
  21.  
  22.   Function RenderAll()
  23.     For local o:TGameObject = EachIn elements
  24.       o.Render()
  25.     Next
  26.   End Function
  27. End Type
  28.  
  29.  
  30. Type TShip extends TGameObject
  31.   'the override is NOT needed but helps as NG then fails to compile
  32.   'if you removed or renamed the "Render" method in TGameObject and
  33.   'so making the override no longer an override
  34.   Method Render() override
  35.     print "Render Ship"
  36.   End Method
  37. End Type
  38.  
  39. 'create a new ship
  40. local myShip:TShip = new TShip
  41. 'attention this does NOT delete the old ship but
  42. 'adds a new one now!
  43. myShip = new TShip
  44.  
  45. 'adds even more ships
  46. For local i:int = 0 until 10
  47.   new Tship
  48. Next
  49.  
  50.  
  51. 'loop...
  52. TGameObject.UpdateAll()
  53. TGameObject.RenderAll()
  54.  


Another approach - and I think the better one is to NOT use the "new()" as this hides the "addition".


Code: BlitzMax
  1. Type TGameObject
  2.   global elements:TList = new TList
  3.  
  4.   Method Update()
  5.     print "Update Base"
  6.   End Method
  7.  
  8.   Method Render()
  9.     print "Render Base"
  10.   End Method
  11.  
  12.   Function UpdateAll()
  13.     For local o:TGameObject = EachIn elements
  14.       o.Update()
  15.     Next
  16.   End Function
  17.  
  18.   Function RenderAll()
  19.     For local o:TGameObject = EachIn elements
  20.       o.Render()
  21.     Next
  22.   End Function
  23.  
  24.   Function Register(o:TGameObject, skipCheck:int = False)
  25.     if skipCheck or not elements.contains(o)
  26.       elements.AddLast(o)
  27.     endif
  28.   End Function
  29.  
  30.   Function Unregister(o:TGameObject)
  31.     elements.Remove(o)
  32.   End Function
  33. End Type
  34.  
  35.  
  36. Type TShip extends TGameObject
  37.   'the override is NOT needed but helps as NG then fails to compile
  38.   'if you removed or renamed the "Render" method in TGameObject and
  39.   'so making the override no longer an override
  40.   Method Render() override
  41.     print "Render Ship"
  42.   End Method
  43. End Type
  44.  
  45. 'create a new ship
  46. local myShip:TShip = new TShip
  47. 'attention this does delete the old ship
  48. myShip = new TShip
  49. 'manage this ship
  50. TGameObject.Register(myShip)
  51.  
  52. 'adds even more ships
  53. For local i:int = 0 until 10
  54.   TGameObject.Register( new TShip)
  55. Next
  56.  
  57.  
  58. 'loop...
  59. TGameObject.UpdateAll()
  60. TGameObject.RenderAll()
  61.  




bye
Ron
« Last Edit: August 09, 2021, 08:50:34 by Derron »

Offline Derron

  • Hero Member
  • *****
  • Posts: 3674
Re: How to create inheritance?
« Reply #4 on: August 09, 2021, 08:56:49 »
If you often "retrieve" elements anonymously (just storing them in the Gameobject-collection and only pass around the ID - a simple number). It might be a good idea to give each game object an ID ... and simply store the elements in a TIntMap. This way - and with eg an ensured "order" of adding objects, you can pass around the number in multiplayer code, or when storing stuff for a savegame ...


Code: BlitzMax
  1. Type TGameObject
  2.   Field id:int
  3.  
  4.   global lastID:Int = 0
  5.   global elements:TIntMap = new TIntMap
  6.  
  7.  
  8.   Method New()
  9.     lastID :+ 1
  10.     self.id = lastID
  11.   End Method
  12.  
  13.   Method Update()
  14.     print "Update Base"
  15.   End Method
  16.  
  17.   Method Render()
  18.     print "Render Base"
  19.   End Method
  20.  
  21.   Function UpdateAll()
  22.     For local o:TGameObject = EachIn elements.Values()
  23.       o.Update()
  24.     Next
  25.   End Function
  26.  
  27.   Function RenderAll()
  28.     For local o:TGameObject = EachIn elements.Values()
  29.       o.Render()
  30.     Next
  31.   End Function
  32.  
  33.   Function Register(o:TGameObject)
  34.     If not o then Return
  35.     'overrides an existing object with the same ID (so eg. if it was added before already)    
  36.     elements.Insert(o.id, o)
  37.   End Function
  38.  
  39.   Function Unregister(o:TGameObject)
  40.     If not o then Return
  41.     elements.Remove(o.id)
  42.   End Function
  43.  
  44.   Function Get:TGameObject(id:Int)
  45.     Return elements.ValueForKey(id)
  46.   End Function
  47. End Type
  48.  


If you need more than the 2147483647 game objects, you would need an "id:long" (and a TLongMap - which you would need to write/adapt on your own - or simply ask me/copy from TVTower/DIG-Framework source).


bye
Ron

Offline zelda64bit

  • Jr. Member
  • **
  • Posts: 54
Re: How to create inheritance?
« Reply #5 on: August 09, 2021, 15:21:51 »
Thank you very much Derron for the examples, I will study them calmly until I can apply them. ;)

By the way, the translator translates quite badly

Offline zelda64bit

  • Jr. Member
  • **
  • Posts: 54
Re: How to create inheritance?
« Reply #6 on: August 09, 2021, 20:12:41 »
I have it almost finished, now I have the object class and the player and enemy class that inherit from object.

The only thing I need is to be able to eliminate objects, for that I use the function remove to eliminate an object and the clear function to eliminate them all.

But there is a problem ... from the player class an instance is created and from the enemy class 10 instances are created, when I try to eliminate all the enemies with clear the 10 enemies and the player are eliminated, but I only want to eliminate the 10 enemies and leave the player alive.

How would it be done?

Offline Derron

  • Hero Member
  • *****
  • Posts: 3674
Re: How to create inheritance?
« Reply #7 on: August 09, 2021, 20:39:14 »
simply do not remove player from your list/map ?!

Remove what you want "individually", do not simply clear your list.


bye
Ron

Offline zelda64bit

  • Jr. Member
  • **
  • Posts: 54
Re: How to create inheritance?
« Reply #8 on: August 10, 2021, 14:49:47 »
But isn't there some way to delete many objects of one class, without affecting objects of other classes?

Offline Derron

  • Hero Member
  • *****
  • Posts: 3674
Re: How to create inheritance?
« Reply #9 on: August 10, 2021, 16:06:39 »
variant 1:
Remove each one separately.

variant 2:
have lists for each "class" - then you can of course empty them the way you want.
Of course it will be very hard then to "sort" them (when taking other object classes into consideration too - so ships behind a planet, a planet behind ships ...)


If you want to delete objects a thousand times per frame, then you might better think of a different solution to your problem. If you just want the "ability" to remove all "classX" from the elements list you can simply do:

For local my:TMySpecialClass = EachIn myList
...

it will skip everything "incompatible" (not extending or exactly being TMySpecialClass).
Please keep in mind that you should avoid modifying the "myList" when iterating over it - you might run in the "concurrent list modification" issue (iterator might stumble over it).
TIntMap would be better there then -> store the IDs of "to Delete" elements, and then remove by "ID" in a second step.

Else you need to find _fast_ ways to access elements via "index". Brl.ObjectList contains a TList like array. When removing an element do not forget to decrease the "index value" (else you skip stuff then too).


bye
Ron

Offline zelda64bit

  • Jr. Member
  • **
  • Posts: 54
Re: How to create inheritance?
« Reply #10 on: August 11, 2021, 14:36:41 »
Thanks for the help.

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal