Need help with types

Blitz3D Forums/Blitz3D Beginners Area/Need help with types

Chi3f Stadi(Posted 2004) [#1]
Hello,

i got stuck in a type problem. It would be great if somebody can help.

Description:
I create some NPC's (Bot's) and some of the NPC's get a Type called Effect assigned during Gameplay.

But i'm not sure if i did it correctly. The type Effect is an child of the type NPC. And there can be more than one Effect types for a NPC.

I think this part should be ok ! Some lines are ommited ...
Type NPC
Field entity
Field model
...
Field his_effect.Effect
End Type

Type Effect
Field entity
Field model
...
End Type

...

Function CreateBot()
this.NPC = New NPC
this\model = CopyEntity(..
...
End Function

Function CreateEffect(this.NPC)
If this<>Null
For i = 1 To 6
this.NPC\his_effect.Effect = New Effect
this.NPC\his_effect\entity = CopyEntity(sprite)
...
Next
End If
End Function

And here is where i got stuck.
Function UpdateNPC()
For this.NPC = Each NPC
If this\his_effect <> Null
ScaleSprite this\his_effect\entity,0.1,0.1
TurnEntity this\his_effect\entity2,90,0,0
...
Next
Next
End Function

With this method i can just iterating through the first type Effect in type NPC but not though all, so:
- How can i iterate though all of them.

I tried also this\his_effect = After this\his_effect but this goes to the next NPC handle.

best rgds
Karel


semar(Posted 2004) [#2]

With this method i can just iterating through the first type Effect in type NPC but not though all, so:
- How can i iterate though all of them.



The effect type structure can be looped as any other type structure, so if you want to go through all the effects, you just need:

for temp.effect = each effect
.
.
next


Anyway, I would do this in a slight different manner.

Since type collections can't be located in different sections, that is, each type element will belong to the same element collection, I would add in the NPC type structure an ID field.

The same ID field should be added in the effect type structure, and set it to the NPC one used to create the effect for.

In other words:
[CODE]
type t_npc
field NPC_ID
.
.
end type

type t_effect
field NPC_ID
.
.
end type

NPC.t_npc = new t_npc
npc\npc_id = NPC ;here is where the id is set
.
.
;let's add an effect fot the just created npc
effect.t_effect = new t_effect

;let's assign the same npc id to the effect
effect\NPC_ID = NPC
.
.


;Now, if you want to go through all the effect that belong to an NPC:

for npc.t_npc = each t_npc ;go through all NPC
for effect.t_effect = each t_effect ;go through all effects
if effect\NPC_ID = npc\NPC_ID then ;peek only the ones that belong to the current NPC
;...and do something here
endif
next
next
[/CODE]

Hope this has sense for you,
Sergio.


Chi3f Stadi(Posted 2004) [#3]
Thanks semar. Thats my point !

Imagine, if the Bot get's killed, the effect is still iterated if i do it like you suggested. This would look very strange.

I want to iterate all effects from the parent NPC. How do i solve that.


semar(Posted 2004) [#4]
I've edited my previous post, and added some other concept.
It may help..


semar(Posted 2004) [#5]
As already said, when you destroy a bot, before you perform the delete command on the npc, then delete all the associated effects:
delete_effect_that_belong_to_this_npc_ID(NPC_ID)
delete NPC
.
.
function delete_effect_that_belong_to_this_npc_ID(NPC_ID)
for effect.t_effect = each t_effect
if effect\NPC_ID = NPC_ID then
delete effect
endif
next
end function


Did it help ?

Sergio.


Chi3f Stadi(Posted 2004) [#6]
Thank you semar. That's another solution which i considered in the beginning. But it's not answering my question. Sorry.

What i don't like is that i always have to remember that a NPC may has an effect while deleting !

delete_effect_that_belong_to_this_npc_ID(NPC_ID)

May it has also another type associated. So i have to keep all those things in mind which doesn't make me very happy.

It would be nice to just delete the parent and this is deleting all dependend types as well. This is working with the codesnippet on the top.

My problem is still how to iterate through all the "child" types ?

rgds
Karel