SCO said:
You understand the concept of references and copy right?
I understand the concept, although sometimes I don't remember/confound which command does copy, and which ones does only reference the same item.
SCO said:
Why would you want to copy a object that you have already initialized into the list?
Because I might have several tanks, identical ones, except for the starting position and current hp. So I create a list from a template, change the values for x and y (position in the grid) and add them to the unit_list list. So when I access unit_list[0], it's the first tank, unit_list[1] is the same as in the same model, but not the same unit.
I'm not sure that's the "right" or best way, but that's the way I could think of.
SCO said:
This is part of what i meant about stateful and "organizing your functions":
Your functions and subfunctions should always leave the object in a consistent state, and that is much harder if there is lots of state. You might find that your logic will not work with a inconsistent state, and be tempted to add hacks. Avoid it if you can - go back and find the code that did it. Let me give you a example of "inconsistent" state:
Code:
//invented syntax
int x, y
rotate(int degrees):-
x = x cos degrees + y sin degrees
y = − x sin degrees + y cos degrees
You should see the error quickly. Not so surprisingly, the way to avoid inconsistent state in this case, is to make a temporary variable and make sure at the end of the function that the x and y are in consistent states. Tests/asserts are useful to detect this, but they are not part of your program the output should never be tested at runtime except if you're doing military computers that have to check about cosmic rays and shit. The input is permissible, at the boundary of your code, if you're using shit libraries.
Not coincidently, code like the code above is one of the reasons that recent languages "copy" primitives into functions instead of using pointers.
dosomething(&x) might not work so good if it uses x inside the function and the argument. It doesn't avoid the class of errors like the example shows, but it is not quite as mysterious.
Most of my functions leave the original list intact, except when I want them to modify, which is the case of the move(ind) function. This function gets an unit, by it's index (ind) found and returned by other function, and modifies its x and y values.
I still have to add more units, right now there's only tank. But it's quite easy to do so, since I just have to follow the same positions in the new unit template and all the functions will work with them.
So in the end, I have a few list templates:
EDIT: When I say attributes in a list, I don't mean attributes in a programming sense, I mean unit attributes, like the movement, range, hp, etc. The "attributes" in the list tank are variables. I will quote "attributes" when they refer to game attributes, not programming ones.
tank[several "attributes"]
future_units[several "attributes" in the same order to conserve indexes]
A working list to use on my functions:
unit_list[tank, tank, future_units, etc]["attribute" index]
And inside tank1, tank2, I have mostly the same values for the "attributes", except for x, y, current hp, has moved this turn, has attacked this turn.
This way works fine, because the templates can have the same name, and be the same, and when I use unit_list.append(tank), the program adds a new list to unit_list, NOT just the references. Which was my problem with classes. I just added references to the same object, and I don't know how to create multiple objects with the same name in a list, I don't even know if it's possible.
You might ask, "why not just call your second tank "tank 2"?"
Well, because I can simply create (as I have done) a function that skipping the error control, messages, and menu opening to choose which unit parts, says simply:
unit_list.append(tank)
graphical part here
To use tank2, instead of tank, well... I don't know how to do that. unit_list does NOT, as far as I know store the template list name, so there's no way to check for a "tank2" object, so that it can impede the program of adding it again and thus creating the 2 references 1 object problem.
Also, I don't think a loop would work, since I can't make:
variablename_number = blah blah blah
while changing just "number" in the variable name. And if I could, I would still need to bypass the "checking if there's already a variablename_number" in the unit_list.
EDIT: Actually, with objects, I think I can create a loop which changes one attribute, so it might be possible, something like tank.number.changing_variable = template
But then I would have to acces them by writing tank.number.1.attribute.
Which is a lot of writing.
I could create an object with an extra attribute, that tells the number of the unit, and a function that checks if there's already an object with that number in the unit_list list. But by fucking jesus, that's a lot of work, and I might end up running into some problem I can't foresee and can't deal with later.
I hope what I wrote is clear, communication is hard enough without one of the parts not understanding exactly what it's talking about.