Curiosity Labs
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Cool Code: Members in virtual subclasses

2 posters

Go down

Cool Code: Members in virtual subclasses Empty Cool Code: Members in virtual subclasses

Post  Nate Wed Jun 29, 2011 1:49 pm

Functions as exclusive constants:
So I was thinking about how to manage Entities in more concise and encapsulated way. The Salem implementation works in a small scope, but in cases where Entities are subclassed into dozens of classes or more, the base class would become unmanageable.

Dynamic binding (the trick we're using in C++ to treat every entity subclass as an Entity), allows us to call

Code:

for( all entities )
  entity->update()

and since each function implementation for each subclass of Entity is different, each different type of entity is able to perform separate actions.

Extend this implementation to the mess of fields we currently have in the Entity base class.

Constants can be replaced with function calls exclusive to subclasses of entity as long as they are only called from within that class. For instance, within the update() function of Fighter (a subclass of Entity), you could call

Code:
float speed_x = this->get_speed_x();

Where get_speed_x() exists only on the fighter and is implemented as follows.
Code:
float Fighter::get_speed_x(){
  return 1.3;
}

Since this is a function exclusive to Fighter, a constant does not need to be created as a member in the Entity class. This will only work for constants however.



Last edited by Nate on Thu Jun 30, 2011 11:41 am; edited 2 times in total

Nate

Posts : 70
Join date : 2011-06-14

Back to top Go down

Cool Code: Members in virtual subclasses Empty Re: Cool Code: Members in virtual subclasses

Post  MaverickProgrammer Wed Jun 29, 2011 2:14 pm

I really liked your use of static functions to push back entities. It's like a singleton in the sense the class points to the same variable and handles them, but it completely avoids all difficulties like singletons.

MaverickProgrammer

Posts : 63
Join date : 2011-06-14

Back to top Go down

Cool Code: Members in virtual subclasses Empty Re: Cool Code: Members in virtual subclasses

Post  Nate Wed Jun 29, 2011 2:50 pm

It's the same general idea, but replacing constant members with functions outside of the base class.

Nate

Posts : 70
Join date : 2011-06-14

Back to top Go down

Cool Code: Members in virtual subclasses Empty Re: Cool Code: Members in virtual subclasses

Post  MaverickProgrammer Wed Jun 29, 2011 3:33 pm

Design-wise I learned it's better to have something slightly messy that works and clean it up as we go than sit around trying to create the "perfect architecture". Not to say we shouldn't stop to think about how we'll go about and code something, but over complicating things is a no-no.

MaverickProgrammer

Posts : 63
Join date : 2011-06-14

Back to top Go down

Cool Code: Members in virtual subclasses Empty Re: Cool Code: Members in virtual subclasses

Post  Nate Wed Jun 29, 2011 5:18 pm

This is a way of simplifying the base class and unload the responsibilities of remembering constants to children. I don't understand what you mean by complicating things.

It can even be taken a step further, allowing you to add members to subclasses of Entity. If every Entity had one virtual method acting as an entry point ( update() ), that can be called once every frame and it would be in charge of doing all its own operations. In doing this, it will also reference it's personal fields, rather than external systems assuming its base type and calling various functions and members on it.

Code:
main loop(){
for( all entities )
  one_entity->update()
}

Assuming all the following parameters are not in the Entity base class (only in the Fighter subclass), the following code will still work because they're within scope.
Code:
void Fighter::update(){  //implemented virtual function
  this->get_input()
  this->move(this->vx, this->vy)
  this->collide(my_exlusive_dimensions)
  this->render(my_animation.current_frame())
}


Nate

Posts : 70
Join date : 2011-06-14

Back to top Go down

Cool Code: Members in virtual subclasses Empty Re: Cool Code: Members in virtual subclasses

Post  MaverickProgrammer Thu Jun 30, 2011 7:21 pm

Ah! I see! That's actually pretty smart.

MaverickProgrammer

Posts : 63
Join date : 2011-06-14

Back to top Go down

Cool Code: Members in virtual subclasses Empty Re: Cool Code: Members in virtual subclasses

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum