Game Development Community

RTSUnitModifier

by Swarthmore College (#0007) · in RTS Starter Kit · 07/02/2007 (11:22 am) · 2 replies

The RTSUnitModifier is a little confusing. It seems as though, if you say %unit.addModifier(%someModifier.getId()), it works, but then later try to add the same modifier again, the second modifier has no effect. Is this because these are datablocks? Is there a cleaner way to do this than just creating a new datablock each time you want to stack modifiers?

Thanks

#1
07/06/2007 (8:21 am)
So, to clarify this, now that I've realized modifiers don't stack and it is because they are datablocks...

Problem:
I want to be able to modify some of the stats of my units as though the stats represented HP. Also, I have changed the bars above the units' heads to reflect these stats. To implement this, I made some default modifiers and assigned them depending on what kind of unit it was. Then, later, I tried to change these modifiers with:
function RTSUnit::changeStat(%this, %stat, %amount) {
  %current = %this.getNetModifier().getFieldValue(%stat);
  %this.getNetModifier().setFieldValue(%stat, %amount+%current);
}
But, because I was changing this on the server, the client side RTSUnit never changed, and thus, the guiRTSTSCtrl is redrawn, the stat bars don't change.

Possible Solutions:
1) (Simple. Yet horrible.) Make datablocks. Lots of datablocks. So that, when I want to change stats, I just add a datablock that increases the stat by one. Then, when I want to change it again, I add another datablock that increases the stat by one. I have 3 stats, which go up to 10. I'd also like to do fractions of these numbers.

2) (Kind of hacky, but probably best.) Update the client's corresponding RTSUnit when I update the server side one. I would have done this already, except that when I call resolveGhostID torque segfaults and when I call resolveGhost it doesn't exist. It doesn't have a console function, I could try making one, which is what I will do after posting this.
EDIT: quick note, it turns out that resolveGhostID just calls resolveGhost. I'm going to try and find out why it segfaults...

3) (Cool, but I don't really know how.) Do most of this in code. I tried to add an update function and set the mask bits and set the RTSUnit as dirty but I don't really understand all of that, and whatever I did didn't work. I have not tried actually adding the stats I want by copying ShapeBases' mDamage stat as an example (this is the functionality I want, but I think it would be very complicated).

Any suggestions would be greatly appreciated.
Thanks
#2
07/06/2007 (1:14 pm)
Final solution:

function RTSUnit::changeStat(%this, %stat, %amount) {
  %current = %this.getNetModifier().getFieldValue(%stat);
  %sum = %current + %amount;
  if (%sum >= 10) {
    %sum = 10;
  }
  %this.getNetModifier().setFieldValue(%stat, %sum);
  // client stuff
  %client = %this.client;
  commandToClient(%client, 'ChangeStat', %client.getGhostID(%this), %stat, %amount);
  if (%this.getNetModifier().getFieldValue(enthusiasm) <= 0 ||
      %this.getNetModifier().getFieldValue(fear) >= 10) {
    %this.applyDamage(1000000);
  }
}

and on the client side:
function clientCmdChangeStat(%unit, %stat, %amount) {
  %clientUnit = ServerConnection.resolveGhostID(%unit);
  %clientUnit.changeStat(%stat, %amount);
}

Turns out that resolveGhostID was segfaulting because the ambiguously defined ServerConnection needs to call it. Who knew?