Skip to main content
GameDev.net gamedev.net

const int global variable contains it's previous value at every other run of the script.

Started by ohlidalp Aug 26 at 3:49 PM 2 replies 450+ views
Original Post
ohlidalp
ohlidalp

Hello.
I my project, for every script I load, I inject a global variable with it's own ID, like this:

scriptModule->AddScriptSection("abcd", fmt::format("const int thisScript = {};", unit_id).c_str());

However, without any modification to the C++ code (only to one included script), this variable started exhibiting a glitch - for every odd invocation of a script, it contains exactly the value from the previous run.

I debug-stepped into asCBuilder::CompileGlobalVariables() and saw that the value is set correctly. (Please ignore the values in the screenshots, they come from multiple tests).

However, when hitting a breakpoint in a function invoked by the script with thisScript as parameter, I see the old value. Same happens when I concat the value to string and log that.

On the master branch of my project, everything works OK. This is the outer script loading code: ScriptEngine.cpp#L835-L927 and this is where the global var is set up: ScriptEngine.cpp#L965

However, things break in PR 3395, specifically after this commit which only changes one include script: 5a5138f583c2e72aa505ca3a4abef95822f7e8fb, this is the offending line:174|179|dictionary@ scriptDetails = game.getScriptDetails(thisScript);. As commented in the PR, it was repeatedly reported by a tester using GitHub CI artifacts (comment link) and also happens to me running my own builds. In both cases AngelScript is obtained using Conan from ConanCenter: self.requires("angelscript/2.35.1")

I think I may have hit a bug in AngelScript, but it's difficult to isolate this case from the rest of the game (I don't know what exactly triggered it), so I'm posting this first and I'll continue investigating.

EDIT: Still happens with angelscript 2.38 (also conancenter).

ohlidalp
ohlidalp

Update: It turns out this issue had been in the game for a while, it just passed unnoticed until my recent changes.
I've modified the 'include' example to replicate what the game does, and sure enough, the problem does not manifest. The likely culprit is some memory corruption going on in the game. However, so far I've only tested with a devbuild of AngelScript 2.38, not the conancenter package, so there's a minor chance the issue is there.

WitchLord
WitchLord

Hi @ohlidalp

I honestly didn't quite understand the problem. Are you getting the same value for thisScript in different modules? Or are you getting the same value everytime you run a script from an already compiled module?

Anyway, it looks like you're no longer thinking it is a bug in AngelScript, so I won't spend time investigating it.

In order to identify the memory corruption that you suspect exists in your code, I'd recommend setting a memory break point to see where the memory is modified from.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - <a href="http://www.angelcode.com/tower" rel
Sign in to reply to this topic.