Noob question about the nature of OOP
Noob question about the nature of OOP
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?
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?
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?
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?
-
- Posts: 2655
- Joined: Aug 28, 2008 10:54
- Location: new york
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:
Then you would make a Game Type...
Lemme know if anything needs clarification...
This is an outline of how I attack the same problem...
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
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
This is an outline of how I attack the same problem...
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?
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?
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?
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?
-
- Posts: 2655
- Joined: Aug 28, 2008 10:54
- Location: new york
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
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
-
- Site Admin
- Posts: 6323
- Joined: Jul 05, 2005 17:32
- Location: Manchester, Lancs
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.
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