Help with accessing multiple types from entities?

Blitz3D Forums/Blitz3D Beginners Area/Help with accessing multiple types from entities?

Irvau(Posted 2015) [#1]
Alright, I think that the title explains the situation somewhat clearly.
I need to be able to get the type instance from an entity.

I've looked at this post, and what it recommends seems like a great idea. However, there's one problem: the method only stores the pointer index. That *would* work were we only referring to one type, but I have multiple types I'm managing. So not only am I having to record the index with EntityName(), but also the "type of type". From what I know, that's not possible.

So, how would I go about being able to select an entity and get it's type AND index value?

Thanks in advance!


Yasha(Posted 2015) [#2]
The index returned by Handle is unique and will not be shared with objects of another type, nor will it be reused if you delete the object and then Handle another one (I don't know what happens if you leave your program running long enough to exhaust all possible indices, probably Blitz3D breaks).

So you can test what type of object a handle indirectly references by trying to convert it and seeing what happens:

;given h = Handle(something)

Select True
    Case Object.Foo(h) <> Null
        ;it's a Foo
    Case Object.Bar(h) <> Null
        ;it's a Bar
    Case Object.Baz(h) <> Null
        ;it's a Baz
    Default
        ;it isn't any of those, and might not refer to an object that (still) exists
End Select



Aside from that, you can record whatever you want in EntityName - it's a string, so there's no upper limit on the length, e.g.:

NameEntity e, Handle(associatedObject1) + "|" + Handle(associatedObject2) + "|" + nameInTable    ;...etc


You can add as many values together as you like, separated with some separator character, and split the name again later when you want to extract its values. There are various other ways to encode aggregates as strings as well (check the code archives for a bunch of "string array" implementations).

A different option might be to store a reference to a single metaobject containing direct pointers to as many actual associated objects as you want.


Stevie G(Posted 2015) [#3]
Why do you need 2 different types - are the object so vastly different in their attributes?

The only way you can obtain the type that the pointer relates to is by doing something along these lines ...

Pointer = entityname( ThisMesh )
if object.type1( pointer ) <> null
    'Do stuff assuming type 1
else
    if object.type2( pointer ) <> null
        'Do stuff assuming type 2
    else
        'Do stuff assuming type 3
    endif
Endif


Unfortunately, you can't use objects in a select statement as it doesn't recognise NULL for some reason.

[EDIT] Actually you can ... as demonstrated above :)


Guy Fawkes(Posted 2015) [#4]
FIX -- NON-BlitzMax version:



Also, please note: It is better to use JUST "If Object.Type2( pointer ) and ELIMINATE Null altogether, because this is saying: "IF AND ONLY IF Object.Type2( pointer ) is NOT null, then..." It already assumes that Object.Type2( pointer ) HAS to be true before anything else happens.


RemiD(Posted 2015) [#5]
Here is another way : http://www.blitzbasic.com/codearcs/codearcs.php?code=3095

Search for "NameEntity" and then for "EntityName", and you will see.


Yasha(Posted 2015) [#6]
@Guy Fawkes, that is in no way a "fix". Firstly neither I nor Stevie posted any BlitzMax, and secondly `If` doesn't work with objects in Blitz3D.

Comparing against Null is mandatory in a B3D conditional; your example is totally invalid and won't even compile. You can't just make up rules that the language doesn't know about!


Guy Fawkes(Posted 2015) [#7]
Well SCUSE ME for trying to help, ya jerk!


Irvau(Posted 2015) [#8]
XD

Thanks guys!

[EDIT:]
Umm, ok. So I'm trying out Stevie G's method, which is great and works. However, now I need help figuring out how to refer that particular object. I need to be able to change one of the object's variables from within the if statement.

Help once again?


Guy Fawkes(Posted 2015) [#9]
I'm not answering anymore. Cuz' alls I get is CRAP from some un-named people. YOU know who you are!

Sorry, good luck with your project...


Stevie G(Posted 2015) [#10]
Assuming this is your original type setup

Type type1
	Field Life
	Field Blah
End Type

Type type2
	Field Name$
	Field Age
End Type


Do something like this ...

Pointer = EntityName( ThisMesh )

t1.type1 = Object.type1( pointer )
t2.type2 = Object.type2( pointer )

If t1 <> Null
	t1\life = t1\life - 1
	t1\blah = 150
Else
    If t2 <> Null
        t2\name = "StevieG" 
	t2\Age = t2\Age + 1
    EndIf
EndIf



Stevie G(Posted 2015) [#11]
@ Guy Fawkes. Do you honestly think that posting something which doesn't actually work is helpful?


Irvau(Posted 2015) [#12]
Thanks once again!

Oh, and, @Stevie G:
Well, sure, it may not have helped, but at least he tried.

Just saying. :)


Guy Fawkes(Posted 2015) [#13]
Forget YOU, jerk! At least I tried, which is more than I can say about some of you losers on here who think they know it all and think they can just trample all over people! I'm DONE! I'm outta' this thread!