Page 1 of 1

Variables of type Component

Posted: Tue Nov 24, 2015 12:29 am
by Rado1
Problem: usually, when creating games with several levels, I create the "engine" that executes the game logic, and then particular levels are specified by data; possibly loaded from files. In my latest project I came with requirement to specify levels also by several functions that influence their behavior. Of course, usage an expression with many ifs or switch/case statements checking the current level would be too cumbersome. Instead, it would be nice if it is possible to use the same "engine" with pluggable components.

Suggested solution: to allow values/variables of type component and assignments to CallComponent.Component property. In addition, expressions like this should also be allowed:

Code: Select all

component var1;
var1 = <component name>;
@CallComponent(Cmponent: var1);
"Engine extension points" would be defined as CallComponent components and definition of level just assigned appropriate components to CallComponent.Component properties.

Note: Other possible solutions could come from C with pointers to functions, or from other languages supporting lambda calculus or closures. But these features would be too "hardcore" for ZGE, I think.

Re: Variables of type Component

Posted: Tue Nov 24, 2015 7:41 pm
by VilleK
Hi,

This behavior can be done (i.e. hacked) already using reinterpret_cast. See below. But I agree having a Component type for variables would be useful.

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnLoaded>
    <ZExpression>
      <Expression>
<![CDATA[xptr p;

p=reinterpret_cast<xptr>(Comp1);
@CallComponent(Component : p);

p=reinterpret_cast<xptr>(Comp2);
@CallComponent(Component : p);]]>
      </Expression>
    </ZExpression>
  </OnLoaded>
  <Content>
    <ZExpression Name="Comp1" Expression="trace("Comp1 called");"/>
    <ZExpression Name="Comp2" Expression="trace("Comp2 called");"/>
  </Content>
</ZApplication>

Re: Variables of type Component

Posted: Wed Nov 25, 2015 4:27 pm
by Rado1
Yes, the magic of reinterpret_cast is amazing. Thanks Ville.

Is reinterpret_cast explained somewhere, if not, could you please explain it? I know reinterpret_cast from C++, is it similar? Also I did not know that CallComponent.Component property is of type xptr; interesting.

Re: Variables of type Component

Posted: Wed Nov 25, 2015 8:36 pm
by VilleK
It is basically a way to bypass the type checking. It was added to C++ to prevent careless casts between primitive types. Casts can be dangerous but the idea is that if you see reinterpret_cast in the code then the cast has been thought through and really is warranted (because typing "reinterpret_cast" takes longer than just simple C-style cast (int) etc). See "named casts" 11.5.2 in C++ Programming Language book by Stroustrup.

I wanted ZGE scripting to be type safe but in practice this kind of casting has its uses (especially when dealing with external libraries etc). First I added it because Kjell wanted a way to treat a float as an int, but it has proven useful in other cases too.

The actual internal type of CallComponent.Component is Component but since xptr is of pointer type it allows assignment to/from it.

General guideline: don't overuse reinterpret_cast but know when and how to use it.

Re: Variables of type Component

Posted: Thu Nov 26, 2015 7:02 am
by Rado1
Thanks Ville for explanation.