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.

DarkBASIC Professional Discussion / Functions vs. Arrays and UDTs

Author
Message
The Wendigo
21
Years of Service
User Offline
Joined: 13th Sep 2002
Location: A hole near the base of a tree in the US
Posted: 20th Oct 2002 06:52
Is it at all possable to pass into a function a whole array or a UDT (User defined type). With my experience I'm having problems with that. I have a function that passes in 6 values for position and size, 2 values for offsets, and 12 cumulative texture indecese. The parameters are going to be huge unless I can use a UDT or array or something! Help!
1.00 GHZ processor, 256 MB RAM, GeForce 3 64MB, SB Live!, 8 cans of soda per day
ChipOne
21
Years of Service
User Offline
Joined: 12th Sep 2002
Location: Canada
Posted: 20th Oct 2002 07:05
one of the tricks you can use is to create the UDT definition for all the complicated parameters along with the corresponding functions into a secondary file. you have to globally declare an instance (or array) of the UDT in the main file, but everything else related to it can be in that second file.

i do this to get around the limitation of not being able to pass a UDT into a function. for now, this is one of your best bets.

rumour mill has it that Lee will be implementing something of the sort in the future, but this isn't possible for now.

cheers.

-= i only do what my rice krispies tell me to do =-
The Wendigo
21
Years of Service
User Offline
Joined: 13th Sep 2002
Location: A hole near the base of a tree in the US
Posted: 20th Oct 2002 07:11
ok, I might try that. I think for now I'll just do it the hard way though. I've screwed up my program so much already I'm a little leary of taking any chances.

THANX!

1.00 GHZ processor, 256 MB RAM, GeForce 3 64MB, SB Live!, 8 cans of soda per day
Aith
21
Years of Service
User Offline
Joined: 21st Oct 2002
Location:
Posted: 21st Oct 2002 09:52
Hey, ChipOne, could you please elaborate on what you just said. I'm not quite sure what to do, and I feel sure that that information will come in handy. Thanks.

ChipOne
21
Years of Service
User Offline
Joined: 12th Sep 2002
Location: Canada
Posted: 21st Oct 2002 21:18
Sure, no problem.

You're coding your latest RPG and you have a little mystic shop that people can come into. Perhaps they can buy certain things; other things can only be looked at.

One of those things is a Magic Eight Ball -- the kind that you shake to get an answer -- only, it has special powers and features that are relevant to the story and the information at hand at a certain point in the game. Somewhere in your story, you work it in so that you can 'remember' questions, and that you have to go to these mystic shops and pay a small fortune to use a Magic Eight Ball to tell you the answer. It is to be displayed with it's own bitmap background, without your character on screen, and it will have to generate a special message based on the question you ask it.

Should the code to control this be in the same location as that killer camera control code you've written? No, probably not.

In fact, I've inadvertantly added TWO problems..."how do we store the questions?" and "how do we control the eight ball?" Silly ChipOne.

Okay, here's what I'd do:
1) We need a seperate file for the questions, and again a seperate file for the eight ball.
2) Each file will have a UDT to describe the object (entity, type, whatever you'd like to refer to it as. I'll call it an object)
3) Each file will have a list of functions that are ONLY relevant to the UDT.
4) Our main file in the project will have to have a globally defined instance of the UDT (GLOBAL Questions(10) AS Question, for example).

The UDT's might look something like this for the questions:


That would give us three possible responses to each question. You can populate those answers when the person 'finds' the question by reading them in from DATA statements.

That said, we can move onto the eight ball.


This will allow us to use differnt colours for different eight balls and store the index of the question in the question array so we can have direct access to that information as well. You could expand that and put whatever information you like in there.

Now, we'd set up functions like:


Note: I indented ShakeEightBall() and RevealAnswer() to illustrate that they should ONLY be called from DisplayEightBall(). This technique is used to emulate scope, a feature in most languages which allows you define which functions are allowed to talk to (or even be seen by) other functions.

Now, where this gets interesting is when you look at the possibility of having different eight balls in different shops or in other locations. Do you want them to all be the same, or will they have unique characteristics? Will they remember things? Know their location? You can put whatever data in the UDT that you like, and then make another array of the UDT for the eight balls (like we did for the questions/answers):



Now we have five eight balls that can be used in different scenarios. But, this is where it really gets exciting: we can use the same functions to do anything we like to ANY of the eight balls! That's right, all we'd have to do is change our function definitions to include an index into the eight ball array, and we can then perform all of our operations based on that number:


This is just a short primer, but it illustrates some of the use of UDTs without getting into any of the advanced uses. Do your reading in the manuals and re-read this article and hopefully things will start to fall into place. If you have anymore questions, let me know!

Cheers,
ChipOne

-= i only do what my rice krispies tell me to do =-

Login to post a reply

Server time is: 2024-05-08 08:59:27
Your offset time is: 2024-05-08 08:59:27