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 / Am I misunderstanding the global command?

Author
Message
roswell1329
14
Years of Service
User Offline
Joined: 12th Apr 2010
Location:
Posted: 14th Nov 2014 21:51
The following code keeps giving me a "Sprite 0 does not exist at line 21" uncaught exception error. The only thing I can think of that would be causing the problem would be the "global" command isn't working as I expected. Any ideas?

Ranietz
AGK Gold Backer
20
Years of Service
User Offline
Joined: 2nd Sep 2004
Location: Norway
Posted: 14th Nov 2014 22:08 Edited at: 14th Nov 2014 22:09
I'm not sure if you can create a global variable and assign a value to it at the same time.
Does this work?

roswell1329
14
Years of Service
User Offline
Joined: 12th Apr 2010
Location:
Posted: 14th Nov 2014 22:11
Why, yes. Yes it does. I feel silly, and I thank you for your assistance!
Ranietz
AGK Gold Backer
20
Years of Service
User Offline
Joined: 2nd Sep 2004
Location: Norway
Posted: 14th Nov 2014 22:12
No problem. I'm happy to help.
Digital Awakening
AGK Developer
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 14th Nov 2014 22:48
You also do not need to declare globals within a function. I put all my globals at the top of the files they are related to. You don't actually have to run past them during run time. They become globals during compilation.

And out of personal preference I wouldn't put the main loop within a function. I don't know if it could cause any problems, but it does look weird to me. You also want to separate reading player input from updating sprites, into 2 separate functions.

Native Tech
11
Years of Service
User Offline
Joined: 19th Jul 2013
Location:
Posted: 15th Nov 2014 00:11
^^all valid points Digital Awakening, this is good code practice

Native Technology
roswell1329
14
Years of Service
User Offline
Joined: 12th Apr 2010
Location:
Posted: 19th Nov 2014 03:16
Thanks for the pointers, Digital Awakening. The reason I put the main game loop inside a function was a beginner object oriented programming class I had recently taken. When using objects, everything is so self-contained and I really liked that model. It may be overkill back in functional programming, but I didn't see any reason not to do it. I'll remember your advice if I have any further issues I can't explain.

I'm really interested in your other point, however, about separating sprites from the player input. In this case, I'm using the player input directly to place a HUD sprite on the screen. Your suggesting I should just update the location of the sprite using global positioning variables in the player input function, and then position the sprite at those variables in another function? Is there a reason for that? Logically, the HUD sprite has more to do with the player input than anything else in the game.
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 19th Nov 2014 09:11
roswell1329 - If you use C++ or Pascal you can have a proper OOP approach.

-- Jim - When is there going to be a release?
Digital Awakening
AGK Developer
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 19th Nov 2014 11:28
roswell1329:
Sooner or later you wish that you had separated input and stored all the data. It might not be obvious now. But I have found that the more complex a project gets, the better this approach is. Down the line you might want to add a feature that would have been easy to do if your code was more structured. It also makes things easier to debug and it's easy to add support for other control methods.

Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 22nd Dec 2014 04:50
The basic problem from the original post is that 'hud' is not declared global in the function. That is required within functions for accessing a global variable.

And, unless it is an array, you should also initially declare the variable as global in your main program. It helps the compiler to do it there and not just hope that it catches it in the function that first declares it.

Cheers,
Ancient Lady
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 22nd Dec 2014 08:36 Edited at: 22nd Dec 2014 08:53
Not sure what you're saying there Ancient Lacy but it doesn't sound right.
Globals (like #constant and type) can be declared anywhere in the code. The compiler doesn't even need to iterate over them for them to be declared.

You could even have a separate file that just contains your global declarations and nothing else. As long is it is #included then they will be declared even the the code never flows over them
=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 22nd Dec 2014 13:41
@Scraggle:
I think this is a new thing. AppGameKit v1 certainly needed the global command to be executed, and the type command as well.

Conjured Entertainment
AGK Developer
19
Years of Service
User Offline
Joined: 12th Sep 2005
Location: Nirvana
Posted: 24th Dec 2014 20:22
Quote: "Not sure what you're saying there Ancient Lacy but it doesn't sound right."

See her AGK Master Badge?
She knows what she is saying even if you are not sure about it.
So, when in doubt just take her word for it.

I need to take everyone's advice and clean up my code's organization. I always say that 'I will clean the code up when I am finished coding everything', but then at the end I say 'if it ain't broke don't fix it' and I leave it sloppy. At least now I use comments, which I didn't do in the past. They help a lot too when your code gets thousands of lines long and you are trying to polish the game.

I never liked OOP, and still don't. It wasn't used much in the 70's since it was deemed 'too complex' by most programmers of the day, so I had been programming over 15 years before I was introduced to it when OOP became very popular in the late 90's.

To each his own I guess. Glad that you got it sorted out roswell1329, and use whatever programming style you want, like the rest of us do. Sounds like lots of good advice here though, if you wanted to try something old or new.

Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 24th Dec 2014 20:27 Edited at: 24th Dec 2014 20:28
Quote: "See her AppGameKit Master Badge?
She knows what she is saying even if you are not sure about it.
So, when in doubt just take her word for it. "


A badge doesn't automatically make someone correct and this case proves that. I'm right, you don't have to take my word for it, you can try it yourself ... and I'm not in doubt, I checked, I suggest you do the same.
Conjured Entertainment
AGK Developer
19
Years of Service
User Offline
Joined: 12th Sep 2005
Location: Nirvana
Posted: 24th Dec 2014 20:37
Well, if both ways work then it makes it a matter of opinion, in which case there is no right or wrong.
And you seemed to be in some sort of doubt when you began your post with "Not sure what you're saying there... "

Thanks for your suggestion, I will try it, both ways.
However, just because declaring anywhere works, doesn't make it a better method or make her statement wrong. She was simply suggesting a method of organizing the code.

Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 24th Dec 2014 20:42
Quote: "The basic problem from the original post is that 'hud' is not declared global in the function. That is required within functions for accessing a global variable."


That statement is wrong.
What I said is right.

Quote: "doesn't make it a better method or make her statement wrong."

Yes, her statement is wrong. You don't have to declare the global in the function. You can declare it in a function if you want to but there is no reason that you have to do it that way.
Conjured Entertainment
AGK Developer
19
Years of Service
User Offline
Joined: 12th Sep 2005
Location: Nirvana
Posted: 24th Dec 2014 20:51
Oh I see what you are saying now that you quoted her like that.
lol

Assigning the value to it in the declaration was the problem anyway, as stated in the post by Ranietz.

You know something, the game I am currently working on has over 1200+ lines of code and I never declared even 1 global variable!
I must be doing it all wrong.

Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 24th Dec 2014 21:00
I'm done.
I'm not going to argue with you when it will take you 2 minutes to write that code that proves that what I'm saying is correct.

You can continue arguing with no-one if you want or you can test it and see that I'm right.
MikeHart
AGK Bronze Backer
21
Years of Service
User Offline
Joined: 9th Jun 2003
Location:
Posted: 24th Dec 2014 21:28 Edited at: 24th Dec 2014 21:28
Quote: "See her AppGameKit Master Badge?
She knows what she is saying even if you are not sure about it.
So, when in doubt just take her word for it. "


If I followed her history correctly here on the forum, it was giving for being very supportive regarding AGK1. AGK2 handles these things differently!!
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 24th Dec 2014 23:16
I think the newer versions of AppGameKit recognize global scoping without requiring the global declaration in each function.

Some languages do and some don't. In Perl and C++, if something is declared outside of a function, it is automatically global and available to all functions (unless they declare a local variable of the same name). In PHP, you must use the 'global' statement in all functions where you want the variable to be global. Fortran worked the same way.

Different flavors of Basic (which is what Tier 1 is) go either way.

I did not look closely enough at Ranietz's post and your response indicating that it worked.

I think it was a case of the parser not quite working as expected with the global declaration and assignment on the same line. There have been some issues with that type of thing off and on.

While I have been programming in lots of languages and platforms for more than 30 years, I don't claim to be an expert in anything. Things change and everyone can always learn something new.

The last time I was working with AppGameKit Tier 1 was version 10819.

I have only recently been able to get back to AppGameKit (new job in Anchorage, Alaska) and have not played with the Tier 1 side of it at all. Given my limited time, I was more concerned with making sure that my WIP Tier 2 project still worked (and it does).

Now that we are fairly well settled in our new home and such, I hope to have more time for this forum.

Cheers,
Ancient Lady
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 25th Dec 2014 00:01
AL - I think he called you Ancient Lucy... Can we have some pics of you in the snow wearing a Santa hat, please?

As most Basics, including AppGameKit, are rather primitive things, and have no concept of modularity, the scope rules seem to me to be this:

Anything not declared inside a function or UDT is inherently global. In other words, it is at lexical level zero and visible everywhere if no other identical identifier is used inside a structure or function at a higher lexical level.

AGK arrays appear to remain always global, although I'm not sure that this is true of dynamic arrays in V2 declared inside a function. As it's 62 minutes to midnight of Xmas Eve, I'm not about to experiment!

-- Jim - When is there going to be a release?
unlikely
12
Years of Service
User Offline
Joined: 5th Mar 2012
Location: Ohio, USA
Posted: 25th Dec 2014 00:08
Seems to me (in most cases) to be bad form to declare a global variable inside a function... less visible that that's what's happening if you have globals scattered all over your functions! Probably a better idea, organizationally, to declare them in one place (probably at the top of a file.) Just my opinion, of course.
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 25th Dec 2014 00:37
In many languages you cannot declare globals in functions at all. I can't see why you would.

-- Jim - When is there going to be a release?
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 25th Dec 2014 19:05
JimHawkins, I had not looked into global scoping in Basic before. But that sounds about right.

I might now try to examine a bunch of languages on this topic (if I could find the time). Off the top of my head, only PHP actually requires the use of the key word 'global' inside functions if you want to access something that was defined outside the scope of any function or class (and not even using the 'global' there).

We've been having an unusually warm winter and very little snow. We might have a Santa hat somewhere. If I can find it and it snows before the end of this holiday season, I'll try to post a picture.

Cheers,
Ancient Lady
Digital Awakening
AGK Developer
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 25th Dec 2014 19:41
Quote: "I think this is a new thing. AppGameKit v1 certainly needed the global command to be executed, and the type command as well."


While this had some bugs very early in the beginning. I have declared my types and globals at the beginning of my files since before I started on RTA.

Login to post a reply

Server time is: 2024-11-25 14:26:59
Your offset time is: 2024-11-25 14:26:59