Jump to content

TipsTricks


photo

Recommended Posts

Posted

Java style derived classes:

class Foo {
  void foo() { printf("base\n"); }
};
Foo f = new Foo() {
  void foo() { printf("derived\n"); }
};
f.foo();

Fast type based branching:
switch(typeid(variable)) {
  case typeid(int): printf("int\n"); break;
  case typeid(float): printf("float\n"); break;
  case typeid(string): printf("string\n"); break;
}

Function identifiers:
int id = get_function("sin");
call(id,1.0f);
class Foo {
  void foo() { }
};
int id = get_function("Foo::foo");
Foo foo = new Foo();
foo.call(id);

Access to function return value elements and swizzles:
vec3 foo() { return vec3(1.0f,2.0f,3.0f); }
printf("%f %f\n",foo().x,foo()[0]);
printf("%f %f\n",translate(1.0f,2.0f,3.0f).m23,translate(1.0f,2.0f,3.0f)[14]);

String based operator "new":
File file = new("File");
LightOmni light = new("LightOmni",vec4_one,vec3_one);

Delete all internal elements and clear the array:
Foo array[] = ( new Foo() );
array.delete();

Append an element into specified position:
int array[] = ( 0,1 );
array.append(1,13);            // 0,13,1
array.append(3,13);            // 0,13,1,13

Find a map element:
int array[] = ( -1:-1, 0:0, 1:1 );
array.find(-1);                // returns -1 and it's correct
array.find(2);                // returns -1 and it's wrong
array.find(2,-13);            // returns -13 as a workarround

Safe array element getter.
int array[] = ( -1:-1, 0:0, 1:1 );
array.check(-1,-13);            // returns -1
array.check(-2,-13);            // returns -13

Class casting:
class Foo {
    void foo() { }
};
int foo = new Foo();
Foo(foo).foo();                // Manually cast to type
//foo.foo();                // incorrect because foo is int

Variable number arguments of call and thread methods:
int args[] = ("%s\n","hello world");
call("printf",args);            // prints "hello world"

Ref-count based memory management for external classes:
File file = class_manage(new File());
file = 0;                // file will be deleted when ref count is zero

Functions inlining:
void get() { return a; }        // automatic functions inlining
void set(int b) { a = b; }

User class operator[] overloading:
class Foo {
    mat4 m;
    void __set__(Foo f,int index,int value) {
        f.m[index] = value;
    }
    int __get__(Foo f,int index) {
        return f.m[index];
    }
};
Foo f = new Foo();
f[0] = 10;
f[1] = f[0];
printf("%g %g\n",f[0],f[1]);        // prints "10 10"

Extended swizzles:
vec4 v = vec4(10.0f,11.0f,12.0f,13.0f);
printf("%s\n",typeinfo(v.xyzw));    // prints "10 11 12 13"
printf("%s\n",typeinfo(v.10zw));    // prints "1 0 12 13"
printf("%s\n",typeinfo(v.xy10));    // prints "10 11 1 0"

New typeid argument type:
int a = 1;
if(typeid(a) == typeid(int)) printf("int\n");

New classid statement:
class Foo { };
if(classid(new Foo()) == classid(Foo)) printf("Foo\n");

Check class inheritance:
File file = new File();
is_base_class("File",file);        // return 1 bcz File is File
is_base_class("Stream",file);        // return 1 bcz Stream is a base class for File
is_base_class("Socket",file);        // return 0 bcz Socket is not a base class for File
New syntax of is_base_class() function:
is_base_class(classid(File),file);    // return 1 bcz File is File
is_base_class(classid(Stream),file);    // return 1 bcz Stream is a base class for File
is_base_class(classid(Socket),file);    // return 0 bcz Socket is not a base class for File

Super keyword:
class Foo {
  void foo() { printf("base\n"); }
};
class Bar {
  void foo() { printf("derived\n"); }
  void bar() {
    super.foo();   // prints "base"
    this.foo();    // prints "derived"
  }
};
Bar bar = new Bar();
bar.bar();

Interface class support and virtual function declaration:
class Interface {
  void update() = 0;
};
class Foo {
  void foo() = 0;
};
class Bar : Foo, Interface {
  void update() { printf("Bar::update(): called\n"); }
};

class Baz : Interface {
  void update() { printf("Baz::update(): called\n"); }
};

Interface interfaces[0];
interfaces.append(new Bar());
interfaces.append(new Baz());
foreach(Interface i; interfaces) {
  i.update();
}
// Bar::update(): called
// Baz::update(): called

The current thread identifier:
printf("%d\n",get_thread());

New array functions:
  int a[] = (1,2,3,4);
  int b[] = (5,6,7,8);
  int c[] = (9,10,11,12);
  int d[] = (1:1.0,2:2.0,3:3.0);
  a.append(b,1,2);
  a.remove(0,6);
  a.append(c,1,2);
  printf("%s\n",a.typeinfo());
  printf("%s\n",b.typeinfo());
  printf("%s\n",c.typeinfo());
  printf("%s\n",d.typeinfo());

Fast class based branching:
class Foo { };
class Bar { };
void foo(int a) {
  switch(classid(a)) {
    case classid(Foo): printf("Foo\n"); break;
    case classid(Bar): printf("Bar\n"); break;
    case classid(File): printf("File\n"); break;
  }
}
foo(new Foo());
foo(new Bar());
foo(new File());
printf("%d %d\n",classid(new Foo()),classid(Foo));
printf("%d %d\n",classid(new Bar()),classid(Bar));
printf("%d %d\n",classid(new File()),classid(File));

Variable referencing:
class Foo {
  int v;
};
void foo(int &v) {
  if(v) printf("%d\n",v);
  v = 13;
}
{
  int v = 0;
  foo(v);
  foo(v);
}
{
  Foo f = new Foo();
  foo(f.v);
  foo(f.v);
}
{
  int v[] = (0);
  foo(v[0]);
  foo(v[0]);
}
{
  Foo f[] = (new Foo());
  foo(f[0].v);
  foo(f[0].v);
}
Posted

This is our internal article with scripting TipsTricks.

PS: "super" keyword and interface classes will be available in the next SDK update.

  • 4 weeks later...
Posted
class Bar : Foo, Interface {
  void update() { printf("Bar::update(): called\n"); }
};

Is multiple inheritance supported by now? The documentation says it is not.

 

 

There are some peculiarities of the class inheritance in UnigineScript:

Posted

Manuel,

 

 

Is multiple inheritance supported by now? The documentation says it is not.

 

 

Yep, it's completely supported now. Documentation will be updated soon.

Posted

Manuel,

 

 

 

 

Yep, it's completely supported now. Documentation will be updated soon.

Brilliant! Thanks. :)

Posted

Thats realy awesome stuff! :)
Looking forward to test it this evening!

 

  • 3 months later...
Posted

Alexander, please include details about reflection and attributes, this is the awesomest power of unigine script)

now we are creating no-headache automatic gui generation based on this features.

  • 9 months later...
Posted

Hello,

 

Maybe someone can use this too. This will slightly modify the extern_info.h in such a way, that everything should work with an IDE.

Mainly the engine.* commands are mapped to a class with sub classes, this way you have all methods.

#!/bin/python

# General values
subclass = {}
shortones = []

# Normal unigne classes
default = ''

# Special engine class
output = 'class engine {\n'

# Open file and read all lines
with open('extern_info.h') as f:
    content = f.readlines()

# Check each line and save it to the right place
for line in content:
    # For lines which contains engine. we have to do the magic
    if "engine." in line:

        # Split line
        sub_command = line.split(".")
        
        # There are two type of lines, lines with sub classes and normal methods
        if len(sub_command) >=3:
            # Get sub class
            sub = sub_command[1]

            # Only append if not already known
            if sub not in subclass:
                subclass[sub] = []
                commandMap = subclass[sub]
                commandMap.append(sub_command[0].replace('engine','') +  '' +sub_command[2])
            else:
                commandMap = subclass[sub]
                commandMap.append(sub_command[0].replace('engine','') +  '' +sub_command[2])
        else:
            shortones.append(sub_command[1])
    else:
        default += line

# Add normal methods
for method in shortones:
    output += '\t' + method

# Create engine class with all sub classes and methods
for sub in subclass:
    # Create an instance of each sub class
    output += '\t' + sub + ' ' + sub + ';\n'

    # Define class
    output += '\tclass ' + sub + ' {\n'

    # Add methods
    for entry in subclass[sub]:
        output += '\t\t' + entry

    output += '\t};\n'

output += '}'

# Add the default classes
output = default + output

# Write and save
f = open('extern.h', 'w')
f.write(output)
f.close()

post-31-0-42314100-1417379440_thumb.png

post-31-0-41160500-1417379450_thumb.png

×
×
  • Create New...