Variables of type Component

If there is something important you think is missing in the current version of ZGameEditor then you can post a feature request here!

Moderator: Moderators

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

Variables of type Component

Post 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.
Attachments
example.zgeproj
(894 Bytes) Downloaded 777 times
User avatar
VilleK
Site Admin
Posts: 2356
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

Re: Variables of type Component

Post 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>
User avatar
Rado1
Posts: 775
Joined: Wed May 05, 2010 12:16 pm

Re: Variables of type Component

Post 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.
User avatar
VilleK
Site Admin
Posts: 2356
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

Re: Variables of type Component

Post 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.
User avatar
Rado1
Posts: 775
Joined: Wed May 05, 2010 12:16 pm

Re: Variables of type Component

Post by Rado1 »

Thanks Ville for explanation.
Post Reply