Please login or register.

Login with username, password and session length
Advanced search  

News:

This forum provides RSS feed. To query recent posts use this url. More...


Author Topic: Nesting Objects  (Read 6053 times)

0 Members and 1 Guest are viewing this topic.

creatorbri

  • Occasional poster
  • **
  • Karma: 0
  • Offline Offline
  • Posts: 61
  • I'm a llama!
    • View Profile
Nesting Objects
« on: December 05, 2003, 11:03:59 PM »

I have a coding question. I want to be able to store objects within objects. I attempted to use nested object notation like this:

Code: [Select]
self.cExamine = new Object ("scripts\comment_object.script");
self.cExamine.Add ("It's an old chest.", null);

But I get an error on that second line. Is there some way to do this without having to separate them like

Code: [Select]
self.cExamine = new Object ("scripts\comment_object.script");
var Temp = self.cExamine;
Temp.Add ("It's an old chest.", null);

Since it doesn't complain about nesting objects within objects, it seems like this should work. Suggestions?
Logged

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re:Nesting Objects
« Reply #1 on: December 06, 2003, 10:09:02 AM »

Multiple member references in a single expression are not supported. You'll have to separate them like in your second example.
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

creatorbri

  • Occasional poster
  • **
  • Karma: 0
  • Offline Offline
  • Posts: 61
  • I'm a llama!
    • View Profile
Re:Nesting Objects
« Reply #2 on: December 06, 2003, 03:50:50 PM »

Basically I'm trying to reduce the lines of code I have to type to accomplish various tasks, to make development simpler as we put together the room logic for our game. In fact I got that example down to one line by creating a function which returns a Comment object. The following example shows how this object could be used in a game:

Code: [Select]
// ====== Initialize Comments ======
// This part happens within the Init script(s) for Sammy.

// These comments will be said if Sammy is looked at
self.cLook = newComment ("That's my archnemesis Sammy the Pirate! He's got an ill-favored look.", "actor_sammy_look1.ogg");
var Temp1 = self.cLook;
Temp1.Add ("It's mean ol' Sammy the Pirate.", "actor_sammy_look2.ogg");

// These are comments that Sammy himself will make.
self.cGrowl = newComment ("Arrgh!", "sammy_growl1");
var Temp2 = self.cGrowl;
Temp2.Add ("Grrrr!", "sammy_growl2");
// ============================


// ====== Event: Look At Sammy ======
// - This occurs in the event handler part of the
//   Sammy script(s).

// Have the actor comment on Sammy
Temp1 = self.cLook;
Temp1.Say (actor);

// Have sammy respond with a growl
Temp2 = self.cGrowl;
Temp2.Say (self);
// ============================
The purpose of creating this object is to be able to add any number of comments to one object, as in the example above, and then loop through them as the Say method is called. But I'm still stuck with the same problem.

I should probably ask whether objects are passed by reference or value. But I also need advice. Can anyone help me clean up this code, so it doesn't take so much extraneous junk?

Edit: Correction -- in most cases, its actually the object being talked about that stores the comment object, not the speaker; unless the speaker is just talking in response to something. I've corrected the code sample above accordingly.

In case its not obvious, the whole reason for doing this is to separate the comments by their entities, rather than hard coding them into the game logic. If I add an entity to a room, I shouldn't have to modify any code but the script for that entity. There's probably a simpler way to do this -- there usually is -- but this is the best I've been able to think of yet.
« Last Edit: December 06, 2003, 08:30:53 PM by irbrian »
Logged

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re:Nesting Objects
« Reply #3 on: December 06, 2003, 10:22:38 PM »

In case its not obvious, the whole reason for doing this is to separate the comments by their entities, rather than hard coding them into the game logic. If I add an entity to a room, I shouldn't have to modify any code but the script for that entity.

I don't think I get it. It usually IS done that way. If you look at the wme demo project, there's typically one script attached to an entity, containing all the event handlers relevant to that entity, such as

on "look" ...

So the code is tightly tied to its object without affecting the general game logic. Or am I missing something here? (I am, probably...)

Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

creatorbri

  • Occasional poster
  • **
  • Karma: 0
  • Offline Offline
  • Posts: 61
  • I'm a llama!
    • View Profile
Re:Nesting Objects
« Reply #4 on: December 07, 2003, 12:43:16 AM »

Sorry, you're right about the talking being part of the objects in the demo, but the part of the equation that makes the difference is the Comment Lists. I want to be able to do funky things with comment list objects, such as iterating through a list either once or indefinitely each time the Say method is called, "plugging in" sound files, and using comment dependencies (in order for comment X to be played, comment X first checks that comment Y has been played). Stuff like that. So I'm wanting the code that jumps semi-intelligently through a list of comments to be self contained.

Originally I was going to put the code in the actor's methods, and store the lists as arrays inside the various entities' scripts; but I thought having a separate Comment object and preparing and storing the object inside the entity's script would keep things slightly more modular. I may have to just rethink the whole thing. :(
Logged

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re:Nesting Objects
« Reply #5 on: December 07, 2003, 07:17:23 PM »

Originally I was going to put the code in the actor's methods, and store the lists as arrays inside the various entities' scripts; but I thought having a separate Comment object and preparing and storing the object inside the entity's script would keep things slightly more modular. I may have to just rethink the whole thing. :(

Hmm, how about having the comment object wrapped inside the entity methods? I mean, the entity would have a GetNextComment() method and that method would internally use the comment object. That way all the "messy" code would stay hidden inside the entity.
« Last Edit: December 07, 2003, 07:19:58 PM by Mnemonic »
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave
 

Page created in 0.037 seconds with 21 queries.