FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Array of objects

 
Post new topic   Reply to topic     Forum Index -> General
View previous topic :: View next topic  
Author Message
ViolentAJ



Joined: 05 Feb 2009
Posts: 56

PostPosted: Sat Apr 18, 2009 9:39 pm    Post subject: Array of objects Reply with quote

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
View user's profile Send private message
ViolentAJ



Joined: 05 Feb 2009
Posts: 56

PostPosted: Sun Apr 19, 2009 12:42 am    Post subject: Reply with quote

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
View user's profile Send private message
doob



Joined: 06 Jan 2007
Posts: 367

PostPosted: Sun Apr 19, 2009 3:29 am    Post subject: Reply with quote

You have to set how long the new array should be:
Code:
Enemy[] enemies = new Enemy[3];
Back to top
View user's profile Send private message
csauls



Joined: 27 Mar 2004
Posts: 278

PostPosted: Mon Apr 20, 2009 2:38 am    Post subject: Reply with quote

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:
Code:
Foo[] var = void ;


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
View user's profile Send private message AIM Address Yahoo Messenger
ViolentAJ



Joined: 05 Feb 2009
Posts: 56

PostPosted: Tue Apr 21, 2009 11:28 pm    Post subject: Reply with quote

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
View user's profile Send private message
doob



Joined: 06 Jan 2007
Posts: 367

PostPosted: Wed Apr 22, 2009 2:03 am    Post subject: Reply with quote

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
View user's profile Send private message
ViolentAJ



Joined: 05 Feb 2009
Posts: 56

PostPosted: Thu Apr 23, 2009 2:42 am    Post subject: Reply with quote

Thanks. It worked out.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> General All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group