Question about CurrentModel, variables and functions

All topics about ZGameEditor goes here.

Moderator: Moderators

Post Reply
User avatar
Ats
Posts: 603
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Question about CurrentModel, variables and functions

Post 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 !
Last edited by Ats on Wed Jul 11, 2018 10:44 am, edited 2 times in total.
User avatar
Ats
Posts: 603
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Question about CurrentModel, variables and functions

Post 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?
Last edited by Ats on Wed Jul 11, 2018 12:08 pm, edited 1 time in total.
User avatar
rrTea
Posts: 475
Joined: Sat Feb 15, 2014 9:54 am

Re: Question about CurrentModel, variables and functions

Post 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.
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Re: Question about CurrentModel, variables and functions

Post 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
Post Reply