Constants and enums

Started by
8 comments, last by WitchLord 19 years, 8 months ago
Hi, just asking if there's any way to express constants and enumerations, both globally and for classes and objects? I noticed it's possible to state a variable (property) as const, but it still needs to be physically present. For example, I have C++ code like this:

class Foo
{
public:
    enum FLAGS
    {
        BAR  = (1 << 0),
        GORP = (1 << 1),
        // ... and so on
    };
    // ...
};

When exposing the Foo class API to scripting, I'd like to expose the possible flags as well to be used with methods. As I see it, currently I'd have to create "proxy" global const variables holding the same values, and then register them as global properties (with prefixed names or so). Or is it possible to introduce class specific constant values in some better way? What about static class member fields?
--Jetro Lauha - tonic - http://jet.ro
Advertisement
You can add enums and constants by building a script section with the declarations and adding it to the module that you're building.

const char *constants = "const int flag1 = 0; const int flag2 = 1; const int flag3 = 2; // etc";engine->AddScriptSection("module", "constants", constants, strlen(constants));engine->AddScriptSection("module", "loaded script", script, strlen(script));engine->Build("module", 0);


I may add some way of registering enums and constants directly through other Register functions but there are a lot of other things I judge more important.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Thanks for the reply.

That's what I ended up doing then, with adding class specific prefix to the values which are related to some classes.

All added script sections get deleted on a Discard call though, although registered object types, methods, functions and properties seem to remain registered.

Basically I want to keep the same base with the different registered addon APIs (containing script section adds to get the "enums"), and then load some specific script file on top of that which can be changed.

For now I ended up reinitializing the whole scripting engine when I load a new script.

Also the solution of adding script sections to make "enums" has the problem that it screws up the line numbers for debugging purposes related to the actual loaded script file. This is easily "fixed" by not adding newlines at all for the initial script sections, in which case only the columns for the first line are wrong (which is most probably a comment or something valid anyway). Would it be possible to get line numbers local to the added section name?

P.S. Other small things: I also find it a bit annoying having to write explicit type conversions in script code. For example, some func returns an int and another takes in a float for a coordinate, and I'd like to pass the int directly to the func. Now I have to write float(var) in all cases like this. Also requirement on defining bits explicitly with a hex value (0x) felt a bit stupid. :) And last, the debug libs keep making a huge stats.txt (and bunch of other __*.txt files), so I stopped using the debug lib for now.

Edit: Not to sound too negative, I thought that I should probably mention that apart from the small issues I'm having, I have found out AS quite easy and nice to use so far (with the few small tests I have been making).
--Jetro Lauha - tonic - http://jet.ro
Critiques are good :)

Actually the script sections get deleted when you call Build(). Because there is no sense in keeping two copies (albeit in different form) of the scripts.

I've been thinking about a way to make the engine configuration more dynamic. Currently there is no way to unregister functions/objects/properties. I'll probably end up doing something similar to the modules, or create unregister functions. My goal is that it should be possible to dynamically load plug-ins that get registered with the engine and then unloaded when no longer needed. Allowing for the registration of constants and enums like this makes a lot more sense in this case so I'll probably add that as well.

I've been aware of a problem with the wrong line numbers for a while, but haven't found the time to fix them. What you tell me gives me a little more insight as line numbers are supposed to be local to the script sections. If they are not then something is definitely wrong. I'll see if I put more effort into fixing this.

I may change the way implicit conversions are treated for future versions. I was thinking that limiting the implicit conversions would help the script writer avoid difficult to find bugs, but I may just have hindered the ease of development. I'll have to think about this one.

You can avoid the creation of the stats.txt and the __*.txt files by undefining the precompiler flag AS_DEBUG from the project settings. These files are used by me to verify the bytecode.

Thanks for letting me know that you like the library after all. That was an relief (I mean it).

I only ask you to have patience because I currently have almost no time at all to work on AS, as I'm working on a paying project right now. In a couple of months I should be back to the normal rythm of releases.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Sounds good. I perfectly understand the issue of having not enough time, don't worry. :)

I also have been wondering have you put up much thought to possible exploits through the scripting interface? AS gives a bit more direct interface to program execution by having pointers and such, which are usually banned in scripting side as is. Although to me it seems there's some validation regarding pointer handling. For the application I'm currently making this is not really an issue though, I'm just interested if you have been focusing on this side as well. That is, assuming that application itself doesn't sloppily open holes in the functionality it exposes, how secure do you think AS is against intentional misuse of scripting?
--Jetro Lauha - tonic - http://jet.ro
Pointers aren't a problem because (AFAIK at least), AS doesn't allow the modification or creation of pointes. You can only pass them around between functions. This is also the reason why arrays are a bit more complex.
Gyrbo is correct.

I'm very concious of security issues and I try my best to make AngelScript as secure as possible. As far as I know, AngelScript itself doesn't expose any way of exploiting either the application or operative system. The script writer can only do what the host application allows him to do.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Quote:Original post by jetro
Also the solution of adding script sections to make "enums" has the problem that it screws up the line numbers for debugging purposes related to the actual loaded script file. This is easily "fixed" by not adding newlines at all for the initial script sections, in which case only the columns for the first line are wrong (which is most probably a comment or something valid anyway). Would it be possible to get line numbers local to the added section name?


I found a little time to take a look at the problem with the line numbers and I did find a small bug in the as_scriptnode.cpp file. To fix it do the following modification:

void asCScriptNode::UpdateSourcePos(int pos, int length){ // Add this line to the beginning of the function if( pos == 0 && length == 0 ) return; // No more changes after that


{EDIT} I fixed an error in the bug fix {/EDIT}

This fixes the bug where some errors always reports the position on row 1 and column 1. Though from what you describe this isn't exactly the problem you're having.

I was trying to reproduce the problem as you described it, but I didn't have any success in that. One script section shouldn't affect the line numbers in another, unless there is a really serious bug somewhere.

Could you test the fix and tell me if it also fixes your problem?

[Edited by - WitchLord on July 27, 2004 7:38:47 AM]

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

I tried to apply that fix, but it resulted in the addon bstr registration starting to fail.

Assertion failed on expression: r >= 0
File: bstr.cpp, Line: 29

r equals to -12 at that point, which seems to be asINVALID_TYPE.

I tried to reproduce the problem I described, and for some reason couldn't get it anymore. Apparently I have changed something on the way in my app/scripting init code which fixed the issue for me, or that I have just done something wrong before. So you can leave this issue for now, I guess, since there's no test case for it. :)
--Jetro Lauha - tonic - http://jet.ro
That there isn't a test case for just proves that I haven't tested it yet. ;)

I'll see if I can find a problem with registering bstr when using the bug fix.

OK. The problem did exist. I have no idea why you weren't able to reproduce it though. The problem was that UpdateSourcePos() didn't update when pos == 0, which is in fact a valid value. Change the previous bug-fix to be as follows:

if( pos == 0 && length == 0 ) return;

The difference is the && instead of the ||. Note, I've updated the bug-fix above.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement