Vice City: Multiplayer

Server Development => Scripting and Server Management => Topic started by: value2k on Mar 04, 2016, 05:35 AM

Title: Problem with variable
Post by: value2k on Mar 04, 2016, 05:35 AM
Hello i got problem with my script

local jobid;
function onScriptLoad()
{
CreatePickup(408, Vector(-315.38, -536.935, 10.3346));
}
function onPlayerSpawn( player )
{
jobid = 0;
}
function onPickupPickedUp( player, pickup )
{
if (pickup.ID == 0)
{
if ( jobid == 1 )
{
MessagePlayer("Nein.", player);
return false;
}
player.Cash = player.Cash + 1000;
jobid=1;
}
}

And server write a ERROR:
AN ERROR HAS OCCURED [the index 'jobid' does not exist]

CALLSTACK
*FUNCTION [onPlayerSpawn()] scripts/main.nut line [54]

LOCALS
[player] INSTANCE
[this] TABLE

AN ERROR HAS OCCURED [the index 'jobid' does not exist]

CALLSTACK
*FUNCTION [onPickupPickedUp()] scripts/main.nut line [209]

LOCALS
[pickup] INSTANCE
[player] INSTANCE
[this] TABLE

Please help :(
Title: Re: Problem with variable
Post by: ysc3839 on Mar 04, 2016, 05:47 AM
Replace local jobid; with jobid <- 0;
Title: Re: Problem with variable
Post by: . on Mar 04, 2016, 05:54 AM
Every file is compiled as a function. And as with all functions. All local variables are deleted once the function ends. Same goes with that local variable. Which is why you need to make it global. But be aware though. Not everything should be made global to pollute your global scope. Try to keep it clean. Or at least name things appropriately. Use names that you wouldn't find in a function. Usually people prefix them with 'g_' and uppercase name. That 'g_' stands for global variable and avoids confusion and collisions. Like 'g_MyVar'.
Title: Re: Problem with variable
Post by: value2k on Mar 04, 2016, 05:59 AM
Quote from: S.L.C on Mar 04, 2016, 05:54 AMEvery file is compiled as a function. And as with all functions. All local variables are deleted once the function ends. Same goes with that local variable. Which is why you need to make it global. But be aware though. Not everything should be made global to pollute your global scope. Try to keep it clean. Or at least name things appropriately. Use names that you wouldn't find in a function. Usually people prefix them with 'g_' and uppercase name. That 'g_' stands for global variable and avoids confusion and collisions. Like 'g_MyVar'.
Yes, but i need variable only for player, one for player
Title: Re: Problem with variable
Post by: Xmair on Mar 04, 2016, 06:27 AM
jobid <- array ( GetMaxPlayers ( ) , 0 );

function onPlayerPart( player , reason )
{
jobid [ player.ID ] = 0;
}
I think this is what you meant.
Title: Re: Problem with variable
Post by: value2k on Mar 04, 2016, 02:32 PM
Quote from: Xmair on Mar 04, 2016, 06:27 AMjobid <- array ( GetMaxPlayers ( ) , 0 );

function onPlayerPart( player , reason )
{
jobid [ player.ID ] = 0;
}
I think this is what you meant.
Yes, thx