View previous topic :: View next topic |
Author |
Message |
ViolentAJ
Joined: 05 Feb 2009 Posts: 56
|
Posted: Sat Apr 18, 2009 9:39 pm Post subject: Array of objects |
|
|
I'm sorry for posting here continuously, but there are so few resources. I try googling, but to little avail.
I'm trying to make an array of Enemy objects for a simple RPG battle that I'm making.
This is my game client so far:
Code: |
module GameClient;
import std.stdio; //I must remember the import declarations go under module
import std.cstream;
import std.conv;
import Character;
import Enemy;
import Mitsuki;
bool playing = true;
bool valid = false;
static Mitsuki mitsuki = new Mitsuki();
static Enemy ruffian = new Enemy("Ruffian");
static Enemy miscreant = new Enemy("Miscreant");
//An array holding the enemies
static Enemy[] enemies = {ruffian, miscreant};
void main(string[] args){
//Do loop to handle gameplay. of course, we will play at least once
do {
playing = false;
//playRound();
} while(playing);
}
void playRound(){
bool correct = false;
writefln("\n=*=*New Round*=*=\n");
mitsuki.printStats();
for(int i = 0; i < enemies.length; i++){
enemies[i].printStats();
}
do {
writefln("Pick your target: ");
for(int i = 0; i < enemies.length; i++){
writefln("\t%d) %s", i, enemies[i].getName());
}
writef(">");
din.readLine();
} while(!correct);
}
|
When I try to compile, I get the following error:
C:\WorkingInD\Characters\GameClient.d(19): Error: a struct is not a valid initializer for a Enemy[] |
|
Back to top |
|
|
ViolentAJ
Joined: 05 Feb 2009 Posts: 56
|
Posted: Sun Apr 19, 2009 12:42 am Post subject: |
|
|
Ok. This is my new code, and I get the following errors:
Code: |
module GameClient;
import std.stdio; //I must remember the import declarations go under module
import std.cstream;
import std.conv;
import Character;
import Enemy;
import Mitsuki;
void main(string[] args){
bool playing = true;
bool valid = false;
Mitsuki mitsuki = new Mitsuki();
//An array holding the enemies
Enemy[] enemies = new Enemy[];
//Do loop to handle gameplay. of course, we will play at least once
do {
playRound(enemies, mitsuki);
writefln("Game over. Input 'quit' to quit, or any other string to play again.");
writef(">");
if(din.readLine() == "quit"){
playing = false;
}
} while(playing);
}
void playRound(Enemy[] enemyArray, Character playerChar){
bool correct = false;
writefln("\n=*=*New Round*=*=\n");
playerChar.printStats();
for(int i = 0; i < enemyArray.length; i++){
enemyArray[i].printStats();
}
do {
writefln("Pick your target: ");
for(int i = 0; i < enemyArray.length; i++){
writefln("\t%d) %s", i, enemyArray[i].getName());
}
writef(">");
din.readLine();
} while(!correct);
}
|
These are the errors:
Code: |
C:\WorkingInD\Characters\GameClient.d(18): Error: new can only create structs, dynamic arrays or class objects, not Enemy[]'s
C:\WorkingInD\Characters\GameClient.d(18): Error: cannot implicitly convert expression (new Enemy[]) of type Enemy[]* to Enemy[]
|
|
|
Back to top |
|
|
doob
Joined: 06 Jan 2007 Posts: 367
|
Posted: Sun Apr 19, 2009 3:29 am Post subject: |
|
|
You have to set how long the new array should be:
Code: | Enemy[] enemies = new Enemy[3]; |
|
|
Back to top |
|
|
csauls
Joined: 27 Mar 2004 Posts: 278
|
Posted: Mon Apr 20, 2009 2:38 am Post subject: |
|
|
Or you can just declare the variable with no initializer at all. Dynamic array typed variables are initialized by default to a new zero-length array.
As a side note, if you want to explicitly disable that default you can do say by specifying void as an initializer:
Of course, do this with care and only in those cases you know the default array absolutely would not have been used anyway. (Such as temporaries that only hold foreign slices during certain logic branches.) _________________ Chris Nicholson-Sauls |
|
Back to top |
|
|
ViolentAJ
Joined: 05 Feb 2009 Posts: 56
|
Posted: Tue Apr 21, 2009 11:28 pm Post subject: |
|
|
Thank you.
I'll try to learn more D over the summer. I think that I did a bit for this project, now I just have to work on my powerpoint. Thanks everyone.
I just have one more question. I'm trying to tack an int on the end of a dynamic array. I looked at the array documentation, and I thought that this would work.
Code: |
this.stackArray.length++;
this.stackArray[this.stackArray.length - 1] = item;
|
However, I get an error saying that stackArray.length is not an l value.
I also tried this:
Code: |
int[] temp = [item];
this.stackArray[] ~= temp;
|
Compiler got mad and said:
StackClass.d(22): Error: slice expression this.stackArray[] is not a modifiable lvalue
Command C:\DProgramming\dsss-0.78-x86-windows\bin\rebuild.exe returned with code 1, aborting. |
|
Back to top |
|
|
doob
Joined: 06 Jan 2007 Posts: 367
|
Posted: Wed Apr 22, 2009 2:03 am Post subject: |
|
|
For some reason you cannot increment properties. Either you have to:
Code: | this.stackArray.length = this.stackArray.length + 1;
this.stackArray[this.stackArray.length - 1] = item; | or just use the append operator like this:
Code: | this.stackArray ~= item; |
|
|
Back to top |
|
|
ViolentAJ
Joined: 05 Feb 2009 Posts: 56
|
Posted: Thu Apr 23, 2009 2:42 am Post subject: |
|
|
Thanks. It worked out. |
|
Back to top |
|
|
|