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 AppGameKit Corner / Type Array questions

Author
Message
Golelorn
8
Years of Service
User Offline
Joined: 20th Nov 2016
Location:
Posted: 29th Jan 2018 23:14 Edited at: 29th Jan 2018 23:57
How can I see if the exact type I have in an array matches another?


For instance, say I have:

type star
A
B
C
D
E
endtype

Inside an array of SKY

What if I change C? Technically, its different, and so I want to insert it into SKY( I wouldn't want to if its the same ).

Would this work?

Br as star
for i...
if SKY[i] <> Br then SKY.insert(Br)
next i

Is it that simple?


This does not work. Can't use = or <> on a type. So, looks like I have to check each type definition individually?

Arrays have been the bane and blessing of my short coding hobby. Thanks, coding dudes.

PS. Is there a way to pass a type into a function?
edit: Yes. https://www.appgamekit.com/documentation/guides/types_004.htm

----
So I ended up doing this:

Has not broken anything... Please let me know if you have suggestions. I know most of you out there got this sh*t figured out a lot better than me.
GarBenjamin
AGK Developer
7
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 30th Jan 2018 02:07 Edited at: 30th Jan 2018 02:18
Yeah it seems there is no equality comparison functionality provided for types.

This might actually be a great thing to submit as a request to add. Because I think it would be much faster execution for them to implement internally.
Ideally we could just write

or

and in both cases they would check every field (or possibly a hash) inside these instances of these types (the same of course).

However, they are not implemented (yet!) so you are left with basically two options... the first is the brute force way of checking every field as you mention above.

The second option is to add one more field to your type definition named hash or checksum or something along those lines.

Every time a field value changes this hash must be regenerated. So that does add a bit of overhead. However, it pays off when it comes time to compare if two instances of a type are equal.

Here I have used a string variable for the hash but you could also use an integer value.



NOTE: That code is not meant to be perfect production ready. It is just to illustrate the concept of hash comparison vs field level comparison.
TI/994a (BASIC) -> C64 (BASIC/PASCAL/ASM/Others) -> Amiga (AMOS/BLITZ/ASM/C/Gamesmith) -> DOS (C/C++/Allegro) -> Windows (C++/C#/Monkey X/GL Basic/Unity/Others)
Ortu
DBPro Master
17
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 30th Jan 2018 12:58 Edited at: 30th Jan 2018 13:06
You could maybe use toJSON to convert to strings and compare that way. I know toJSON works on type arrays and type variables, I'm not sure if it works on just a specific index though.

If myArray[i].toJSON() = myTest.toJSON()

If not, you could just set a temporary variable = the index, then stringify that

Check as my type
Check = myArray[i]
If Check.toJSON() = myTest.toJSON()
http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 30th Jan 2018 15:28
Quote: "Can't use = or <> on a type. So, looks like I have to check each type definition individually?"


Because we cannot overload a comparison operator on a type and every type can be customized, the language wouldn't know what to compare to properly solve the equation. To compare, you'd have to write your own function, which could be difficult without already know what type you're passing to it. Which btw, yes you can pass types just as you would any generic data type. You can even return types. And I'm pretty sure you can pass types by reference as well, I think using "ref as" instead of just "as". (not 100% on the command name there)




Honestly, I see no need for a typeof (instanceof, whatever) command in AGK. If you do, then I think your coding structure needs adjusted. In other languages, you can leave the type open by passing to a function just a generic Object. Or some other object class that has other subclasses that can all belong to it. AppGameKit can't do this, therefore there should never be any mix. You would always know the type being passed.

If Sky is a type and star is a type, you can't have an array defined as Sky and fill it with Stars. If AppGameKit allows this I'd say it's a flaw.
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Golelorn
8
Years of Service
User Offline
Joined: 20th Nov 2016
Location:
Posted: 30th Jan 2018 19:05
Thanks, all. Some very valuable knowledge.

Login to post a reply

Server time is: 2024-11-23 00:47:29
Your offset time is: 2024-11-23 00:47:29