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.

Newcomers DBPro Corner / [Tutorial][DBPro][Beginner] - Types / UDTs

Author
Message
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 15th Jul 2007 22:41 Edited at: 15th Jul 2007 23:08
Hola all, welcome to my newest tutorial, since I noticed there weren't any on this subject, and I think it is important that people learn, because without it you will all become miserable failures and end up in the gutter

So, let's get started shall we?

What is a type?

A type is a type of... ok, bad start... A type, is like an integer, string, or whatever - well, you use it similarly.
For example, you may want to use a type to.. oh I dunno, make a person, a game person, not a real one. BUT, a person must have a name, a weight, a height, a hair color (or more than just one hair colour, we will get to that (or more like, get to why that is a bit hard in DBP)). Types are useful for this, and especially with arrays.

Some of you may be suprised to find that to make a type in DBP, you type (no pun) this:


and then:


There is some more stuff inbetween those though. Not much point in just having those lines. This is where the next section starts.


How does one make a type?

Well, I already explained a few things, yeah? A type is just like integer, or string, or float, or double, or dword, or byte, or something. You may notice, that all those types have names, i.e. integer, string, float, double, dword, byte, etc. Well, our types have to have names too, so, that will be the first thing to go between the "type" and the "endtype" (well, more specifically, after the "type").
Let's make a type about a doughnut as an example:


Cool. Now we can use this like any other type, i.e.

(Notepad really sucks for writing tags)

Now, what we do here is, create a doughnut, called "mydoughnut", of type "doughnut", yeah? Awesome.

Ok, so what information can we give about a doughnut? Well, it's size (as in, radius), it's thickness, it's colour, it's topping, w/e. These things will be the next things we add in:


Awesome, now, let's make ourselfs a doughnut:


Do you understand? If you dont, back to the top! Read it again and again, and you will get it.


Now! If you were clever like me, you would have noticed that all the doughnut information ALSO has types, i.e. "size as integer". What does that tell you? Yup! You can use other types, in types, in types, in types, as much as you like.


Trees (not the wooden ones)

But if you were to draw them, they would look like upside-down trees, unless you were to draw them upside-down, in which case they would look like normal trees.

Let's make a new type as an example. This will be a topping for our doughnuts:


OUr topping has a few pieces of data. The ingredient. I like chocolate. Whether or not is has hundreds and thousands. I vote no, but that's my taste, and also, how much.
What is we were to substitute the old topping from the doughnut type with this? Let us try:


Now we can customize the topping at the same time as the doughnut. Joy to the world.


And that, is one tasty doughnut.


But seriously, how would I really use a type?

That doughnut thing was serious enough! What if you wanted.. a doughnut game.. ?

Well, here's one for ya, try to figure this one out yourself:


Another very useful thing you can do is combine them with arrays:


Now you have 10 players! Each sharing the same 'type' of data.

Speaking of types, and arrays, you can't have arrays inside types. It doesn't work, not in DBP, no sir, no way jóse. But there are workarounds that do work. Might suck a little, but nevermind.

Method 1)

Lets say you had a type for particle emitters, and inside it, you wanted to store all the objects it used. Well, check out method one:


I got this idea from pointers when I started learning C++, which btw is a much more advanced language than DBP, but let's forget I ever said that.
A pointer in C++ can be used to represent a string (for example). It will point to the location in memory of the first letter. Well, this points to the first object used by the particle emitter, and also how many object it uses. This will only work if all the objects are numbered sequentially, TG for FreeObject() functions.

Method 2)

Let's use the same idea, a particle emitter. With this, you can use a lookup table. An array. The array could just be a big array of integers to which all the objects are added. Look into the array commands (especially array insert at bottom), to work this properly.

So lets say the array looked like this:


These would be all the objects created in the scene. You can use exactly the same way as the previous method, but, you guessed it, point to a location in the array!


Method 3)

For this, you would have to make a type for each particle. It sucks, I know, but I will tell you anyway, because this way you dont have to have "from A to B" like in the last two examples. I will explain.

This way, you can have an array of objects, just like before, but, you might only want array elements 2,3 and 5. How do you work that the same way as last time? The truth is, you can't. But do not weep, because I will show you how to do it this way.

Each particle shares a particle type, right? That particle would have data in it such as the object number, and the emitter it uses. So, like this:


Now, when you create an emitter, and you want to see what objects you need, find then like this:


Simple as pi.


And that, is my two cents on types. PLEASE! STOP ASKING QUESTIONS THAT INVOLVE THIS SIMPLE STUFF! LERN!!!1 LERN!!11! (Learn about arrays too if you dont already know)

Bye then.

"It's like floating a boat on a liquid that I don't know, but I'm quite happy to drink it if I'm thirsty enough" - Me being a good programmer but sucking at computers
Xenocythe
18
Years of Service
User Offline
Joined: 26th May 2005
Location: You Essay.
Posted: 15th Jul 2007 23:42
Nice tutorial, mind if I add it to darkdev?

Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 15th Jul 2007 23:55
No problem

"It's like floating a boat on a liquid that I don't know, but I'm quite happy to drink it if I'm thirsty enough" - Me being a good programmer but sucking at computers
Xenocythe
18
Years of Service
User Offline
Joined: 26th May 2005
Location: You Essay.
Xsnip3rX
17
Years of Service
User Offline
Joined: 20th Feb 2007
Location: Washington State
Posted: 16th Jul 2007 03:31
I think this should be stickied, Also, Admins, a suggestion if i may, Make a special Sub-Forum for DarkBasic Pro/Classic Products for tutorials only?

Code Dragon
17
Years of Service
User Offline
Joined: 21st Aug 2006
Location: Everywhere
Posted: 16th Jul 2007 04:26
Quote: "Make a special Sub-Forum for DarkBasic Pro/Classic Products for tutorials only?"


I think that would be a good idea.

Great tut by the way.

This sig has been viewed times.
Sixty Squares
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 16th Jul 2007 04:32 Edited at: 16th Jul 2007 04:32
Quote: "Make a special Sub-Forum for DarkBasic Pro/Classic Products for tutorials only?"


There'd have to be a way to stop it from getting flooded with poor tutorials though... Say someone wrote a tutorial with the wrong info and put it there. People new to DB would go read those and get bad info

Anyway, the tutorial is great, I like the playful nature of how it's written

@Code Dragon: Nice signature

Kieran
17
Years of Service
User Offline
Joined: 6th Aug 2006
Location: Hamilton, New Zealand
Posted: 16th Jul 2007 04:32
Really nice tutorial Zotoaster...It really explains well.

djchaos
17
Years of Service
User Offline
Joined: 26th Mar 2007
Location: Brighton, UK
Posted: 16th Jul 2007 05:44
yes i strongly agree, i think there should be a tutorials section & all the best ones get stickied and the crap ones get rejected or get locked or something
vibe runner
17
Years of Service
User Offline
Joined: 7th Aug 2006
Location: The Future
Posted: 16th Jul 2007 19:44
This is a good start. Some thoughts:

1. Many, many more examples using arrays. You mention them briefly. In my view array-use UDTs should be the majority of the tutorial.

2. You have not demonstrated Types within Types! (Or did I miss it?) Again, this is a major strength of DPB.

3. I STRONGLY agree about the use of Tutorial Forum.
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 16th Jul 2007 20:01
Quote: "1. Many, many more examples using arrays. You mention them briefly. In my view array-use UDTs should be the majority of the tutorial."


Well, I gave one or two examples.. it's a tutorial, not a codebase

Quote: "2. You have not demonstrated Types within Types! (Or did I miss it?) Again, this is a major strength of DPB."


Look at the Trees section.


Thanks for all the comments

"It's like floating a boat on a liquid that I don't know, but I'm quite happy to drink it if I'm thirsty enough" - Me being a good programmer but sucking at computers
vibe runner
17
Years of Service
User Offline
Joined: 7th Aug 2006
Location: The Future
Posted: 16th Jul 2007 20:17
I think working examples rather than abstract examples are vastly more useful. I would recommend a very short program using a "useful" array of a type that included a type within it. Tasty food toppings is not as useful as a real-code example.

In particular putting XY or XZY type inside an entity type and put an array of those entities on the screen.

But a great start.
Sixty Squares
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 17th Jul 2007 01:03
@vibe runner: How about a character list example?



Crazy Programmer
AGK Developer
19
Years of Service
User Offline
Joined: 6th Sep 2004
Location: Lost in AGK
Posted: 17th Jul 2007 04:40 Edited at: 17th Jul 2007 04:41
Nice little tut Zo. Keep up the good work.
Thats just funny it should be in a sig.


Load Programmer "Crazy Programmer",1
Xsnip3rX
17
Years of Service
User Offline
Joined: 20th Feb 2007
Location: Washington State
Posted: 17th Jul 2007 05:50
@ Sixty Squares


where's mine?

FERSIS
17
Years of Service
User Offline
Joined: 17th May 2006
Location:
Posted: 27th Jul 2007 03:34 Edited at: 27th Jul 2007 03:41
The only thing the tutorial need is the saving UDT to files and loading part
cheers
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 27th Jul 2007 15:21
Well, I think that's more about file reading that UDTs. The only difference here is that instead of reading the data into variables or arrays, you read it into a UDT element.

"It's like floating a boat on a liquid that I don't know, but I'm quite happy to drink it if I'm thirsty enough" - Me being a good programmer but sucking at computers
vorconan
17
Years of Service
User Offline
Joined: 4th Nov 2006
Location: Wales
Posted: 28th Jul 2007 01:48
Very nice tutorial, helped me a lot. Can I just ask though, how could types be applied to weapons? I can't seem to grasp how you can edit a weapon's properties itself. Sorry if this is a too in-depth question.

Thanks


Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 28th Jul 2007 13:53
Not a problem.

Weapons each have data such as ammo, fire power, recoil, and even things like object numbers etc.

A basic weapon might look like this:


Then you would make a list of weapons using an array:


Fill in the data, and then use whichever weapon you like

"It's like floating a boat on a liquid that I don't know, but I'm quite happy to drink it if I'm thirsty enough" - Me being a good programmer but sucking at computers
vorconan
17
Years of Service
User Offline
Joined: 4th Nov 2006
Location: Wales
Posted: 28th Jul 2007 19:09
Thanks a lot, simple now I know it.


Kyle Katarn
16
Years of Service
User Offline
Joined: 7th Jun 2007
Location: Central Coast, Australia
Posted: 30th Jul 2007 14:24
Interesting, can I just verify something to see if I've understood it at all?

Say you were making an RPG about say... DragonBall Z. With Types, you could set say Goku's HP and KI to it and read from it to get an output for a HUD (Read as a variable)?
Roxas
18
Years of Service
User Offline
Joined: 11th Nov 2005
Location: http://forum.thegamecreators.com
Posted: 30th Jul 2007 23:40 Edited at: 30th Jul 2007 23:41
This should do it

(Not tested)



Edit:

Oops.. I readed wrong.. And writed example lol..

Well you can do that with types

[center]
DarkBIBLE Wikipedia for TGC Users!
indi
21
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 31st Jul 2007 05:52 Edited at: 31st Jul 2007 06:34
here is another example using types.
we are managing elements of the interface.


Sixty Squares
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 5th Aug 2007 19:31 Edited at: 5th Aug 2007 19:31
I have a question. Which is faster:

1. Using lots of UDTs and having arrays for each kind





2. Using lots of multi-dimensional arrays instead.




Imagine if I had 50 times as many different things in my type. Which would be faster then?

Login to post a reply

Server time is: 2024-04-25 15:06:29
Your offset time is: 2024-04-25 15:06:29