I am coding a money printer type system for a friend and I am not getting an error, but it is not doing what i expect, and I cannot tell if the problem is being cased in the timer or because of the net library
I am calling this in server-side ENT:Initialize -
[code]
self:SetNWFloat( "StoredMoney", 0 )
timer.Create( self:EntIndex().."printer", 5, 0, function() printMoney( self ) end ) -- 5 seconds to test func without wait
[/code]
and here is the function it points to -
[code]
function printMoney( ent )
ent:SetNWFloat( "StoredMoney", ent:GetNWFloat( "StoredMoney" ) + 250 )
if table.HasValue( { 8, 9, 10, 11, 12, 13, 14, 15 }, ent.ConnectedAddons ) && not ent:IsOnFire() then
timer.Adjust( ent:EntIndex().."printer", math.random( 100, 200 )/2, 0, function() printMoney( ent ) end )
end
end
[/code]
I have tried using NWInts, setting up the function with printMoney = function(ent), using prints to find where the problem is (prints come from both initialize and in the func the timer points to, both worked), and using timer.Simple()s. What am i doing wrong?
[QUOTE=jack10685;45605715]I am coding a money printer type system for a friend and I am not getting an error, but [B]it is not doing what i expect[/B], and I cannot tell if the problem is being cased in the timer or because of the net library
I am calling this in server-side ENT:Initialize -
[code]
self:SetNWFloat( "StoredMoney", 0 )
timer.Create( self:EntIndex().."printer", 5, 0, function() printMoney( self ) end ) -- 5 seconds to test func without wait
[/code]
and here is the function it points to -
[code]
function printMoney( ent )
ent:SetNWFloat( "StoredMoney", ent:GetNWFloat( "StoredMoney" ) + 250 )
if table.HasValue( { 8, 9, 10, 11, 12, 13, 14, 15 }, ent.ConnectedAddons ) && not ent:IsOnFire() then
timer.Adjust( ent:EntIndex().."printer", math.random( 100, 200 )/2, 0, function() printMoney( ent ) end )
end
end
[/code]
I have tried using NWInts, setting up the function with printMoney = function(ent), using prints to find where the problem is (prints come from both initialize and in the func the timer points to, both worked), and using timer.Simple()s. What am i doing wrong?[/QUOTE]
I might've missed it, but what exactly [B]are[/B] you expecting this to do, and what is it [B]actually[/B] doing? Letting us know those two things would help us in helping you.
I've modified your snippets just a bit with small possible changes you could make until you can tell us exactly what you want these snippets to do:
[LUA]-- unless you expect to have outrageously high amounts of money from these printers,
-- or want to have decimal values (e.g., 250.50, 755.25), you should probably just stick with an integer
self:SetNWInt( "StoredMoney", 0 )
timer.Simple( 5, function() printMoney( self ) end ) -- unless you really need these timers to be unique/modifiable, a simple anonymous timer works fine
function printMoney( ent )
if ( !IsValid( ent ) ) then return end -- ensure the entity is still valid before doing anything with it
ent:SetNWInt( "StoredMoney", ent:GetNWInt( "StoredMoney" ) + 250 )
if ( table.HasValue( { 8, 9, 10, 11, 12, 13, 14, 15 }, ent.ConnectedAddons ) && not ent:IsOnFire() ) then -- what is the ent.ConnectedAddons check for?
-- math.random( 100, 200 ) / 2 is the same thing as math.random( 50, 100 )
-- no expressed need for a unique timer since we only need to run this once; a new timer will be created the next time it prints money
timer.Simple( math.random( 50, 100 ), function() printMoney( ent ) end )
end
end[/LUA]
[QUOTE=Mista Tea;45607042]I might've missed it, but what exactly [B]are[/B] you expecting this to do, and what is it [B]actually[/B] doing? Letting us know those two things would help us in helping you.
I've modified your snippets just a bit with small possible changes you could make until you can tell us exactly what you want these snippets to do:
[LUA]-- unless you expect to have outrageously high amounts of money from these printers,
-- or want to have decimal values (e.g., 250.50, 755.25), you should probably just stick with an integer
self:SetNWInt( "StoredMoney", 0 )
timer.Simple( 5, function() printMoney( self ) end ) -- unless you really need these timers to be unique/modifiable, a simple anonymous timer works fine
function printMoney( ent )
if ( !IsValid( ent ) ) then return end -- ensure the entity is still valid before doing anything with it
ent:SetNWInt( "StoredMoney", ent:GetNWInt( "StoredMoney" ) + 250 )
if ( table.HasValue( { 8, 9, 10, 11, 12, 13, 14, 15 }, ent.ConnectedAddons ) && not ent:IsOnFire() ) then -- what is the ent.ConnectedAddons check for?
-- math.random( 100, 200 ) / 2 is the same thing as math.random( 50, 100 )
-- no expressed need for a unique timer since we only need to run this once; a new timer will be created the next time it prints money
timer.Simple( math.random( 50, 100 ), function() printMoney( ent ) end )
end
end[/LUA][/QUOTE]
Ah forgot to include that in the OP, what i am expecting to happen is for the setints to be writing data to the ents, but Nothing is being written. I have used prints to see that the timer's function executes fine and that the ent is included in it, but ent:SetNWInt/Float is not physically writing data to the entity.
[editline]6th August 2014[/editline]
I have switched to storing the data in a table server-side and sending an int from the table in the net message and that seems to fix the problem. no clue as to why setting nwints was not working.
Sorry, you need to Log In to post a reply to this thread.