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 / Declaring and using UDTs, globals and constants in include files

Author
Message
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 1st Jun 2012 19:41 Edited at: 1st Jun 2012 20:19
Declaring constants in include files appeared to work when I tested it.

Declaring UDTs (types) in an include file also appears to work now.

EDIT: It turns out that it doesn't work to define types in include files -

menu.agc (see the main.agc for the things that look like they are missing here):


main.agc:


The error also occurred when the include/global declaration were reversed.

I know declaring globals doesn't, and it should.

This might be why.....

When the AppGameKit IDE compiles an app (Tier1, obviously), it copies all the code in the main.agc file into a file called SourceCode.agc in the media directory, skipping any include statements.

Then it goes through the include list and adds the code for each, in the order seen, to the SourceCode.agc file.

The compiler also creates a SourceCode.txt file in media which appears to be something that defines sizes/offsets/types/names. It contains things in this order:


When you declare a global in main.agc, it shows up in the SourceCode.txt file.

When you declare a global in an include file, it does not show up in the SourceCode.txt file, even though it does appear in SourceCode.agc. This would appear to indicate that the compiler ignores the global command while parsing include files.

Cheers,
Ancient Lady
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 1st Jun 2012 20:38
If you do not use a UDT in an array, then it is okay to define it in an include file.

But if you make an array of them, the UDT needs to be defined in the main.agc file.

At least that is what I see happening.

Cheers,
Ancient Lady
Marl
13
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 1st Jun 2012 20:44
As you surmised, Include files are added to the end of main.agc.

With constants that doesn't matter because they are evaluated on the first compiler pass.

The same is not true of UDT's, which must be declared before they are used. It doesn't have to be at the start, as long as it's before their use.

Declaring a UDT in an include file means the declaration goes at the end of main.agc, so it can't be used in main.agc as it hasn't been declared yet.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 1st Jun 2012 20:54
I am able to define a UDT in an include file, declare a single instance (non-dimmed) variable of that type in main.agc and use it anywhere and it works.

The problem only comes when you try to create a global array of the UDT.

The compiler fails to recognize the UDT at that stage.

The media/SourceCode.txt file will show a global array of the UDT and a discrete variable of the UDT, but can't reconcile them.

Within media/SourceCode.txt, it prepends array names with '&' and place all of them ahead of the discrete variables.

I think we are seeing a basic problem with UDTs in arrays (which we've seen before).

Cheers,
Ancient Lady
Marl
13
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 1st Jun 2012 21:08 Edited at: 1st Jun 2012 21:08
If it helps, I tend to only use UDTs in the file they were defined in and don't use direct access to their fields from anywhere else.

Values are accessed using simple functions from that same include

It adds a couple of cycles going through the functions but it keeps everything modular so I can plug include files in where I need them and they bring with them the functions needed to access their data.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 1st Jun 2012 21:21
You still need to define the global array in the main.agc file. I've tested this several times. It passes compile, but fails when you try to execute.

It's just as easy, until TGC does anything about it, to define the types that I know will be used in arrays in main.agc.

I was only hoping that it was one of those things that now worked the way us old-style programmers would like them to.

Cheers,
Ancient Lady
Marl
13
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 1st Jun 2012 22:05
Quote: "You still need to define the global array in the main.agc file"

What I'm saying is that I don't do it that way.

If the array is a display array, I define it in a display include, a game array in a game include, a menu array in a menu include. Everything is compartmentalised

The only thing I define in Main.agc is program control stuff such as program state, loop times etc. Everything else is includes.

The examples I post on here are the exceptions, which are kept in one file for simplicity.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 2nd Jun 2012 00:25
You might not do it that way and I would rather not. I'd much rather do it the way you describe. But it just doesn't work for me if I try to copy the UDT object from the array.

I tried a test that defined the UDT, a global array of that type and a single variable of that type in an include file.

The UDT was defined before the variable and array were declared.

Neither is used in any other file.

Inside a function within same file, I looped through the array. The first thing I did was copy the current array element into the single variable (this turned out to be a solution to a problem with directly referencing elements of the UDT in the arrays in 1065).

The compiler had no issue. But 100% of the time, at run time, the "both user defined type values must use the same type" error would pop up for the line where the copy is being done.

100% of the time this works fine as long as the array is declared in main.agc.

I consider this to be a bug.

But, since I can work around it, I just do that and don't worry about it.

I will create a simple project that demonstrates this and post it in the google list.

Cheers,
Ancient Lady
Impetus73
13
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 2nd Jun 2012 12:40
Ancient lady, everything inside an include file HAS to be inside a function, then you call it after the include command. Ifnot, it will not be run, as it's below the main file code.

main.agc:


init.agc:


----------------
AGK user - novice
Did Amiga / AMOS programming in the 90's, just started programming again with AGK.
Cliff Mellangard 3DEGS
Developer
18
Years of Service
User Offline
Joined: 20th Feb 2006
Location: Sweden
Posted: 2nd Jun 2012 13:36 Edited at: 2nd Jun 2012 13:40
I always use gosubs for my types!
You can place them before the include command as agk dont care.

I want my types and globals and constants for specific tasks in the same file

Example

Rem // setting up entity types and globals
gosub setup_entitys

rem // here do i include the file.
#include "Entity.agc"

Rem // assign a number to a type.
entity.spanky= 666

This always works for me.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 2nd Jun 2012 19:29
Impetus73, I'm a little confused by your comment.

Cliff, I would also like to do that. But some things just don't work correctly.

The code snippets in the first post for this thread illustrate the problem I am having in a simple way.

Because of the way the compiler handles things in different passes, it is perfectly okay to use '#constant' in include files.

In theory, you should also be able to declare globals, but that never works.

For UDTs, if it is used exclusively within the include file AND you do not try to copy an array entry into a variable of the same type as the array, you can get away with putting the UDT definition in the include file.

You always have to put globals in main.agc.

Cheers,
Ancient Lady
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 3rd Jun 2012 00:23 Edited at: 3rd Jun 2012 00:24
My experiences have been different.

Globals have never caused me a problem no matter where I declare them, even from within functions in included files.

I always call constants from my main.agc but it sounds like these might have been fixed.

UDT's and UDT arrays I have always worked when declared for the first time in the main.agc file and intermittently when declared elsewhere for the first time.

Quote: "You always have to put globals in main.agc."

I haven't found this at all.

This is all a little confusing. I think we need some clarification from TGC if Paul is listening?

Cliff Mellangard 3DEGS
Developer
18
Years of Service
User Offline
Joined: 20th Feb 2006
Location: Sweden
Posted: 3rd Jun 2012 01:00 Edited at: 3rd Jun 2012 01:04
Quote: "Cliff, I would also like to do that. But some things just don't work correctly."


But you cant do this !!

#include "menu.agc"
global dim g_menu[5] as tMenu
global g_men as tMenu

You confuse agk!

You have to create the same type setting like this!
type tMenu
i as integer
b as string
endtype

But call the other one tMenu2.

Something like this!
////////////////////////////////////////////////////
type tMenu
i as integer
b as string
endtype

global dim g_menu[5] as tMenu

type tMenu2
i as integer
b as string
endtype

global g_men as tMenu2
////////////////////////////////////////////////

You are trying to create both an array type and an ordinary type of the same one that confuses agk.

And as an extra note!
If you use constants and want to do an calculated value simply use brackets
#constant MyNumbers (10+28)
This dont work!
#constant MyNumbers 10+28
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 3rd Jun 2012 02:39 Edited at: 3rd Jun 2012 03:47
Cliff, my declaration does not confuse AGK. I use exactly the same type for the array and the single variable. It should be no different than declaring an array of integers and copying one value to an integer variable.

The UDT is a definition, just like string, float or integer, just more complex.

If I used your suggestion, I would, in fact, be trying to assign a different type. As long as I define my globals in main.agc, everything is happy.

EDIT: The whole point of UDTs is to be able to define an object type once and use it repeatedly.

Cheers,
Ancient Lady
AgentSam
12
Years of Service
User Offline
Joined: 14th Mar 2012
Location: Virtual Space
Posted: 3rd Jun 2012 12:18
I am following this thread with some interest...

Tier 1 Basic - Dividing a project into multiple files using include files

I also like to create modular projects - and with AppGameKit Tier 1 Basic, that means having to use include files.

To avoid compiler problems, which I have noticed exist in abundance (!), I only put FUNCTIONS in the include files - and I declare all constants, UDT, and globals in the main.agc file.

The AppGameKit example project Games/SpaceShooter demonstrates this type of modularization fairly well. In this project, all the globals and types are in the main.agc file. The include files are nothing but a collection of functions that use those types and globals.

Note, however, that this is in sharp contrast to how things would be done in C/C++ where the constants, structs/unions, globals and function declarations would be the ones that are in the include files. The function definitions would still be in a separate .C or .CPP file, though. (This is an almost complete opposite of Tier 1 Basic. No?)

Include file behavior, in regard to globals and UDTs

Baxslash wrote:
Quote: "I think we need some clarification from TGC if Paul is listening?"


I agree, it would be nice to hear from the language developers what limits exist on the use of globals and UDTs in include files. The exact rules as defined by the language, please.

It's seriously counter-productive that we have to keep guessing on this, and it seems that milage varies. So, some comments from the language developers on this, would be great.

And if what they say doesn't work - then let's post a bug report and have it fixed!
Impetus73
13
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 3rd Jun 2012 12:22 Edited at: 3rd Jun 2012 12:36
Ancient Lady, your right, there is something fishy with the originally posted example...

I get errors no matter how I try to resolve it, you should post it on the google board.

----------------
AGK user - novice
Did Amiga / AMOS programming in the 90's, just started programming again with AGK.
AgentSam
12
Years of Service
User Offline
Joined: 14th Mar 2012
Location: Virtual Space
Posted: 3rd Jun 2012 13:10
If you copy the UDT elements individually, you avoid the error.

menu.agc contains:


main.agc contains:


However, Ancient Lady, please clarify this for me:

When you declare an array like this "global dim g_menu[5] as tMenu", doesn't this mean it's a zero based array with indexes ranging from 0 to 4? Yet, in the for loop you use indexes from 0 to 5.
The AppGameKit manual is, once again, not very helpful in its description of arrays and dim. (It reads like a mystery novel, only giving one piece of information here and there, but never the complete picture of a particular topic in one place, as one might expect of a proper language reference.)
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 3rd Jun 2012 13:13
Totally agree about modularity. Without a proper modular system, which Basic isn't, I would have though that rules should be fairly simple: when including files the whole of that file should be inserted into the text stream and parsed at that point - with the compiler noting that if it had already included a file it wouldn't do it again (to avoid circularity where several files import another file).

That would have no impact on the language definition as such, would it, would clarify things, and aid code-re-usability.

-- Jim
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 3rd Jun 2012 17:46
AgentSam, in AppGameKit Tier1 if you dim something as having X elements, you are actually creating space for X+1 and can use indices from 0 to X.

Don't try the same thing in Tier2.

Also, there would be no point, almost, with declaring a UDT variable the same as the elements in an array if I was going to then copy each piece separately. Especially when there are 10 to 20 pieces.

Impetus73, I will create a couple of sample projects and post them on the google board later (I'm still doing morning routing stuff).

Cheers,
Ancient Lady
MarcoBruti
13
Years of Service
User Offline
Joined: 20th Nov 2011
Location: Caput Mundi
Posted: 3rd Jun 2012 17:58
BTW, anyone knows if:
- is it possible to pass arrays inside function calls?
- is it possible to pass UDTs inside function calls?
In other languages, this is done by passing pointers or references. In DarkBasic, there are memblock that let you create arrays or vectors using a pointer-like type. Do you think that memblock shall be introduced in AppGameKit Tier-1, or shall we have to turn to Tier-2? I have no reasons or willing to revert to Tier-2, since I do not want that my hobby turns to be a nightmare, and I find AppGameKit Basic very easy to use and fast to deploy, as DarkBasicPro is.
Here is attached a short library I have written to manage that for my DarkBasicPro projects that requested to pass arrays into functions. I would like to do similar things in AGK.
AgentSam
12
Years of Service
User Offline
Joined: 14th Mar 2012
Location: Virtual Space
Posted: 3rd Jun 2012 18:25 Edited at: 3rd Jun 2012 18:27
Ancient Lady wrote:
Quote: "Also, there would be no point, almost, with declaring a UDT variable the same as the elements in an array if I was going to then copy each piece separately. Especially when there are 10 to 20 pieces."


I agree. And I did not mean to imply that you should. It should absolutely be possible to assign a variable of user defined type, to another variable of the same type, directly.

If I'm not mistaken, you are doing a similar UDT assignment here:
http://forum.thegamecreators.com/?m=forum_view&t=195252&b=41&msg=2327946#m2327946



So, just out of curiosity, did it work in that project? While essentially the same thing fails to work, when include files are added to the equation?
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 3rd Jun 2012 19:29
It is possible, as of 107, to pass UDTs into functions. But I don't know if any changes made to the passed UDT will stick. I haven't tried that, yet.

I don't know about arrays. I strongly suspect the answer is no because AppGameKit Basic does not seem to store information about size that would pass into the function along with the array. This is related, I suspect, to the issue of putting arrays within UDTs. AppGameKit basic does not really handle arrays of UDTs very well (at least there have been enough issues about it, as evidenced by the post AgentSam found).

AgentSam, you found the post where I listed a more complete example and where I found the solution to dealing with arrays of UDTs. I was already using include files by that time. The UDT was defined and declared in main.agc and the code executed with a function in an include file.

Yes, the solution I posted did work. And it is the same project I am currently working on. Which is much closer to being finished.

Cheers,
Ancient Lady
AgentSam
12
Years of Service
User Offline
Joined: 14th Mar 2012
Location: Virtual Space
Posted: 3rd Jun 2012 19:44
The one conclusion I have drawn from all the problems surrounding Tier 1 Basic, is that Tier 2 is the way to go.

In fact, I would not be sorry for one minute if Tier 1 Basic was killed off completely. It's a bloody mess, the compiler is crappy, the documentation is a childrens story, and that doesn't even touch on the actual bugs in the AppGameKit graphics engine architecture. And the amount of time spent caring for the Tier 1 basic could be spent on working on the engine architecture.

I'm really sincerely sorry to be such a grumpy old man here (among the oldest on the forum very likely), but unless I was already working on a Tier 1 project to be published, I would have walked away from Tier 1 already. My next project will DEFINATELY NOT be using Tier 1 basic.

For all those who absolutely need to rely on Tier 1 basic, I hope all the problems with it get resolved sooner rather than later.
Cliff Mellangard 3DEGS
Developer
18
Years of Service
User Offline
Joined: 20th Feb 2006
Location: Sweden
Posted: 3rd Jun 2012 19:55
Quote: "In fact, I would not be sorry for one minute if Tier 1 Basic was killed off completely. It's a bloody mess, the compiler is crappy, the documentation is a childrens story, and that doesn't even touch on the actual bugs in the AppGameKit graphics engine architecture. And the amount of time spent caring for the Tier 1 basic could be spent on working on the engine architecture."

Do you know how many that bought agk thanks to its basic version

Kill basic will kill agk as there is to many app tools already like tier 2.

I like tier 1 and if it gets killed so do i leave agk for something else.
polomint
12
Years of Service
User Offline
Joined: 3rd Apr 2012
Location: Lancashire, United Kingdom
Posted: 3rd Jun 2012 22:06
Tier 1 is great for knocking together a quick prototype before converting it all to Tier 2. This is the way I tend to do things...

Blackberry App Development & ZX Spectrum Game Development.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 3rd Jun 2012 22:22
polomint, I think I'll go that route also, once I get my current app out there.

On the other hand, Tier 1 is the way to go to code once and publish on many platforms. It does work. Even if we manage to find various issues on the different platforms.

Cheers,
Ancient Lady
polomint
12
Years of Service
User Offline
Joined: 3rd Apr 2012
Location: Lancashire, United Kingdom
Posted: 3rd Jun 2012 22:34
Yeah, code once and multiple publishing is handy, although once you have a Tier 2 development environment setup for each platform it does get easier, although keeping the code for each platform can be a pain..

Blackberry App Development & ZX Spectrum Game Development.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 3rd Jun 2012 22:49
Yup. I know. I've had to deal with issues like that a lot.

The fun I have now is working on my PC and my Mac at the same time. All the keyboard shortcuts are different. Way back when, I monopolized 4 computers in the company computer room one day. Each was a completely different operating system, command set and keyboard setup. In addition to annoying the other programmers (because they couldn't use those machines), I must have burned lots of brain activity calories keeping straight what machine, project, etc. I was working on.

There's a lot to be said for having a mild attention deficit and obsessive/compulsive issues as a programmer. Multi-tasking is much easier.

Cheers,
Ancient Lady
MarcoBruti
13
Years of Service
User Offline
Joined: 20th Nov 2011
Location: Caput Mundi
Posted: 4th Jun 2012 00:16
Hi, in 5 months I published 5 apps with Tier-1.
Tier-1 is very powerful. With Tier-2, I suppose that I had to do a lot of additional effort, even with more flexibility.
BTW, on Windows XP the compiler is not yet very stable.
In my opinion Tier-1 with additional platform-specific plug-in is the future.
Cliff Mellangard 3DEGS
Developer
18
Years of Service
User Offline
Joined: 20th Feb 2006
Location: Sweden
Posted: 4th Jun 2012 01:24
By the way ancient lady
You are completely right about using the types that way .
I noticed it when i started to go thru pedrolbs astar code.

i did now that i could do that but iam not an coder but an happy hobbyist
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 4th Jun 2012 01:34 Edited at: 4th Jun 2012 01:36
Structures in Pascal were my first introduction to 'encapsulating' data. C++ and OOD (self-taught on that) made it even better.

Being able to make things compact and 'easy' to transport is great.

As a 'formally trained' and long experienced professional coder, I am more than happy to listen to anyone else. They may have ideas that I did not think of.

My ex says the thing says that he hated most about giving me up (his idea, not mine) was the bragging rights to the best programmer he's ever known.

I'm good, but I can always learn. And it is those who haven't been taught and have been learning on their own (which I've done with several languages) that may have the most imaginative ideas.

Outside the box is the best way to think in programming. Tequila and beer don't hurt either.

Okay, this is my last post for the night (just enough Tequila to really relax, too much to be responsible in my posting).

Happy Programming to all! And to all, a Good Night!

Cheers,
Ancient Lady
AgentSam
12
Years of Service
User Offline
Joined: 14th Mar 2012
Location: Virtual Space
Posted: 4th Jun 2012 11:55
Good night... Ancient Lady. (However - good morning in these parts.)

I was a bit grumpy last night (morning for some), but I'm sure Tier 1 will get better in time. I know there's value in it. It's just not in a happy place at the moment.

Cheers.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 4th Jun 2012 21:08
Well, I just ran a set of tests.

It turns out that if you declare something global inside a function in an include file, it truly is global and can be accessed anywhere.

And you can do the type declaration in the include file, because it won't be used until the function is called.

Someone is probably entitled to an "I told you so" about here.

It just never occured to me that I could do the global dim thing in a function and have it stick. I should have realized that was okay because you can certainly do it in PHP. Many other languages require that a global variable must actually be declared by itself (not in a function) somewhere.

We all learn new stuff. Now I'll go play with my files to do the best 'encapsulation' possible in AppGameKit Tier1.

Cheers,
Ancient Lady

Login to post a reply

Server time is: 2024-11-23 15:21:12
Your offset time is: 2024-11-23 15:21:12