Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Author
Message
mnemonic
19
Years of Service
User Offline
Joined: 14th Jan 2007
Location: Sweden
Posted: 10th May 2013 16:32 Edited at: 10th May 2013 16:34
Hi!

As DBpro isn\'t an OOP language I came up with an idea of \'faking it\'

OOP is not a must, but it can be benefical.

Anyway! This is what I\'ve done so far:

File: Class_person1.dba



File: fakeOOP.dba (Set as main file, or better expressed: application entry point\')



One benefit of using OOP is more readable and manageble code. Without OOP we have a big \'spaghetti-code\' warning.

Why do I want to send the custom type(object) to it\'s \"methods\"?

This is because in reality you could just call these functions from anywhere in the program, so that argument is basically just to make sure the correct object uses it\'s corresponding \'methods\'

I get an error when compiling this saying:



so,,, what does that mean? Maybe it complains because I\'m sending the object as whole to the function, so therefore I\'d like to try sending a pointer instead.
But DBpro does not seem to handle pointers very well.

Mayby I\'m trying to do something impossible here. What do you think? Well, atleast impossible with DBpro

BEST!

www.memblockgames.com
Andrew_Neale
16
Years of Service
User Offline
Joined: 3rd Nov 2009
Location: The Normandy SR-2
Posted: 10th May 2013 16:48
Have you accidentally included the file within the IDE as well as using the include statement?


Previously TEH_CODERER.
James H
19
Years of Service
User Offline
Joined: 21st Apr 2007
Location: St Helens
Posted: 10th May 2013 17:07 Edited at: 10th May 2013 17:09
I dont know the first thing about OOP but I know that I get a different compilation error, fixed by changing 2 typos using code provided;


the \ outside the quotations...
Quote: "
"

presumably you havent posted all your code as
Quote: "\'SetAge\'"
does not appear in your snippets - however the error looks to be the same typo
ShellfishGames
13
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 10th May 2013 17:57 Edited at: 10th May 2013 17:58
Quote: "Maybe it complains because I\'m sending the object as whole to the function, so therefore I\'d like to try sending a pointer instead.
But DBpro does not seem to handle pointers very well.

Mayby I\'m trying to do something impossible here. What do you think? Well, atleast impossible with DBpro"


I'm not even sure whether sending whole type objects as parameter works in DBP, but I usually let each "class" have an array of its objects. So there's a "constructor" such as addPerson( <attributes> ) which adds an object to this array and returns the array index, and all the non-static methods take this index as first parameter.

Regarding the error message I'd assume that Andrew_Neale is right, at least those situations usually result in such an error, as far as I know.

Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 10th May 2013 20:46
Quote: "// Class_person1

type person1
age as integer
name as string
endtype

//\"fake methods\"

function SetAge(theObject as person1, value as integer)
theObject.age = value
ENDFUNCTION

function SetName(theObject as person1, value as string)
theObject.name = value
ENDFUNCTION"


Won't work. Functions cannot return user defined types. The easiest way to make that work is use a global UDT as a return object.

Chenak
23
Years of Service
User Offline
Joined: 13th Sep 2002
Location: United Kingdom
Posted: 12th May 2013 03:07
I think the easiest way to get OOP in DBPro is to use Barnskis LUA plugin. I have a really nice setup which allows for a lot of OOP functionality including inheritance, meta methods and instantiation. I think I may upload what I have because I've moved on from DBPro.

In terms of speed I'm not sure what effect using LUA has but I've noticed no speed difference to when I don't use LUA.
Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 12th May 2013 10:29 Edited at: 9th Mar 2014 04:53
DBpro (from memory) default behavior is to pass into functions by value. So any alterations make inside the function are acting upon a different copy of the structure.

There has been couple of OO pre processor projects for DBpro in past, DOOP (or something like that). Certainly worth a bit of search if you're interested.

Andrew_Neale
16
Years of Service
User Offline
Joined: 3rd Nov 2009
Location: The Normandy SR-2
Posted: 12th May 2013 13:29 Edited at: 12th May 2013 18:50
Obviously this isn't OOP at all but, down a similar route to your original example, I quickly knocked this together.



It creates a global dynamically sized array for instances of the class which you never access directly but add to via a class constructor which then returns a "pointer" for you to use in future. It also creates a second dynamic array which is just a collection of pointers to instances. This means that you can dispose of instances, which can be nice for memory usage with large numbers of large classes, without breaking any of your pointers.

To try and explain a bit better:

Global Player As Integer : Player = Person_New()
This creates a new instance of the "person" class and creates a pointer to it in the lookup collection and returns the index of the pointer. "Player" is now the reference you will use for all interactions with the class instance. The constructor also ensures the arrays are initialised the first time it is called.

Person_SetAge(Player, 12)
This takes the index that was created with the constructor, finds the pointer to the actual instance and then sets the "age" property on the instance to "12".

Person_Dispose(Player)
This takes the index that was created with the constructor, finds the pointer to the actual instance and then deletes the instance of the class. It resets the pointer in the lookup array and also cleans up the lookup array as much as possible by deleting any reset pointers at the end of the collection so as not to affect any still active indices.

Person_DisposeAll()
This clears out both the instances and their pointers entirely which would be useful for when the application state changes drastically and you need a clean start so you won't end up carrying global variables around needlessly. Whilst it isn't a huge amount, any memory you can claim back from the 2Gb limit is worth having!

Hope this at least slightly makes sense and might be of use to someone. Only tested it very briefly and it seems to work fine, but let me know if you need any help with it.

[Edit]Changing language on code snippet for syntax highlighting and a couple of typos in remarks.[/Edit]


Previously TEH_CODERER.
Mage
Valued Member
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location:
Posted: 12th May 2013 16:59
Quote: "Obviously this isn't OOP at all but, down a similar route to your original example, I quickly knocked this together."


Yeah, using good OOP inspired naming conventions for global variables and functions can really help make the code readable.

Andrew_Neale
16
Years of Service
User Offline
Joined: 3rd Nov 2009
Location: The Normandy SR-2
Posted: 12th May 2013 19:08
Quote: "Yeah, using good OOP inspired naming conventions for global variables and functions can really help make the code readable."


The naming conventions make a huge difference, but the other part of the code I provided was the ability to pass custom types by reference rather than value both to and from functions more easily via a pointer-esque system. I have gone a step further with some of my own tests and dropped UDTs entirely in favour of making my own class system and managing the memory for them entirely manually. With that system I can use commands like the following:

New("Player", CLASS_PERSON)
Creates an instance of the "person" class (from a set of constants linked to my custom class definitions) under the variable name "Player".

Set("Player.Age", "12")
Sets the value of the "Age" property of the variable named "Player" to "12".

Value = Get("Player.Age")
Gets the value of the "Age" property of the variable named "Player".

Value = Call("Player.Print", "")
Calls the "Print" method of the variable named "Player" optionally passing in comma delimited parameters. Globally a variable called "This" becomes available within the method, which is part of the class definition, which is basically a pointer to the instance of the class, in this case "Player", that called the class method.

Admittedly this was probably overkill and there is a little overhead from parsing the strings passed into the methods but it is still as nippy as running a script, for example. It is also very memory efficient as I manage it directly so that each property uses only the memory it needs and instance can be created and dropped at will rather than being sat in memory for the entire duration of the application as DBPro variables normally would be. I also managed to cater for arrays within arrays (i.e. Player.Weapons[0].Attachments[0] to get the first attachment for the first of the player's weapons). It was really more of an experiment/practice though. I'm far more at home in C++ or C# but DBPro is fantastic for quick prototypes so anything I can do to make development in DBPro more similar to the C style languages...


Previously TEH_CODERER.
Mage
Valued Member
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location:
Posted: 12th May 2013 21:26 Edited at: 12th May 2013 21:27
Yeah I was and am on my mobile so I didn't get a good look at it. It's a good idea with some big advantages.

I know you just threw up a rough example, but I'd suggest you make a player.exists() function. You'd be able to turn a lot of your functions into one liners that way. Readability being the subject and all.

Thanks for the example.

Andrew_Neale
16
Years of Service
User Offline
Joined: 3rd Nov 2009
Location: The Normandy SR-2
Posted: 12th May 2013 21:33
Quote: "I know you just threw up a rough example, but I'd suggest you make a player.exists() function. You'd be able to turn a lot of your functions into one liners that way. Readability being the subject and all."


Yeh, well...I was just testing you. Haha, no, you're quite right. It would've saved me time writing up the example as well but, as you say, it was a bit of an oversight in a quick example.

Quote: "Thanks for the example."


No worries. Hopefully it will prove useful to someone or at least be something to think about.


Previously TEH_CODERER.
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 12th May 2013 22:45
Quote: "DBpro (from memory) default behavior is to pass into functions by value. So anything alterations make inside the function are acting upon a different copy of the structure."


Right. And while I just learned that AppGameKit does in fact let you return UDTs now(i haven't tested personally yet), DBP does not.


Quote: "There has been couple of OO pre processor projects for DBpro in past, DOOP (or something like that)."

Coincidentally, I just looked this up in response to another post I read.
http://forum.thegamecreators.com/?m=forum_view&t=100985&b=8


Quote: "Yeah, using good OOP inspired naming conventions for global variables and functions can really help make the code readable."

I use all caps for constants and globals are prefixed with an underscore.

"You're all wrong. You're all idiots." ~Fluffy Rabbit
Mage
Valued Member
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location:
Posted: 12th May 2013 23:54
Quote: "I use all caps for constants and globals are prefixed with an underscore."


Yes and to add to this, OOP inspired grouping / factoring.
Related functions following a grouped naming convention.

Player.Add()
Player.Exist()
Player.Delete()

_player.name
_player.race
_player.x
_player.y

SERVER_MAXPLAYERS
SERVER_IP
SERVER_VERSION

Quote: "Right. And while I just learned that AppGameKit does in fact let you return UDTs now(i haven't tested personally yet), DBP does not."

Would be nice if it did.

Login to post a reply

Server time is: 2026-07-07 02:17:46
Your offset time is: 2026-07-07 02:17:46