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 / Pass into a function that takes any types

Author
Message
Richard_6
8
Years of Service
User Offline
Joined: 3rd Feb 2017
Location:
Posted: 3rd Apr 2017 02:39 Edited at: 3rd Apr 2017 02:46
I’m trying to reproduce something I found on the documentation:



I want to create a single function that might works for different types, not using global variables.
Here is what I’m trying. I’m sure I’m doing something wrong and I appreciate any help.

Jack
20
Years of Service
User Offline
Joined: 4th Oct 2004
Location: [Germany]
Posted: 3rd Apr 2017 03:04 Edited at: 3rd Apr 2017 03:50
Quote: "MyPartCards as PartCards[0]"

Creates An array with the real length of 1.
Use MyPartCards as PartCards[] or MyPartCards as PartCards[-1] to create an empty array.




MyPartCards.length=MyPartCards.length+1
MyPartCards[0].LifeCycle=16

I recommend you the following, if you get multiple entries:


MyPartCards.length=MyPartCards.length+1
pos = MyPartCards.length
MyPartCards[ pos ].LifeCycle=16

The TestTypeToArray function does not work.
I attach you a code, that may help:

Once you call FnTestArraysLoop, you copy your MyPartCards array into the PartCardList array, that is only inside this function. You can't call another function to refer on this array again.



[/url]
Richard_6
8
Years of Service
User Offline
Joined: 3rd Feb 2017
Location:
Posted: 3rd Apr 2017 04:48
Thanks Jack, but MyPartsCards is not global (and I want to avoid globals here) and I don't want the function to works specifically for this type.
blink0k
Moderator
12
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 3rd Apr 2017 05:03
You cannot have a function that accepts a variable type as a parameter
Markus
Valued Member
21
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 3rd Apr 2017 07:26
Quote: "I want to create a single function that might works for different types"

why you need this? example of use?
one thought is to use one Type with Struct of different Types. means Type in Type
AGK (Steam) V2017.03.31 : Windows 10 Pro 64 Bit : AMD (17.2.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 3rd Apr 2017 08:03
Quote: "why you need this? example of use?"


I remember needing this a while ago.
I had a vector type called:

and in another library I had a point type called:


I also have a vector library that used type tVect2 but I couldn't pass anything to it of type tPoint, obviously.
It is something that can be done in C++ so if you use Tier 2 it's OK, but in Tier one - you're screwed!
AGK V2 user - Tier 1 (mostly)
Markus
Valued Member
21
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 3rd Apr 2017 11:18 Edited at: 3rd Apr 2017 11:18
@Scraggle
if you not need quickness you can convert this type. unfortunately you can not set new values there because its a copy.

Function XY(a ref as tPoint)
...
End Function as tVect2

local a as tPoint
Hello(XY(a))

Function Hello(a ref as tVect2)
AGK (Steam) V2017.03.31 : Windows 10 Pro 64 Bit : AMD (17.2.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
Phaelax
DBPro Master
22
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 3rd Apr 2017 13:49
Ideally, what it sounds like you want is an implementation of Interfaces, which AppGameKit c annot do. In Scraggles case, it might look something like this:



In any case, you might need to rethink the structure of what you're trying to do.

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Richard_6
8
Years of Service
User Offline
Joined: 3rd Feb 2017
Location:
Posted: 5th Apr 2017 04:51
Thank you guys!

Let me give you an example. I have a function with a sliding feature. I want to be able to use this function with any types, not just one (passing by ref).

So let's say the first type contains cards data and the second type contais dice data. Cards or dice could take the advantage of the same sliding feature. That's why I thought of calling a single function for both.

Any insights?

Thanks!
Markus
Valued Member
21
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 5th Apr 2017 11:36
Quote: " function with a sliding feature"


like this?
Type sliding
x
y
End Type

Type cards
s as sliding
End Type

Type dice
s as sliding
End Type

local d as dice
local c as card

Slide (d.slide)
Slide (c.slide)

Function Slide (s ref as sliding )
AGK (Steam) V2017.03.31 : Windows 10 Pro 64 Bit : AMD (17.2.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
GameDevGuy
9
Years of Service
User Offline
Joined: 18th Jul 2015
Location:
Posted: 5th Apr 2017 22:06
Hi Richard,

I can't imagine what you are wanting appearing in the language any time soon.

The ability for a function to work on different types is an ability of so called Object Oriented Programming (OOP), which uses concepts like "Polymorphism".

While some implementations of the BASIC language go down the OOP path, AppGameKit isn't one of them.

Unless someone knows otherwise, (or you really want to fill your functions with lots of messy if then or select statements) I think with AppGameKit you are just going to have a different sliding function for each type.

Personally, while copying, pasting and editing multiple versions of functions does annoy my sense of what elegant programming should be like, I find the simplicity of the AppGameKit language refreshing and makes it more difficult for me to write buggy code when compared to other more sophisticated languages that do have OOP features.
Richard_6
8
Years of Service
User Offline
Joined: 3rd Feb 2017
Location:
Posted: 6th Apr 2017 06:38 Edited at: 6th Apr 2017 06:48
@markus: really clever. I had no clue that this was possible. thanks

@GameDevGuy: interesting perspective and yes, OOP would be great on AppGameKit!

I ended up using something real simple and it worked well. One function working for two different types. The only problem is that any variable inside the types must be arrays as the parameter ref does not accept simple integers. So even if its a sprite ID, I will need to add SpriteID[]. But no big deal!

Phaelax
DBPro Master
22
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 6th Apr 2017 06:52
Quote: "The only problem is that any variable inside the types must be arrays as the parameter ref does not accept simple integers."


I think you need to organize your data structures a bit better and that shouldn't become an issue. I'm not sure what TypeToArray is even suppose to do other than increasing the array the size and incrementing the value of the first element. Function name doesn't describe what appears to be going on in the function.

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Markus
Valued Member
21
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 6th Apr 2017 09:27 Edited at: 6th Apr 2017 09:28
Quote: "parameter ref does not accept simple integers"

unfortunately integer is not supported by ref but you can do this

type myinteger
int as integer
endtype

type Cards
Ace as myinteger[0]
endtype

type Dices
AnyNumber as myinteger[0]
endtype

local d as Dice
d.AnyNumber[0].int
AGK (Steam) V2017.03.31 : Windows 10 Pro 64 Bit : AMD (17.2.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)

Login to post a reply

Server time is: 2025-06-06 17:33:06
Your offset time is: 2025-06-06 17:33:06