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.

Dark GDK / Functions and Arrays

Author
Message
Deeamon
16
Years of Service
User Offline
Joined: 6th Jun 2008
Location:
Posted: 6th Jun 2008 17:27
Hi guys,

I have looked through forums and tried to use the visual c++ help files and even went online to find answers but to no avail.

I have learned how to creat a simple array.

int myobjects[1];
myobject[1]=1;

For example.

My problem is I want to make the array global through all of my functions. How to do that.

Sorry I am new to c++. Basic I have down but I want to see how well the performance of dark gdk is compared to dark basic.

Hope this made sense.

Thanks
Deeamon
unitech
17
Years of Service
User Offline
Joined: 27th Jun 2007
Location:
Posted: 6th Jun 2008 19:31 Edited at: 6th Jun 2008 19:32
I would suggest learning about classes. You make a main.cpp like you did.. then you make a game.cpp with a game.h file. the h file is a class that holds all of you data and what not. You decide in that h file what is global and private.


indulge
http://www.cplusplus.com/doc/tutorial/
Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 6th Jun 2008 19:57 Edited at: 6th Jun 2008 19:59
If you create the array outside of any function then it will be accessible to all of your functions. Any function can change the array and any function can read from the array.

e.g.

Attachments

Login to view attachments
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 6th Jun 2008 22:15


Does that actually work. It would likely fail in runtime. You've declared an integer array of only one integer. Since arrays are zero-based the only addressable element would be myobject[0].

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
unitech
17
Years of Service
User Offline
Joined: 27th Jun 2007
Location:
Posted: 6th Jun 2008 22:27 Edited at: 6th Jun 2008 22:28
haha good call lilith,, Totally didnt catch that. yeap, you array from int myobjects[1]; is [ byte ]-> null So only have one element. myobject[1]=1; is asking for the second element and there is none.. As lilith pointed out you would need myobject[0]=1;
Deeamon
16
Years of Service
User Offline
Joined: 6th Jun 2008
Location:
Posted: 6th Jun 2008 22:43
got it. Thanks. I actually was able to get the array to be global. Thanks Michael. I have another question and sorry if I sound like I don't know to much.

How can I store 'string' values in the arrays. If I set up and int array obviously I can only enter integers. I tried to set up char arrays but when I do this

char inventory[20]

instead of being able to enter records into 20 fields it only allows 20 characters to be typed in the inventory variable. Am I missing something. It is apparent that I really need to learn c++ better but alittle help could go a long way

Thanks Again. The quick reply was awesome.
deeamon
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 6th Jun 2008 22:57
Quote: "char inventory[20]

instead of being able to enter records into 20 fields it only allows 20 characters to be typed in the inventory variable. Am I missing something."


What you declared is an array of 20 characters. That means only 20 characters. Before I go on I will issue the standard warning that the programmer is generally responsible for providing enough storage space for the data he/she needs. Using strings alleviates this a bit but increases the learning curve. My rule is to err on the side of excess and reserve more space than I think I'll need since user input could go wild.

Do do what you want to do is something like this.

char inventory [20][50];

Which essentially sets up an array of 20 character arrays of size 50 characters each. In order to, say, copy some text into the fifth element of the array you do something like:

strcpy (inventory [4], "Flying Fickle Finger of Fate");

and to print it out would be

dbPrint (inventory [4]);

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Deeamon
16
Years of Service
User Offline
Joined: 6th Jun 2008
Location:
Posted: 6th Jun 2008 23:17
awesome Lilith. I think I got it now. I appreciate your help.

Deeamon
Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 7th Jun 2008 03:56
Lilith is our local genius. Uh, one of them. We have a bunch of 'em.

If you can do any models for FW, reply to the FleetWars thread.

Click here!

Login to post a reply

Server time is: 2024-10-07 21:27:20
Your offset time is: 2024-10-07 21:27:20