• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.

Archived

This topic is now archived and is closed to further replies.

caffeineaddict

Functions in Arrays?

7 posts in this topic

Hi i''m writing a new program/small game and I had an idea about something but i''m not sure if it would work. I''ll try to explain. I want to make things in a 2 dimensional array be generated at run time, maybe use a 3 dimensional array to not sure though, anyway, i wanted to write a function that did all the necessary stuff to fill in the array and then put the function in the array to put the information there. For example int function() { do stuff... } int array[2][2] { function();,function();,function();, function();,function();,function();, function();,function();,function();, }; would this work? is there another, better and or efficient way to do this, i''m open to criticism, i''m not a genius at this stuff but i''m trying to learn
0

Share this post


Link to post
Share on other sites
it is possible, by making the array as a pointer array to
functions, as described:

  
int (*f)(); //declaration of a pointer to a function

//you can use this to put an array by adding brackets


int func()
{
//your code here

};

void main()
{
f=&func();
//now we''ll just call the function

(*f)();
}

/now we can call the function as



anyway, it works fine for me
0

Share this post


Link to post
Share on other sites
Is an array of function pointers really what you need?

In particular, why exactly do you need a 2-d array as opposed to a 1-D array?

Just curious. 8)

Timkin
0

Share this post


Link to post
Share on other sites
For me, using VC++, I had to do this, or it wouldnt compile:

f=&func;

//No brackets

0

Share this post


Link to post
Share on other sites
the address of a function can be retrieved by just leaving the brackets away...

thus

f = &func();
or maybe safer
f = &(func());

would be simply
f = func;

does
f = &func;
work also?
0

Share this post


Link to post
Share on other sites
well, you can use something like this :
Works fine with my source code (VC++ v6.0)

typedef HRESULT (* tagRenderFunc)();
tagRenderFunc RenderFunc[3];

HRESULT Func1();
HRESULT Func2();

enum eGameState
{
eStatus1,
eStatus2,
} g_GameState;

HRESULT InitGame()
{
...
RenderFunc[0] = Func1;
RenderFunc[1] = Func2; // etc
.....

}


HRESULT RenderGame()
{
....
RenderFunc( (long)g_GameState )();
...
}





0

Share this post


Link to post
Share on other sites
Actually, I think what he''s looking for is to fill an array using a function, not an array of function pointers. Is that right?

To do that, it''s quite simple, just go like this:

  
int array[2][2];
for( int i = 0; i < 2; i++ )
{
for( int j = 0; j < 2; j++ )
{
array[i][j] = function();
}
}


What you''ve got won''t compile, because a static array initializer has to be filled with static data. Also, the size of your array and the size of the static initializer are different (you''ve said the array is 2x2, but you''ve given a 3x3 initializer).

Finally, to LeMatsch, your examples of f = &func(); and f = &(func()); aren''t legal. If you do &(func()), then the compiler will first evaluate (i.e. call the function) func(), and then try and take the address of what it returns. While that''s nothing at all what you''re trying to do, it''s also not valid (since you can''t get the address of a returned value).


War Worlds - A 3D Real-Time Strategy game in development.
0

Share this post


Link to post
Share on other sites
Thanks for the replies. Dean Harding, sorry about that, I knew I said a 2x2 array, but then accidently filled in as a 3x3. Well i''ll try those things out and see if it works. Thanks again.
0

Share this post


Link to post
Share on other sites