Page 1 of 1

Question about RemoveModel.Model

Posted: Thu Dec 31, 2015 6:31 pm
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.

Re: Question about RemoveModel.Model

Posted: Fri Jan 01, 2016 4:14 pm
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).

Re: Question about RemoveModel.Model

Posted: Fri Jan 01, 2016 7:39 pm
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>

Re: Question about RemoveModel.Model

Posted: Fri Jan 01, 2016 9:33 pm
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

Re: Question about RemoveModel.Model

Posted: Sat Jan 02, 2016 12:47 pm
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>

Re: Question about RemoveModel.Model

Posted: Sat Jan 02, 2016 12:59 pm
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

Re: Question about RemoveModel.Model

Posted: Sat Jan 02, 2016 2:13 pm
by Rado1
Kjell wrote:Your KeyPress component has a RepeatDelay of 0.
Stupid me, I did not notice. Thanks.