Question about RemoveModel.Model

All topics about ZGameEditor goes here.

Moderator: Moderators

Post Reply
User avatar
Rado1
Posts: 775
Joined: Wed May 05, 2010 12:16 pm

Question about RemoveModel.Model

Post by Rado1 »

What's the meaning of RemoveModel.Model property? It seems that it removes the specified Model component from the project. Is it correct?

Remark: the current version of ZGE crashes if RemoveModel is called from outside the scope of a model (e.g. from ZApplication.OnUpdate) or from another model.
User avatar
VilleK
Site Admin
Posts: 2274
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

Re: Question about RemoveModel.Model

Post by VilleK »

Hi,

Yes if Model property is set, then only that model instance is removed from the current set of active models.

If the Model is not set, then there needs to be an active model, i.e. the RemoveModel command must either be inside a Model.OnUpdate (or other model list), or be called from such a list. That you get a crash is probably for that reason (I should add some better error checking here).
User avatar
Rado1
Posts: 775
Joined: Wed May 05, 2010 12:16 pm

Re: Question about RemoveModel.Model

Post by Rado1 »

I made some experiments and it seems that setting the Model property works only if the model instance was created by SpawnModel where SpawnStyle = Reference. In the case of spawning by cloning or by createModel() function, runtime access violation is reported.

Really strange behavior is achieved when the model was cloned and you run RemoveModel with Model set to that model. In that case, as I already reported, the Model component is removed from the project's component tree, not its instance from runtime. Just run the following project in preview, press RMB, save the project and reopen it again. It does not contain the Model1 component anymore.

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnLoaded>
    <SpawnModel Model="Model1"/>
  </OnLoaded>
  <OnUpdate>
    <KeyPress Comment="RMB" Keys="}">
      <OnPressed>
        <RemoveModel Model="Model1"/>
      </OnPressed>
    </KeyPress>
  </OnUpdate>
  <Content>
    <Model Name="Model1">
      <OnRender>
        <RenderSprite/>
      </OnRender>
    </Model>
  </Content>
</ZApplication>
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Re: Question about RemoveModel.Model

Post by Kjell »

Hi Rado1,
Rado1 wrote:I made some experiments and it seems that setting the Model property works only if the model instance was created by SpawnModel where SpawnStyle = Reference. In the case of spawning by cloning or by createModel() function, runtime access violation is reported.
It's meant to be used as follows ..

Code: Select all

model myClone = createModel(myModel); // Should have been called spawnClone() instead of createModel()
@RemoveModel(Model: myClone);
When you spawn a clone it ( obviously ) won't reside at the same memory address as the original .. so when you have the original selected in a RemoveModel component you end up deleting the original ( which you should never do ), not the clone.

K
User avatar
Rado1
Posts: 775
Joined: Wed May 05, 2010 12:16 pm

Re: Question about RemoveModel.Model

Post by Rado1 »

Hi Kjell,

your example works, but I tried similar example with global variable and it throws "Invalid pointer operation". Here it is (again activate RemoveModel by RMB click):

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnLoaded>
    <ZExpression Expression="MyModel = createModel(Model1);"/>
  </OnLoaded>
  <OnUpdate>
    <KeyPress Comment="RMB" Keys="}">
      <OnPressed>
        <ZExpression Expression="@RemoveModel(Model: MyModel);"/>
      </OnPressed>
    </KeyPress>
  </OnUpdate>
  <Content>
    <Model Name="Model1">
      <OnRender>
        <RenderSprite/>
      </OnRender>
    </Model>
    <Variable Name="MyModel" Type="3"/>
  </Content>
</ZApplication>
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Re: Question about RemoveModel.Model

Post by Kjell »

Hi Rado1,
Rado1 wrote:I tried similar example with global variable and it throws "Invalid pointer operation".
Your KeyPress component has a RepeatDelay of 0. So unless you click at the "speed of light", you end up executing RemoveModel multiple times / over multiple frames .. and since you can only destroy a clone once ( obviously ), any consecutive time RemoveModel is executed you get a "invalid pointer operation" error.

K
User avatar
Rado1
Posts: 775
Joined: Wed May 05, 2010 12:16 pm

Re: Question about RemoveModel.Model

Post by Rado1 »

Kjell wrote:Your KeyPress component has a RepeatDelay of 0.
Stupid me, I did not notice. Thanks.
Post Reply