4

I have a game object which processed in two completely different places. In Contact Listener i check some conditions and if they occur i must save one or more portions of complex data. So i decided to use struct. For example:

struct SomeStruct
{
    int value1;
    int value2;
    CGPoint value3;
    b2Vec2 value4;
};

typedef SomeStruct SomeStruct;

In Game Scene i go through all game objects and if its the stack/array not empty, do some stuff and wipe it.

In Contact Listener it repeats from the beginning.

I must use this architecture because of strict order of execution (method must be called after other methods).

I suspect that i need something like vector or NSMutableArray (i think it will not work with struct), so vector may the the only way.

But don't understand how to achieve it. May you help me with some code/pseudocode or link to the book/article where i can found a solution?

2 Answers 2

4

Cocoa provides NSValue class for that purpose:

This creates an object that you can add to NSMutableArray:

NSValue *someObj = [NSValue valueWithBytes:&myStruct objCType:@encode(SomeStruct)];

You can use [someObj pointerValue] to access a void* representing the address of the structure that you put in NSValue.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, can't understand how to get/change a value of certain struct element… :(
@maaboo Convert void* to a pointer to your structure, and change that structure's fields through the -> operator: ((SomeStruct*)[someObj pointerValue])->value2 = 5
3

There is a lot of solutions for this problem.

  1. Don't use struct. An obj-c class is practically the same thing as a struct.
  2. Use CFArray (CFArrayCreateMutable) and put it there as a pointer.
  3. Use a C++ class with STL vector.
  4. Use a C array (SomeStruct[]) and increase its length when you need it.
  5. Use a classic implementation of a stack, with a linked list (every struct has a pointer to the next value).

2 Comments

Yes, probably Obj-C class with only variables will help. I'll try to use it. I'm a bit confused with memory management in this case.
malloc and free :) nothing complicated about that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.