Page 1 of 1

Question about CurrentModel, variables and functions

Posted: Wed Jul 11, 2018 9:23 am
by Ats
Before starting to add a lot of content to my game, I'm trying to simplify a few things.
Therefore, instead of having:

Code: Select all

FlyingEnemy:Model
Definitions
  FlyingEnemyLife:Byte
OnCollision
  CurrentModel.FlyingEnemyLife--;
  if (!CurrentModel.FlyingEnemyLife)  @RemoveModel();
per enemy model, I would like to call a function such as:

Code: Select all

void TestCollision(model m)
{
  m.life--;
  if (!life) @RemoveModel(Model:m);
}
(it's a very simplified example, there's much more things going on. That's why I want to use that method)

So how can I do this?

The life variable can't be set in each enemy models with the same name.
So I tried to pass the needed var in the function:

Code: Select all

OnCollision
  TestCollision(CurrentModel, CurrentModel.FlyingEnemyLife);
  
...

void TestCollision(model m, byte life) { life--; }
but this only changes life in TestCollision, it's not changing FlyingEnemyLife.

I was even thinking of having only one enemy model, but that would introduce a lot of code to setup the collision boxes and to display the correct meshes. And I wouldn't need the TestCollision function since everything would be in one model...

Thanks for your help !

Re: Question about CurrentModel, variables and functions

Posted: Wed Jul 11, 2018 10:45 am
by Ats
I managed to find a method that works:

Code: Select all

byte TestCollision(model m, byte life)
{
  life--;
  if (!life) @RemoveModel(Model:m);
  return life;
}

...
FlyingEnemy:Model
Definitions
  FlyingEnemyLife:Byte
OnCollision
  CurrentModel.FlyingEnemyLife = TestCollision(CurrentModel, CurrentModel.FlyingEnemyLife);
Is that how it's done?

Re: Question about CurrentModel, variables and functions

Posted: Wed Jul 11, 2018 11:10 am
by rrTea
I'd use BaseModel for that, and just have something like EnemyHP in the Definitions of the BaseModel which is used for all enemies (this'd also make it easy to put the code for removal when HP is 0 in the BaseModel etc), not sure how practical that'd be in your project.

Re: Question about CurrentModel, variables and functions

Posted: Thu Jul 12, 2018 10:59 am
by Kjell
Hi Ats,

If you want to modify a variable or property from a function you have to make sure it's passed as a reference. For some types this happens automatically, but for basic types ( byte, int, float, string ) you need to add "ref" to the argument otherwise the variable gets copied and is local to the function. So for your example you probably want ...

Code: Select all

void TestCollision(model m, ref byte life){life--;}
TestCollision(CurrentModel, CurrentModel.FlyingEnemyLife);
Anyway, as rrTea said .. you might want to use a BaseModel instead :wink:

K