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 / Game Crashes when function calls another function?

Author
Message
T3dd1n0us
12
Years of Service
User Offline
Joined: 24th Aug 2011
Location:
Posted: 1st Sep 2011 23:32
I have main.agc which defines my game objects:

// Object Structure.
Type GameObj
Obj_Image
Obj_Sprite
x#
y#
Obj_Width
Obj_Height
EndType

//====== Game Objects ==================
// Cards
Card_0 as GameObj
//======================================

in Media.agc I have 2 functions, 1 loads the object, the other calls that function to load each image into an object:

function LoadSprite(obj as GameObj, ImgName as string)
obj.Obj_Image = LoadImage(ImgName)
obj.Obj_Sprite = CreateSprite(obj.Obj_Image)
obj.Obj_Width = GetSpriteWidth(obj.Obj_Sprite)
obj.Obj_Height = GetSpriteHeight(obj.Obj_Sprite)
EndFunction

Function LoadCards()
LoadSprite(Card_0, "0.jpg")
EndFunction

When I call LoadCards() from Main.AGC it crashes. When I call LoadSprite(Card_0, "0.jpg") from main.agc it works fine?

Is this a compiler bug? Or am doign something wrong?

Thanks
Bursar
15
Years of Service
User Offline
Joined: 17th Sep 2008
Location:
Posted: 2nd Sep 2011 11:44
Try declaring Card_0 as a global.

When you call LoadSprite directly you are passing in the local copy of Card_0, so it works.

When using the LoadCards function, there is no local Card_0 for it to work with.
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 2nd Sep 2011 12:20
Quote: "Try declaring Card_0 as a global."

Aren't UDTs automatically global?




I think the problem is here: LoadSprite(Card_0, "0.jpg")
I think there needs to be an element in there such as Card_0.obj_image

Bursar
15
Years of Service
User Offline
Joined: 17th Sep 2008
Location:
Posted: 2nd Sep 2011 12:41
Quote: "Aren't UDTs automatically global?"

Arrays of types are, but I don't know when declaring a single instance of the type.

Quote: "I think there needs to be an element in there such as Card_0.obj_image"

That doesn't explain why it works when called directly. Maybe it's just luck that it assigns the right value to the right element.
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 2nd Sep 2011 13:24
Quote: "That doesn't explain why it works when called directly. Maybe it's just luck that it assigns the right value to the right element. "

I think it could be because when he passes Card_0 into LoadSprites, it expects a datatype of GameObj and so it's legal because he then accesses everything from the object's elements. But when he uses it in the loadcards() function he doesn't use the elements which causes a problem. That's my thinking of it anyway

T3dd1n0us
12
Years of Service
User Offline
Joined: 24th Aug 2011
Location:
Posted: 3rd Sep 2011 09:38
Hi Guys,

Thanks for the reply's

Quote: "Try declaring Card_0 as a global."

All my Card_... are declared in main.agc. They will be used everywhere.

When i change it to sub and call is as suc it seems to work:

Main.agc



Media.agc


When LoadCards is defined as a function with no parameters and called as such it crashes.

Regards
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 3rd Sep 2011 14:31
Oh, I see what you're doing now, my bad, I was getting confused with LoadSprite and CreateSprite. I see. I'm not too sure why that is...
Everyone ignore what I said earlier as it is incorrect.

Bursar
15
Years of Service
User Offline
Joined: 17th Sep 2008
Location:
Posted: 3rd Sep 2011 16:16
Quote: "All my Card_... are declared in main.agc. They will be used everywhere."

Doesn't matter. Despite what the docs say, if you don't specifically declare a variable as global, it will only be local.
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 4th Sep 2011 00:03
Quote: "Despite what the docs say, if you don't specifically declare a variable as global, it will only be local. "

I think UDTs are excepted though (individually and arrays) because I never declare any of my UDTs as global and they work across functions and across different source code files. I'll also point out that I can call a function within a function without the app crashing. Maybe the problem lies somewhere else...

LeeBamber
TGC Lead Developer
24
Years of Service
User Offline
Joined: 21st Jan 2000
Location: England
Posted: 6th Sep 2011 03:10
If this bug(s) is definitely confirmed, can they be added here: http://code.google.com/p/agk/issues/list

I drink tea, and in my spare time I write software.
Funnell7
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location: UK, England
Posted: 18th Sep 2011 01:09
I think I may be experiencing a similar issue. Or maybe I'm doing something wrong? I'm trying to create a simple function, the code is below. Whenever I run the program I am getting the 'Sprite does not exist at line 31'. Reading the above I assumed it was something to do with declaring the Sprite as global. However doing so, doesn't solve the issue either. I've been trying to solve this for the last hour or so and am now completely stumped... Do you have any ideas?



Any help would be much appreciated,

Many thanks!
Hockeykid
DBPro Tool Maker
16
Years of Service
User Offline
Joined: 26th Sep 2007
Location:
Posted: 18th Sep 2011 06:33
Quote: "been trying to solve this for the last hour or so and am now completely stumped... Do you have any ideas? "


You need to make "ButtonSprite" global for it to work in a function, try this:



Funnell7
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location: UK, England
Posted: 18th Sep 2011 14:11
@hockeykid, thanks, I'll give that a try. I did try this,



But this didn't work either. Now that you mention it, I think I remember seeing something saying that you cannot declare global infront, you have to do it on a seperate line. Thanks so much, I'll give it a go...

Login to post a reply

Server time is: 2024-03-28 13:42:21
Your offset time is: 2024-03-28 13:42:21