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 / Passing UDTs into functions and how to make yourself crazy

Author
Message
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 20th Apr 2012 01:25
Start with a couple of simple UDT:


Add a simple function to process the sprite:


Add another to do something extra to the sprite:


Assume that image successfully loaded into g_Img and desired offset percentages set.

The following works great:


It appears that it is fine to pass a UDT variable into a function and it works. However, if you try to pass the UDT variable to another function, it loses cohesiveness.

In my tests, I output debug stuff to a file. Checking the spdat.i_ind value at the start of makeBridgeFAILS shows that it is correct. Checking the value immediately inside processSprite shows that it has been replaced by something very strange (the value varies).

I'm also having a very hard time passing (and catching) a UDT variable as the return on a function. But that is another story.

Cheers,
Ancient Lady
Marl
12
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 20th Apr 2012 03:11 Edited at: 20th Apr 2012 03:11
I had so much trouble with this, I ended up using temporary work arrays.

I copied the UDT variable into the array and passed the array index to the function.

With the array being global, it didn't have to instance the data for the function, which would just access the data in the work array directly.

Another option to try is create a fresh UDT variable inside the function, copy the passed UDT data to the new variable and use that for the calls to the other functions.

It seems to get muddled up re-passing data multiple times.

Sorry, all I got is guesses. Because of issues in the past, I changed the way I code and almost never pass UDTs to and from functions now.

Maybe with 107...
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 20th Apr 2012 03:56 Edited at: 20th Apr 2012 03:57
All your points are good.

I end up using a combination of the same types of techniques.

After spending hours (literally) trying to figure out why various bits just wouldn't work (and some of the above mentioned techniques), I finally got a combination that did.

Realizing the the double passing of a UDT variable was the main problem (and 100% repeatable), I thought I'd share the issue.

It just might help someone else before they spend too much time banging their head against the wall (like I did).

Happy programming to all....

Cheers,
Ancient Lady
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 20th Apr 2012 16:00 Edited at: 20th Apr 2012 16:01
Don't pass the type into the function.

The following code works perfectly. NO TEMPORARY ARRAYS NEEDED.


"here is a an expression to remember - He who has the gold makes the rules."
Paypal disagreed!
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 20th Apr 2012 16:54
In my actual program, the sprite data object holds 15 elements used to 'describe' the sprite. It gets loaded up in the level initialization function from data in a file. The image index is just one small piece.

In my case, it is necessary to pass a UDT because functions won't allow more than 9 parameters without barfing the program. Note that it doesn't tell you it doesn't like 10 parameters, it just fails at run time with seemingly random errors.

Passing a locally created UDT var from one function into another is fine. It's passing it onto a third function that fails.

Another option is to create the local UDT, copy to a global and then use that global everywhere.

Either way involves two steps (do two separate calls, or copy global after function call).

The example I provided was to show something that currently does not work.

Cheers,
Ancient Lady
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 20th Apr 2012 17:54
The example Mobiius (nice new avatar BTW) has given passes the index of a global UDT array. Then you just reference that one array to get your sprite data. This is a fairly simple way to keep track of your sprites using as many parameters as you like and the way I track all of my sprite parameters to avoid problems like this.

It's not a very oop way of passing data around but it is quite practical.

I think Mobiius was just pointing out a way around the problem, if you are reporting this as a bug (rather than as a problem in need of a solution) then it might get fixed if you post it on the google board here instead:
http://code.google.com/p/agk/issues/list

Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 20th Apr 2012 18:20 Edited at: 20th Apr 2012 18:25
I appreciate what Mobiius was saying, and I have used that method for various things. And I really like his new avatar! (The old one caused headaches.)

I'm not sure if it's really a bug, but it probably is. In the code that doesn't work, the types of the calling parameters in the functions are clearly declared and the variables used in the first level function to build the UDT are global and declared in the main.agc file. So, the compiler should be able to keep the structure (and content) straight no matter how many functions it gets passed through.

I will come up with a better example of good and bad and post it in the google list.

And I'll post the better example here, maybe with a downloadable project.

Cheers,
Ancient Lady
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 20th Apr 2012 19:26
Glad you all like the new avatar! lol

"here is a an expression to remember - He who has the gold makes the rules."
Paypal disagreed!
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 20th Apr 2012 20:51 Edited at: 20th Apr 2012 20:56
Okay, as promised, I submitted to bug list with full project attached.

And I've attached the built version here.

The images were taken from the examples provided with AGK.

Here is the code (for easy reference):


The attached file has a type defined that I ended up not using. And the comment listing the error line is not correct. Sorry about that.

Cheers,
Ancient Lady

Attachments

Login to view attachments

Login to post a reply

Server time is: 2024-05-04 15:32:54
Your offset time is: 2024-05-04 15:32:54