clugg / sm-json

A pure SourcePawn JSON encoder/decoder.
GNU General Public License v3.0
82 stars 8 forks source link

[Bug] Compilation error with global defined array #15

Closed Tetragromaton closed 4 years ago

Tetragromaton commented 4 years ago

Description

A clear and concise description of what the bug is. Error on compilation. In case if you try to define JSON_Array as global in plugin, you will get error if you pushObject inside of it.

Reproduction

#include <json>
JSON_Array argr = new JSON_Array();

public void OnPluginStart()
{
argr.PushObject(null);
}
Result:
error 008 | must be a constant expression; assumed zero

Versions

sm-json: v3.0 SourceMod Compiler: SPEDIT SourceMod Build: ver 1.8

clugg commented 4 years ago

Hey there! I don't think this is specifically a problem with the library, but rather just how SourcePawn works. The following snippet, which only uses types native to SourceMod, produces the same error as yours.

StringMap sm = new StringMap();

public void OnPluginStart()
{
    sm.Clear();
}

The best way to get around this (and how I get around it with plugins that have global JSON instances) is by declaring the variable as null in the global scope and then properly initialising the instance in your OnPluginStart, for example:

#include <json>

JSON_Array argr = null;

public void OnPluginStart()
{
    argr = new JSON_Array();
    argr.PushObject(null);
}

Hope this helps.