Noob question about the nature of OOP

New to FreeBASIC? Post your questions here.
Post Reply
Velatus
Posts: 86
Joined: Mar 21, 2009 23:43
Location: France

Noob question about the nature of OOP

Post by Velatus »

Hey there folks.
I'm a bit new to programming (only some stuff in RapidQ and a couple of things in FB), and by the way i'm french.
There is something I would like to understand about OOP, since i've only used procedural languages so far.
Thing is i would like to code a small hack'n'slash game. Nothing too fancy: a player character in the middle of the screen, and critters spawning and running into it.
These critter objects would have some very basic AI (like "run to player character's position and switch to attack mode when within a certain range").
The AI part would certainly not be too hard to implement, but how do i actually spawn the critters in FB (i.e. create multiple instances of the critter object on the map)? And most of all how do i destroy them?
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Post by agamemnus »

You would typically have a UDT (user-defined type) with the data points of a creature, then you'd fill the data points by creating an instance of the type -- for instance, in an array of UDTs. Remove/edit as normal.
Velatus
Posts: 86
Joined: Mar 21, 2009 23:43
Location: France

Post by Velatus »

Thanks for answering so quickly, Agamemnus.
It's simpler than I thought... :)
Would you mind answering another silly question?
How do i make my critters move, i mean all at the same time?
Do i have to cycle through my array of UDT every given amount of time (like with an ONTIMER sort of event that would trigger a FOR loop), and update the positions on the map accordingly?
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

Post by rolliebollocks »

Valatus...

The questions your asking have answers which are more or less processes...

In order to control multiple agents in a game you must store them in array, and loop through the array each time in your game loop...

A chase routine is really easy...

If human's x pos > enemy x pos then ( enemy x pos ) += Acceleration

So if your bad guy is left of your human, he is going to want to go right...

So first you need an NPC type:

Code: Select all

type NPC
   xpos as single
   ypos as single
   velx as single 
   vely as single

  image as fb.image ptr

   declare sub Chase ( inNPC as NPC ptr)
   declare sub Process ( )

end type

sub NPC.Chase ( inNPC as NPC ptr )

   if this.xpos > inNPC->xpos then this.velx += 1 else this.velx -= 1
   if this.ypos > inNPC->ypos then this.vely += 1 else this.vely -= 1

end sub

sub NPC.Process ()
  
   'move him...
   this.xpos += this.velx
   this.ypos += this.vely

'Draw him
    put ( this.xpos, this.ypos ), this.image, trans

end sub
Then you would make a Game Type...

Code: Select all

type Game
  
   dim as NPC ptr badguys(100) 
   dim as NPC ptr human
   declare Constructor 
   declare sub Process

end type

Constructor Game

for i as integer = 0 to ubound(badguys)
   badguys(i) = new NPC
next

end Constructor

sub Game.Process
   for i as integer = 0 to ubound(badguys)
      badguys(i)->Process()
   next
end sub
end type
Lemme know if anything needs clarification...

This is an outline of how I attack the same problem...
Velatus
Posts: 86
Joined: Mar 21, 2009 23:43
Location: France

Post by Velatus »

Thank you very much mister rollibollocks.
Looks good.
There is actually a couple of things that need clarification.
First, why did you use the "Ptr" keyword? I mean: i more or less know what pointers are about (i.e. something pointing to a memory address), but why do you use "Ptr" in this particular case?
Second, i had never seen the "New" statement before. Could you please tell me what it does? Because now i'm just guessing: is it the way you actually create a new instance of a type?
Velatus
Posts: 86
Joined: Mar 21, 2009 23:43
Location: France

Post by Velatus »

Oh and by the way -- boy am i an ignorant --, what does the "->" do? I've never seen it, nor used it obviously. Is it FB, or pseudo-code?
axipher
Posts: 891
Joined: Dec 27, 2005 16:37
Location: Sudbury,Ontario

Post by axipher »

Using pointers is much faster for cases like this. The NEW and DELETE keywords are used with pointers to make new instances of the UDT and call their constructor if they have one. The "->" is a special way to access data within a UDT pointer
Velatus
Posts: 86
Joined: Mar 21, 2009 23:43
Location: France

Post by Velatus »

Right. It's getting a bit clearer. And thank you axipher.
So if i get this right, in rolliebollocks' example the data stored in badguys() are pointers to each bad guy, and the "->" symbol allows me to run the private sub NPC.Process() from within the "Game" object, through its private sub Game.Process().
Is it what this symbol is made for, or is it just me guessing?
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

Post by rolliebollocks »

The sub is only private if you specify it as such.

Here there's a confusion of terms, because private/public means something specific here.

A public sub can be accessed anywhere, while a private sub can only be accessed within the UDT procedures...

Basically, private subs are way to further encapsulate your data...

rb
Velatus
Posts: 86
Joined: Mar 21, 2009 23:43
Location: France

Post by Velatus »

There was a confusion of terms indeed. Nothing to do with privateness here.
What is "->" specifically made for then? axipher wrote it was a special way to access data. But what is it that makes it special?
I don't want to sound annoying or anithing, but i'd like to know what i'm doing.[/i]
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

The '->' operator lets you access elements of a user-defined type, similar to how '.' does, except that it doesn't work directly on a variable of that type, but a pointer to such a variable.

For example, let's say p is a pointer to a UDT variable, meaning that *p gives you the variable itself.

Then, you could use p->elt to access the "elt" element of the variable, the same way (*p).elt would.

Code: Select all

type udt
    as integer a, b, c
end type

dim u as udt
dim p as udt ptr = @u

u.a = 10

'' these all give the same thing:
print u.a
print (*p).a
print p->a
Post Reply