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.

AppGameKit Classic Chat / Array.find() does not work, how I expected, can someone explain please?

Author
Message
Xaby
FPSC Reloaded TGC Backer
12
Years of Service
User Offline
Joined: 17th Apr 2007
Location: Berlin
Posted: 13th Jun 2019 08:44 Edited at: 13th Jun 2019 09:06
I wanted to make a game controller thingly

Global FenBtnStr as String[12]=["UP","DP","LP","RP","XB","YB","AB","BB","ST","SE","LB","RB"]



I thought, I would get the numbers 0..11 from Array.find(), but the first values are -1
I am searching if the value of an array field is somewhere in any field of the array. I don't understand how that can ever be "untrue"

I changed my array and it works. But that means, that Array.find() only searches in sorted arrays. But sometimes, you can't sort the array, because you would change there meanings.
Global FenBtnStr as String[12]=["AB","BB","DP","LB","LP","RB","RP","SE","ST","UP","XB","YB"]

What if I have a list of names and it's ... I guess, I have to have Types, and somehow do there the magic and maybe save the ranking sort the list, find something, and after that, I could re-sort the entries with the Type-ID or something like that.
Bengismo
1
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 13th Jun 2019 09:17 Edited at: 13th Jun 2019 09:18
As you have now noticed, the find command only works on sorted arrays. (its because it internally uses a fast binary search)

https://www.appgamekit.com/documentation/guides/12_array_changes.htm
The help rference explains that

If you dont want to sort your array then you could write a simple function to do the find the item using a linear search instead


This ^^ allows you to get the index of the array entry that is the same as the string you pass to it.

Its only 5 lines of code and works fast when only using your 10 entries
fubarpk
Moderator
14
Years of Service
User Offline
Joined: 11th Jan 2005
Location: Adelaide
Posted: 13th Jun 2019 09:24
with numbers



with strings
fubarpk
fubarpk on Itch...………...https://fubarpk.itch.io/
fubarpk on googleplay..https://play.google.com/store/apps/developer?id=fubarpk
Xaby
FPSC Reloaded TGC Backer
12
Years of Service
User Offline
Joined: 17th Apr 2007
Location: Berlin
Posted: 13th Jun 2019 09:33
@Bengismo

I used the App Game Kit Studio Help: Guides > Arrays > Sorting and searching ... arrays_006.htm
I guess, there was no clue about that

Is there a "min(value1, value2)" or "max(value1, value2)" function in AppGameKit?
Bengismo
1
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 13th Jun 2019 09:46
AGK documentation wrote: ".find(item) will only work on arrays that are in ascending order. "


And its pretty easy to write min and max functions

function Min(a,b)
if a<b then exitfunction a
endfunction b

function Max(a,b)
if a>b then exitfunction a
endfunction b

Xaby
FPSC Reloaded TGC Backer
12
Years of Service
User Offline
Joined: 17th Apr 2007
Location: Berlin
Posted: 13th Jun 2019 09:56
@Bengismo thx

will it work with float also, or do I have to declare that the values could be float?
Bengismo
1
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 13th Jun 2019 10:04 Edited at: 13th Jun 2019 10:06
if you declare the passed variables as floats then it will work with floats....like any function

function Minf(a as float,b as float)
if a<b then exitfunction a
endfunction b

function Maxf(a as float,b as float)
if a>b then exitfunction a
endfunction b

if you dont define a type and option_explicit isnt set then the variable defaults to an integer.
Xaby
FPSC Reloaded TGC Backer
12
Years of Service
User Offline
Joined: 17th Apr 2007
Location: Berlin
Posted: 13th Jun 2019 10:07
yeah, I am asking, because a friend of mine ask me, why AppGameKit had no such functions. He came from C# background, and was thinking about some more optimisation. AppGameKit could compile it for ARM or x86 CPUs maybe better than if it is our own function.
Bengismo
1
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 13th Jun 2019 10:17 Edited at: 13th Jun 2019 10:19
a friend asked?? hmmm...ok

There is so much overhead in running interpreted basic that an inbuilt function probably wouldn't have a significant increase at all. Just mapping bytecode to the correct command takes waaaay longer then the actual compare of the two values. (I just added 2000 max operations to a program im working on right now and it didnt even touch the framerate) I'm not sure how many programs anyone has written that are slowed down because of all the min and max calculations.

Best thing to do is probably to put a request on github for the functions if you feel they are necessary and they might get added.

if you know a it of C then you could add them yourself and make a pull request on github for the next release.
Xaby
FPSC Reloaded TGC Backer
12
Years of Service
User Offline
Joined: 17th Apr 2007
Location: Berlin
Posted: 13th Jun 2019 14:12
@Bengismo

I not realy thought about it before. But missed the math-function also. And we have bitshifts and other functions. I also guess, that in my game that would have no difference. It is more the question, if this could impact, if you would have some heavy calculations in simulation or whatever every frame.
Particles, mesh-sorting, scaling objects or what ever.

I also guessed, that App Game Kit Tier 1 Basic would be compiled. I also guess, that he wanted to convince me to use Unity with C#, because I am writing my "own" 3D placement editor and have no fancy effects and so on. But I don't know, if Unity would do any good. Maybe you wouldn't have some hick hack here, but get some hick hack there and for that, it don't realy matter, I guess.

I love the compile time and in AppGameKit Studio there are some things I don't want to miss in the future

I am also targeting the Raspberry Pi, so I don't know, if I can compare the performance on a multi-core x86 without limits to the target platform I have in mind but we will see.

Login to post a reply

Server time is: 2019-08-23 00:22:29
Your offset time is: 2019-08-23 00:22:29