• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
jrick

Crash using asBEHAVE_IMPLICIT_VALUE_CAST

7 posts in this topic

Hi,

I got a problem with implicit value cast.

I register things like that :
template<class T>
class Param {
public:
  Param(const T _value) : value_(_value), ref_count_(1) { };

  T v() { return value_; } const

  int add_ref() { return ++ref_count_; }
  int release() { if( --ref_count_ == 0 ) { delete this; return 0; } return ref_count_;}

private:
  T value_;
  int ref_count_;
};

template<class T>
static Param<T> * Param_Factory(const double) {
  return new Param<T>(double);
}

void myFunction(const double d) {
}

engine->RegisterObjectType("Pdouble", 0, asOBJ_REF);
engine->RegisterObjectBehaviour("Pdouble", asBEHAVE_FACTORY, "Pdouble @f(double)", asFUNCTIONPR(Param_Factory<double>, (), Param<double> *), asCALL_CDECL);
engine->RegisterObjectBehaviour("Pdouble", asBEHAVE_ADDREF,  "void f()", asMETHOD(Param<double>, add_ref), asCALL_THISCALL);
engine->RegisterObjectBehaviour("Pdouble", asBEHAVE_RELEASE, "void f()", asMETHOD(Param<double>, release), asCALL_THISCALL);

engine->RegisterObjectBehaviour("Pdouble", asBEHAVE_IMPLICIT_VALUE_CAST, "double f() const", asMETHOD(Param<double>, v), asCALL_THISCALL);

engine->RegisterGlobalFunction("void myFunction(const double)", asFUNCTIONPR(myFunction, (const double), void), asCALL_CDECL);
 
if I do an implicit cast like this :
Pdouble myValue( 0.3 );
double temp = myValue;   // <-- Implicit cast
myFunction( temp );
 
it works ok, but if I do an implicit cast in the function call like that :
 
Pdouble myValue( 0.3 );
myFunction( myValue );   // <-- Implicit cast
angelscript crashes with an illegal memory access.

Is there anything wrong in my code or is implicit value cast not supposed to work when calling a function ?
0

Share this post


Link to post
Share on other sites

I don't see anything wrong with your code. 

 

It looks to be a bug in the AngelScript compiler. I'll look into it.

0

Share this post


Link to post
Share on other sites

Unfortunately I wasn't able to reproduce the problem you reported.

 

After fixing a couple of minor compiler errors in the code you posted (e.g. missing arg list in asFUNCTIONPR, and missing argument in Param_Factory) I tested both scripts without any error.

 

Are you reproducing the crash with these small script snippets? Or does your real script perhaps have a slightly more complex expression in which the implicit cast is performed?

0

Share this post


Link to post
Share on other sites

My full script is like that :

Pdouble myValue(0.3);

void setup()
{
// double temp = myValue;
myFunction(myValue);
}

void update()
{
}

 

My C++ code is much bigger than the code snippet I provided in my post.

 

I will test the script in a simpler program to see if I still reproduce the problem.

 

 

0

Share this post


Link to post
Share on other sites

FYI, I reproduced the problem with this program :

 

 
#include <Windows.h>
#include "angelscript.h"
#include <stdio.h>
 
using namespace std;
 
void cb(const asSMessageInfo *msg, void *param) {
 
    const char *type = "ERR ";
    if (msg->type == asMSGTYPE_WARNING)
        type = "WARN ";
    else if (msg->type == asMSGTYPE_INFORMATION)
        type = "INFO ";
 
    printf("%s (%d, %d) : %s : %s\n", msg->section, msg->row, msg->col, type,
        msg->message);
}
 
 
 
const char mscript[] = "Pdouble myValue(0.3);\n"
"\n"
"void setup()\n"
"{\n"
"// double temp = myValue;\n"
"  myFunction(myValue);\n"
"}\n"
"\n"
"void update()\n"
"{\n"
"}\n"
"\n";
 
template<class T>
class Param {
public:
  Param(const T _value) : value_(_value), ref_count_(1) { };
 
  T v() { return value_; } const
 
  int add_ref() { return ++ref_count_; }
  int release() { if( --ref_count_ == 0 ) { delete this; return 0; } return ref_count_;}
 
private:
  T value_;
  int ref_count_;
};
 
template<class T>
static Param<T> * Param_Factory(const T _value) {
  return new Param<T>(_value);
}
 
void myFunction(const double d) {
}
 
 
int main() {
 
    asIScriptEngine *ng = asCreateScriptEngine(ANGELSCRIPT_VERSION);
 
    int r;
 
    r = ng->SetMessageCallback(asFUNCTION(cb), 0, asCALL_CDECL);
    if (r < 0) printf("error %d\n", r);
 
 
  r = ng->RegisterObjectType("Pdouble", 0, asOBJ_REF);
    if (r < 0) printf("error %d\n", r);
  r = ng->RegisterObjectBehaviour("Pdouble", asBEHAVE_FACTORY, "Pdouble @f(const double)", asFUNCTIONPR(Param_Factory<double>, (const double), Param<double> *), asCALL_CDECL);
    if (r < 0) printf("error %d\n", r);
  r = ng->RegisterObjectBehaviour("Pdouble", asBEHAVE_ADDREF,  "void f()", asMETHOD(Param<double>, add_ref), asCALL_THISCALL);
    if (r < 0) printf("error %d\n", r);
  r = ng->RegisterObjectBehaviour("Pdouble", asBEHAVE_RELEASE, "void f()", asMETHOD(Param<double>, release), asCALL_THISCALL);
    if (r < 0) printf("error %d\n", r);
  r = ng->RegisterObjectBehaviour("Pdouble", asBEHAVE_IMPLICIT_VALUE_CAST, "double f() const", asMETHOD(Param<double>, v), asCALL_THISCALL);
    if (r < 0) printf("error %d\n", r);
  r = ng->RegisterGlobalFunction("void myFunction(const double)", asFUNCTIONPR(myFunction, (const double), void), asCALL_CDECL);
    if (r < 0) printf("error %d\n", r);
 
    // build
    asIScriptModule *mod = ng->GetModule("moi", asGM_ALWAYS_CREATE);
    mod->AddScriptSection("joo", mscript);
 
    r = mod->Build();
    if (r < 0) printf("error %d\n", r);
 
    // execute
    asIScriptFunction *f = mod->GetFunctionByDecl("void setup()");
    if (!f)
        return 1;
 
    asIScriptContext *ctx = ng->CreateContext();
    ctx->Prepare(f);
    r = ctx->Execute();
    if (r < 0) printf("error %d\n", r);
 
    ng->Release();
 
    return 0;
}
0

Share this post


Link to post
Share on other sites

I've fixed the problem in revision 1584.

 

The compiler was producing an invalid bytecode sequence when doing the implicit cast of the global variable in preparation for the function argument. 

 

Thanks,

Andreas

0

Share this post


Link to post
Share on other sites

I tried the fix. My script is running ok now.

 

Thank you very much.

 

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0