local variables - crashing if empty

Create issue
Issue #20 resolved
Fabian Maurer created an issue

I tried to make a local variable to count the numbers the player said something over a few days. I don't want it global to don't mess with other variable which may have the same name. But if I leave the variable empty (every time assigning 0 would always override the information) crashes the engine.

label Start
    local $TimesSaid; //Crash
end
label Start
    local $TimesSaid = 1; //No crash
end

I wanted to make something like

label AskForGunPoder
    if(GetLoveLevel() > 5) then
        GiveItem(289, 1);
    else
        local $TimesSaid;
        if($TimesSaid == nil) then
            $TimesSaid = 0;
        end
        $TimesSaid = $TimesSaid + 1;
        if($TimesSaid < 5) then
            say "Creeper" "I dont have any for you now";
        else
            say "Creeper" "Stop asking me!";
        end
    end
end

Comments (5)

  1. Chimaine

    while this is indeed a bug, you have a logical error there.

    local $TimesSaid;
    if($TimesSaid == nil) then
    

    This would always be true. There are no C like static locals in MTS.

  2. Fabian Maurer reporter

    Oh I see... Is it possible to work around this problem or do I have to make the variable global ?

  3. Chimaine

    I wouldn't call it a problem but rather per design. That is how local variables work in most lexical scoped languages. So you indeed have to use a script-global variable for that.

  4. Log in to comment