How to reset all values in an array

Learn how to make certain types of games and use gameEditor.

How to reset all values in an array

Postby Hblade » Wed May 23, 2012 2:33 pm

If you want to reset an array and all of it's values, you first need to know the name of your array you are trying to reset. Lets say we have an array called gameArrays[259]. Place this code in the Global Code area
Code: Select all
void
gameArraysReset()
{
    int i;
    for(i=0;i<258;i++)
    {
        gameArrays[i]=NULL;
    }
}


This should work, now once you execute the command gameArraysReset(); you should be able to reset every value in the array.

Note:
The name of the function is the same as the variable array. gameArraysReset is designed to reset that specific array.
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: How to reset all values in an array

Postby Troodon » Fri May 25, 2012 11:59 am

Slightly off topic but can the i<258; part be replaced by i<gameArrays.length?
I can't die, I already tried
~on the forums since GE 1.3.3~
User avatar
Troodon
 
Posts: 1539
Joined: Thu Jan 12, 2006 3:29 pm
Location: HELL
Score: 56 Give a positive score

Re: How to reset all values in an array

Postby schnellboot » Fri May 25, 2012 12:47 pm

and also it has to be i<=258 because you know the last index is 258 ^^
schnellboot
 
Posts: 819
Joined: Sat Mar 31, 2007 1:35 pm
Location: Germany
Score: 102 Give a positive score

Re: How to reset all values in an array

Postby Hblade » Fri May 25, 2012 12:56 pm

mhm :)
Also, if you were to use something like that you'd have to do .length-1 xD Maybe.. or not though :D
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: How to reset all values in an array

Postby Troodon » Fri May 25, 2012 1:19 pm

Yeah, lenght-1 of course. My bad :oops:
I can't die, I already tried
~on the forums since GE 1.3.3~
User avatar
Troodon
 
Posts: 1539
Joined: Thu Jan 12, 2006 3:29 pm
Location: HELL
Score: 56 Give a positive score

Re: How to reset all values in an array

Postby SuperSonic » Fri May 25, 2012 1:34 pm

Wait, so if you have an array like, int myarray[7]; and you want to get it's size, you can just do myarray.length?
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: How to reset all values in an array

Postby Hblade » Fri May 25, 2012 1:53 pm

I havent a clue xD
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: How to reset all values in an array

Postby SuperSonic » Fri May 25, 2012 2:20 pm

Haha, ok :lol:
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: How to reset all values in an array

Postby skydereign » Fri May 25, 2012 3:55 pm

C doesn't use classes for arrays, so you can't use arrayName.length like you might in other languages. If you really wanted to do that you could create your own derived type for the array, some structure that contained your array and your length count. And it wouldn't be length-1 anyways, since you are handling <. The length of that array is 258 (as the values are 0-257). If it were <= you would need a -1.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: How to reset all values in an array

Postby Troodon » Fri May 25, 2012 6:13 pm

Finally, answer to my question. :) Thanks
I can't die, I already tried
~on the forums since GE 1.3.3~
User avatar
Troodon
 
Posts: 1539
Joined: Thu Jan 12, 2006 3:29 pm
Location: HELL
Score: 56 Give a positive score

Re: How to reset all values in an array

Postby schnellboot » Fri May 25, 2012 8:09 pm

sky, actually the length of that array is 279 so it is 0-278 ^^
schnellboot
 
Posts: 819
Joined: Sat Mar 31, 2007 1:35 pm
Location: Germany
Score: 102 Give a positive score

Re: How to reset all values in an array

Postby skydereign » Fri May 25, 2012 8:32 pm

Ah, I didn't read the actual first post. I just assumed it was 0-277 since people were asking about the length -1. Either way though, the length of an array is its last value, which is the value you use in the for loop (since the standard is < instead of <= in for loop).
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: How to reset all values in an array

Postby Hblade » Sat May 26, 2012 3:11 pm

Ya learn something new every day! :)
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: How to reset all values in an array

Postby lcl » Sun May 27, 2012 8:58 pm

Hey Hblade (and others as well) you might want to have a look at this: viewtopic.php?f=5&t=11052#p76692
It's an array resetting function made by me. You just write the name of the array you want to reset and the size of it, or the limit to how far you want it to be limited and it does it all for you! :D
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: How to reset all values in an array

Postby Hblade » Sun May 27, 2012 9:12 pm

Very nice! :D
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Next

Return to Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest