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:
type doughnut
size as integer
thickness as integer
colour as dword
topping as string
endtype
Awesome, now, let's make ourselfs a doughnut:
mydoughnut as doughnut
mydoughnut.size = 10000
mydoughnut.thickness = 30
mydoughnut.colour = rgb(0, 0, 255)
mydoughnut.topping = "More dougnuts"
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:
type dtopping
ingredient as string
hundredsandthousands as boolean
amount as float
endtype
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:
type doughnut
size as integer
thickness as integer
colour as dword
topping as dtopping
endtype
Now we can customize the topping at the same time as the doughnut. Joy to the world.
mydoughnut as doughnut
mydoughnut.size = 10000
mydoughnut.thicknes = 30 `miles
mydoughnut.colour = rgb(255, 0, 255)
mydoughnut.topping.ingredient = "Chocolate" `Woo!!
mudoughtnut.topping.hundredsandthousands = 0
mydoughnut.topping.amount = 100 `kilos
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:
type tWeapon
name as string
power as integer
magic as integer
endtype
type tPlayer
name as string
height as float
weight as float
weapon as tWeapin
endtype
`I'll use the real me for this:
me as tPlayer
me.name = "Zotoaster" `No second name
me.height = 9.2
me.weight = 0.0
me.weapon.name = "Frying pan"
me.weapon.power = 100
me.weapon.power = -2
Another very useful thing you can do is combine them with arrays:
dim players(10) as tPlayer
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:
type tEmitter
name as string
object_start as integer
object_count as integer
endtype
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:
`data in my array, from 1 to 5 (0 to 4 actually)
5
2
7
4
12
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:
type tParticle
obj as integer
emitter as integer
endtype
Now, when you create an emitter, and you want to see what objects you need, find then like this:
for p=1 to emitter.Particle_Count `Made that up just there
`Check if object is part of this emitter
if particle(p).emitter = 0 `You need to make a particle() array btw
`Do something
endif
next p
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