• 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.
Sign in to follow this  
Followers 0
lipsryme

Accessing 2 arrays using indices of one array [Problem]

14 posts in this topic

I'm having a problem that I can't seem to find a solution to, without having to rewrite everything.

 

Let me try to explain the problem:

I've got 2 object's data types, let's call them A and B.

I'm storing my collection of both of them inside an array (seperately).

Now those two are just for the actual data that I need. There's also another array of just indices which I use to uniquely define my objects inside the scene so they're being assigned to my objects of type A and B during instantiation (0, 1, 2 ,3 ,4, ...).

The problem I'm having right now is in the end what my function is getting is these unique IDs which then have to access the actual data inside the A and B arrays. When just instantiating one of the types A or B this works, but if I have two different arrays, and a single array with indices, how could I access those, using the indices of the unique scene ID array.

 

I hope this wasn't too confusing tongue.png

Basically I have two different arrays that I need to access with their corresponding ID from a single array.

I have the information that the unique ids are either of type A or B and I have their corresponding array's sizes.

I just don't know how to get the corresponding ID into their arrays.

Edited by lipsryme
0

Share this post


Link to post
Share on other sites

Subtract the size of one element to each array's pointer to initiate them at base 1.

Now you can use 0 for a null index, positive values for array A and negative values for array B.

If (index == 0) {
    // Raise exception
} else if ( index > 0) {
    Use A[index]
} else {
    Use B[-index]
}
Edited by Dawoodoz
0

Share this post


Link to post
Share on other sites

Hmm I'm not quite sure I understand...

 

I made a simple code example for my problem, maybe it'll make it clearer:

int main()
{
	std::vector<unsigned int> A;
	std::vector<unsigned int> B;
	std::vector<unsigned int> C;

        // Instantitate object of type A
	unsigned int sceneIDCounter = 0;
	A.push_back(sceneIDCounter); // data inside here doesn't matter
	C.push_back(sceneIDCounter);
	sceneIDCounter++;

        // Instantiate object of type B
	B.push_back(sceneIDCounter); // data inside here doesn't matter
	C.push_back(sceneIDCounter);
	sceneIDCounter++;



	// Now we assume that not every object is being rendered
	// and we're getting an array of unique scene indices to render
	// I've got C (culledSceneIDs) but need to access the corresponding indices inside A and B

	
	// Do A here...
	unsigned int culledSceneIDsA[1] = {0};
	for(int i = 0; i < 1; i++)
	{
		std::cout << A[culledSceneIDsA[i]] << std::endl;
	}



	// Do B here...
	unsigned int culledSceneIDsB[1] = {1};
	for(int i = 0; i < 1; i++)
	{
                // In this example this line would fail because the unique scene ID I'm getting is 1 but the actual data inside B would be index 0
		std::cout << B[culledSceneIDsB[i]] << std::endl; 
	}


	return 0;
}
Edited by lipsryme
0

Share this post


Link to post
Share on other sites

If you are using a predefined vector type, you can subtract 1 from the index during access but it will be harder to understand the meaning of the index.

    // Do A and B here...
    unsigned int culledSceneIDs[2] = {1, -1};
    for(int i = 0; i < 2; i++)
    {
        int index = culledSceneIDs[i];
        if (index == 0) {
            // Raise error
        } else if (index > 0) {
            std::cout << A[index - 1] << std::endl;
        } else {
            std::cout << B[(-index) - 1] << std::endl;
        }
    }
Edited by Dawoodoz
0

Share this post


Link to post
Share on other sites

I'm not sure this would work.

The function that is doing the access is being called once for an array of sceneIDs of either type A or B so I can't loop over both at once and branch.

And second the culledSceneIDs can never be negative, they are defined as [0, INF] and increment after each instantiation of either an object A or B.

 

So let's say I've got 10 objects inside my scene, which translates to a storage of 10 unique scene IDs (going from 0 to 9).

Now the function that wants to access the actual data gets an array of those that are not culled, so basically I'm getting something like:

 

culledSceneIDs[4] = { 0, 5, 2, 1 }

 

Which I know are of either type A or B. But I don't know at what index these are inside the actual data array.

These 10 objects I've got in my scene could be 3 of type A and 7 of type B, meaning that the data array of A would be from 0-2 and the data array of B from 0-6.

Now I have to somehow translate the unique scene ID to the actual data id.

 

 

Assuming the above sceneIDs are of type A, that means the first object instantiated was of type A, then came 4 objects of type B and the fifth instantiation was type A again.

But of course the actual data of this fifth would be at index 1, not 5.

 

In this case you could say, sure just subtract the size of B from the sceneID. But now you'd have the problem that when you're processing the first sceneID (0) you would get a negative value of -4 instead of the actual 0 

Edited by lipsryme
0

Share this post


Link to post
Share on other sites

You subtract the size of one element from the pointer like this:

Array = (myType*)((unsigned int)Array - sizeof(Array[0]))

Now Array[1] is the new Array[0].

Edited by Dawoodoz
0

Share this post


Link to post
Share on other sites

Am I doing something wrong?

	// Do B here...
	unsigned int culledSceneIDsB[1] = {0};

	unsigned int* newArray = (unsigned int*)((unsigned int)culledSceneIDsB - sizeof(culledSceneIDsB[0]));

	for(int i = 0; i < 1; i++)
	{
		std::cout << B[newArray[i]] << std::endl;
	}
0

Share this post


Link to post
Share on other sites

 

Am I doing something wrong?

	// Do B here...
	unsigned int culledSceneIDsB[1] = {0};

	unsigned int* newArray = (unsigned int*)((unsigned int)culledSceneIDsB - sizeof(culledSceneIDsB[0]));

	for(int i = 0; i < 1; i++)
	{
		std::cout << B[newArray[i]] << std::endl;
	}

 

The offset should be applied to the B array and the indices should use -1 to reach the first element of B.

0

Share this post


Link to post
Share on other sites

So you have an array of scene object ID's:

 

1,2,3,4,5

 

And each has a corresponding type that you know:

 

A,A,B,B,A

 

And youve got the type storage:

 

A,A,A,B,B

 

So to get the data of a entity, you need to:

1.Calculate how manyth [Data type] of all entities it is (how many entities with the same data type are before it)

2.If it is B, you need to know how many A in total there are.

 

So, you need to keep track of how many A there are in total. That should be easy.

 

But you also need to calculate how many entities with the same type there are before the current entity.

This can be done by either:

1. Iterating through the entities in order (1,2,3,4...) and counting at the same time. You might be able to do this while culling (count while checking visibility) but it will not work for all methods of culling (eg. ones that dont iterate over all entites in order). Basically you then save the preceding entities of same type with the visible entity entry.

 

2. Iterate over all entities in order and for each save how many preceding entities of the same type there are. Same as above but this adds memory overhead for each entity, but only needs to be done for new entities and when an entitys type is changed.

0

Share this post


Link to post
Share on other sites

I'm still not quite sure how the algorithm for the lookup would be assuming I'd know how many object's of the same type are before the index I'm trying to access. (And you are talking about the array that has all unique scene IDs stored, when you talk about the entities?)

I know how many items of each type there are in the scene of course.

Could you give me an example?

Edited by lipsryme
0

Share this post


Link to post
Share on other sites

AB=AAA...ABBB...B

if the entity is of type B:

IndexInAB = AmountOfA + AmountOfBEntitiesBeforeEntity + 1 //i think

 

if its A:

IndexInAB = AmountOfAEntitiesBeforeEntity + 1

 

 

This is assuming that you add the A/B in the AB container each time you add a new entity, always at the end of all As or Bs, and that the entity you add will be the highest ID and that there are no gaps in the entity ID's

 

But if you are going for something dynamic your system isnt really going to work. It might work for some serialization but for other things not so well.

0

Share this post


Link to post
Share on other sites

No wait i might have misunderstood the question.

 

In the code you posted with vectors A,B,C

 

when going over B you should remove the amount of A from the index and it should work.

0

Share this post


Link to post
Share on other sites

That's exactly the problem. Let's consider I instantiate 3 of type A first. So these will have sceneIDs of (0,1,2).

Accessing them using these indices - amount of B works of course because B is 0.

Now when I instantiate one of B it's going to crash because the amount of B is now 1. So when going through the array above for all the A's (0, 1, 2) the 0 is going to become -1 when subtracting the amount of B's from it.

0

Share this post


Link to post
Share on other sites
So you have A0, A1, A2, B3

you can directly use the A indices.
But to use B index (3) you need to subtract amount of A, which gets you 0, the correct B index.
0

Share this post


Link to post
Share on other sites

That's true but in return A would fail if you were to do the same for getting the A index, because then it would give you -1, 0, 1 as indices for 0, 1, 2 scene ID's.

I don't think my design will work at all. If there's a perfect and easy solution to calculate the exact Indices from the sceneID's then that would be great, but I'm probably going to have to change my design.

 

Update: Got it working. I redesigned it to store an std::pair<unsigned int, unsigned int> to always store the actual data ID in combination with the unique sceneID.

Was a few hours of work but this was the best solution.

Edited by lipsryme
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0