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 / Functions inside types

Author
Message
Max123
8
Years of Service
User Offline
Joined: 22nd Mar 2016
Location:
Posted: 13th Apr 2016 13:44
Hello,
I'm very new to App Game Kit. Since I've used Lua, Python and a few other languages before I'm used to writing classes that don't only contain variables but also functions.
In App Game Kit 2 however I get error messages when I try to do something like this:

type myType
x
y
function setX(x)
....
endfunction
endtype

Is there any workaround? For my game I need a lot of collision detection (without physics) and writing a function for every single objects would be too much work und not very effective.
Thank you for any help
Max123
8
Years of Service
User Offline
Joined: 22nd Mar 2016
Location:
Posted: 13th Apr 2016 14:46
And I would have another question:
I would like to use parameters in functions that are types:

block1 as block
player as Player

function collision(object1, object2)
if object1.x (and so on)
endfunction

collision(block1, player)

Can I do something like this?
Dybing
13
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 13th Apr 2016 15:59
To your first question, the simple answer is no. AppGameKit is a procedural language, so data is entirely separate from functions - unlike object oriented languages where these can be conjoined into a class.

In AppGameKit and other procedural languages, data is data and actions are actions.

The 'workaround' is to pass two (or more) datatypes into a function that then check for collisions between the two. You don't need a separate function for each datatype. More or less as you outlined in your second question
Max123
8
Years of Service
User Offline
Joined: 22nd Mar 2016
Location:
Posted: 13th Apr 2016 17:33
Thank you for your answer
The "example" I posted doesn't work. I always get an error message that states: "object1 has been defined as an integer, not a type"
How can I fix this or could you give me an example how something like that is usually made?
Dybing
13
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 13th Apr 2016 19:15 Edited at: 13th Apr 2016 20:02
Something like this should work:



Basic premise is that the spriteIDs are stored in one datatype and then the object itself in another. If several of one type, you just make an array out of both the spriteID variable and the object datatype - and add a pointer to it in some kind of control structure.

edit: And yes, the reason you got the error message is that you defined your function without explicitly telling it what type of data it will receive. AppGameKit then just assume you want an integer.

So for instance function doStuff(object1, object2) is the same as function doStuff(object1 as integer, object2 as integer). To pass a datatype, you need to declare it explicitly as in my example above.
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 16th Apr 2016 02:54
I would give this a read as well. Particularly the "Passing by Reference" section
http://www.appgamekit.com/documentation/guides/12_array_changes.htm
Max123
8
Years of Service
User Offline
Joined: 22nd Mar 2016
Location:
Posted: 16th Apr 2016 21:42
Thanks to both of you
Although I believe I now understand how this works, I would prefer to write my code in C++ as I'm not used to writing code in this style.
I'm just currently having troubles adding new source files to the provided Xcode project on Mac since I always get errors when I do it but I think it's best to open a new thread on this topic.

Login to post a reply

Server time is: 2024-11-24 00:18:27
Your offset time is: 2024-11-24 00:18:27