0
\$\begingroup\$

I'm trying to create a program which involves the creation of several characters/entities.

At the moment I have a Base Entity class an Entities class and a Main class. I can handle one entity alright, however I am trying to create each one editable through a standard function call.

Main Class: http://pastebin.com/AjgEBSke 
Entity Class: http://pastebin.com/5y2wD3Qc
BaseEntity Class: http://pastebin.com/VfqRgeng

I feel a little confused on how to dynamically call and edit each specific character without limiting myself to lots of 'if' statements. I have tried to restart this project several times...

Basically I'm creating a private object in a class. That class will be used to talk to that private object, Save, load and edit etc.

Then in a seperate Entities class I create an instance of that object, this is where I think im getting caught up. As far as I know, I need to handle everything I want to do in that entity in this entity class.

I give the entity/object an ID, and a default name which I want to be able edit later. I also want to eventually be able to call that entity from other classes to 'draw' it, have it return the values I need to draw it, Name, body type etc (other variables in the object).

I have managed to interact with a specific instance of the object class by checking if the String is equal to an objects ID and then doing something, this however I feel is not dynamic and I'll just get into a bigger mess.

Ideally I think I want to be able to enter the object's class' instance name as a string and have it handle everything.

Sorry this has been rather long, if you know of any articles or information on how I'd learn to create dynamic entities to do what I need to do, that'd be great.

Im probably looking more for an explanation of the concept of what I'm trying to do as i've only just really sketched up this code. So this code might be a little pointless at the momemnt. Ive attempted it a few times, trying to set things up better each time, however ultimately fail when I try to make things behave dynamically. If i'm going about this completely the wrong way then please tell me as I'm still learning and don't want to get into the habit of doing things badly.

\$\endgroup\$
1
\$\begingroup\$

I think your question is just a little bit vague. Where exactly do you need lots of if-statements for?

That being said, it sounds like you are having trouble with object-oriented programming. I often rush into a project, only to restart it later, with a better specification (and by that I mean a worked out diagram with classes and methods).

I'm not sure if you want to be able to assign ID's at runtime or just do it at code-time (the word 'dynamically' suggests run-time, but the abundance of if-statements suggest code-time. For code-time ID's, you might want to consider making subclasses. For runtime, I wouldn't understand the problem.

This answer is a kind of a rant, but I hope it still is useful in one way or another.

\$\endgroup\$
1
\$\begingroup\$

You are looking for Polymorphism: http://www.adobe.com/devnet/actionscript/learning/oop-concepts/polymorphism-and-interfaces.html

First basic concept is that you don't need to identify every entities with IDs or Names as long as you plan to use that fields for visualize it or debug. Every field|variable|property you define in a class has is independent memory space for every instance you create.

After that using Polymorphism as explained in the article above, you could define multiple subclasses inheriting from your base clase, that behave different each one according of is methods implementation. Such methods that were defined before on it's base class.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.