Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 3 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Script Language Reference and Guide
TheGreatCthulhu Offline
Member

Posts: 213
Threads: 10
Joined: Oct 2010
Reputation: 32
#12
RE: Script Language Reference and Guide

(01-15-2013, 06:40 PM)Adrianis Wrote: I never did encounter any bugs when using GetGlobalVarString itself as the parameter... in your tests, was there any room for the issue to have been in another part of the code? If it does become clear why that problem was happening, please do share.

Without first storing it as a string@, I get inconsistent results: sometimes it appears to work, more often it doesn't.

I do have a hunch what's this all about. Using HPL1 as a starting point for the string implementation, internally, strings are std:Confusedtring instances from Standard Template Library (STL) of C++. Basically, as you know from C, char arrays are a pain to manage, so C++ comes with a string class that does it for you. If we are to judge by the source code of HPL1, in order to support reference-type semantics (reference-counted handles) of AngelScript, the internally used string is wrapped, via a pointer, in another class, which is then exposed to the script.
Now, implementation details of STL might vary from runtime to runtime, but, what I suspect is happening is that GetGlobalVarString() is for some reason returning a reference (memory address) of the internal char buffer used by the C++ string class, so that, depending on the actual implementation of std:Confusedtring, when in certain circumstances the internal char array gets replaced by a different one (maybe more space was needed), the script's string& is left pointing to invalid memory (or something along those lines).

This also means that this bug might be hard to reproduce, but, here's the code, try it out several times, as in my tests sometimes it works, sometimes nothing is shown (as if the character codes aren't printable):
PHP Code: (Select All)
void OnEnter()
{
    
SetGlobalVarString("TestString""abcd");
    
    
ChangeString(GetGlobalVarString("TestString"));    
    
AddDebugMessage(GetGlobalVarString("TestString"), false);
}

void ChangeString(strings)
{
    
"ABCDEFGHIJKLMN";



Expanding a bit on that reveals that the script is aborted if such conditions are met:
PHP Code: (Select All)
void OnEnter()
{
    
SetGlobalVarString("TestString""abcd");
    
AddDebugMessage("start -->"false);
    
    
ChangeString(GetGlobalVarString("TestString"));    
    
AddDebugMessage(GetGlobalVarString("TestString"), false);
   
    
stringtemp GetGlobalVarString("TestString");
    
int i temp[0];
    
AddDebugMessage("char[0]: " ifalse);
    
    
AddDebugMessage("<--- end"false);
}

void ChangeString(strings)
{
    
"ABCDEFGHIJKLMN";


In case of a problem, the output ends with:
start -->

However, in my case, it's enough to just move the line

string@ temp = GetGlobalVarString("TestString");

to the top, and the string apparently gets pinned in memory, so OnEnter() always finishes successfully:
PHP Code: (Select All)
void OnEnter()
{
    
stringtemp GetGlobalVarString("TestString");
    
    
SetGlobalVarString("TestString""abcd");
    
AddDebugMessage("start -->"false);
    
    
ChangeString(GetGlobalVarString("TestString"));    
    
AddDebugMessage(GetGlobalVarString("TestString"), false);
       
    
int i temp[0];
    
AddDebugMessage("char[0]: " ifalse);
    
    
AddDebugMessage("<--- end"false);
}

void ChangeString(strings)
{
    
"ABCDEFGHIJKLMN";


The script function should have probably been implemented to return a string@, not a string&, like this:
PHP Code: (Select All)
stringGetGlobalVarString(stringvarID); 
(This post was last modified: 01-18-2013, 10:57 PM by TheGreatCthulhu.)
01-18-2013, 10:54 PM
Find


Messages In This Thread
RE: Script Language Reference and Guide - by TheGreatCthulhu - 01-18-2013, 10:54 PM



Users browsing this thread: 1 Guest(s)