Team Spartan Cookies & Milk Forums
  • Issue Tracker
  • Donations
  • Search
  • Member List
  • Help

Register

Team Spartan Cookies & Milk Forums Garry's Mod Starfall v
« Previous 1 … 14 15 16 17 18 Next »
Problems with an emitter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pages (2): 1 2 Next »
Threaded Mode
Problems with an emitter
18-04-2015, 10:19 PM, (This post was last modified: 18-04-2015, 10:34 PM by Destiny's Player.)
#1
Destiny's Player Offline
A passing by member
***
Posts: 237
Threads: 35
Joined: Dec 2013
Problems with an emitter
Good time of the day to everybody who reads this.

So, I have a few problems with this code.
1: It overloads and shutdowns, but that's not the main problem, I just didn't look into fixing it because of the second problem.
2: For some reason, the "if" argument near the end, that checks the "on" variable doesn't give me ANYTHING. Tried with a simple "else", tried with an "elseif On == 0", doesn't work, returns something that makes the render completely visible, not even at 200 Alpha.

Also, thank you to Black and Ia for trying to help me.


--@name Field Door HP
--@author TheNextDecade
--@sharedscreen

function Drawer(posy,posx,col,hp)
render.setColor(col[1]-100,col[2]-100,col[3]-100,col[4])
render.drawFilledCircle(60,63,22,22)
render.setColor(col[1]-150,col[2]-150,col[3]-150,col[4])
render.drawCircle(60,63,22,22)
render.setColor(col[1],col[2],col[3],col[4])
local Perc = 40/100*hp
render.drawRect(55,43,10,Perc)
render.drawRectOutline(55,43,10,40)
end


if SERVER then
wire.createInputs({"Active","HP"},{"normal","normal"})
hook("input","ActiveChange",function(Input,Value)
if Input == "Active" then
if net.start() then
net.setDTVar("Int", 1, Value)
net.broadcast()
end
end
end)
else
hook("net","RecieveString",function(Length)
On = net.getDTVar("Int",1)
end)
Itself = entsConfusedelf()
if On == 1 then
local Alpha = 200
elseif On == 0 then
local Alpha = 1
end

hook("render","Render",function()
render.start3D2D(Itself:pos(),Itself:ang(),1)
render.clear()
Drawer(60,63,{150,250,150,Alpha},100)
render.end3D2D()
end)
end
Find
Reply
19-04-2015, 11:28 AM,
#2
Person8880 Offline
gpj.ecafllorT
*******
Posts: 2,407
Threads: 45
Joined: Sep 2013
RE: Problems with an emitter
That's because of lexical scoping.

--Out here, Alpha will be retrieved from the global scope
if On == 1 then
local Alpha = 200
--Alpha exists here, as the above local variable. Once this block ends, Alpha is gone.
elseif On == 0 then
local Alpha = 1
--This Alpha is different from the one above, and only exists here.
end
--Statement block has ended, all local variables in the block are now out of scope, thus Alpha is once again from the global scope.

What you want to do is make sure your local Alpha value is available to the scope above the if statement block:
--Alpha is now a local value to this scope and all below it, initially nil (note that you don't need to set its value to make it nil)
local Alpha

if On == 1 then
Alpha = 200
--We've now set the local Alpha value from above this to 200
else
Alpha = 1
--We've set the local Alpha value from above this to 1
end
--Either prints 200, or 1 depending on how the above statement evaluated
print( Alpha )
Find
Reply
19-04-2015, 01:20 PM,
#3
Destiny's Player Offline
A passing by member
***
Posts: 237
Threads: 35
Joined: Dec 2013
RE: Problems with an emitter
Hmm... Weird. That worked, but there's still a problem with transiting information between the server and the client. It's not even zero, it's non-existent, i.e. print(On) gives absolutely nothing out, not even an empty message.
Find
Reply
19-04-2015, 01:51 PM,
#4
Person8880 Offline
gpj.ecafllorT
*******
Posts: 2,407
Threads: 45
Joined: Sep 2013
RE: Problems with an emitter
That's because you're using a net hook to try and receive a DTVar, when a DTVar is implicitly networked in the background once set, not explicitly sent.
hook("net","RecieveString",function(Length)
On = net.getDTVar("Int",1)
end)
This bit does nothing. The net hook is for receiving network messages when using something like
if net.start() then
net.writeBit( true )
net.broadcast()
end
Placing net.setDTVar() inside a block like this does nothing, it's not sent in the message. Only data written to the message using the net.write* functions is sent.

To solve your problem, you need to rethink what's actually going on. Your code runs once, when the chip is first loaded. The client and server instances are not run at the same point in time. The client instance will load some measure of time later, depending on transferring speed of the code to each client.

At the moment your code runs, the DTVar may be available and correct, or it might not. Therefore, the safe and best way to deal with this (and to detect when it changes!), is to be checking the DTVar value in the render hook so it's always using the most up to date Alpha value.

--@name Field Door HP
--@author TheNextDecade
--@sharedscreen

if SERVER then
wire.createInputs({"Active","HP"},{"normal","normal"})
hook("input","ActiveChange",function(Input,Value)
if Input == "Active" then
--NOTE: No net.start()/net.broadcast(), DTVars aren't sent as messages, they're persistently networked values.
net.setDTVar("Int", 1, Value)
end
end)
else
function Drawer(posy,posx,col,hp)
render.setColor(col[1]-100,col[2]-100,col[3]-100,col[4])
render.drawFilledCircle(60,63,22,22)
render.setColor(col[1]-150,col[2]-150,col[3]-150,col[4])
render.drawCircle(60,63,22,22)
render.setColor(col[1],col[2],col[3],col[4])
local Perc = 40/100*hp
render.drawRect(55,43,10,Perc)
render.drawRectOutline(55,43,10,40)
end
Itself = entsConfusedelf()

hook("render","Render",function()
--Read the DTVar to make sure we're always up to date (reading DTVars is super cheap so there's no worry about performance)
local On = net.getDTVar("Int",1)

--Now sort out the alpha from the current value of On
local Alpha
if On == 1 then
Alpha = 200
else
Alpha = 1
end

--Finally, draw. Note that render.clear() does nothing in 3D2D, you always have to redraw if you want anything to show up.
render.start3D2D(Itself:pos(),Itself:ang(),1)
Drawer(60,63,{150,250,150,Alpha},100)
render.end3D2D()
end)
end
Find
Reply
19-04-2015, 09:05 PM,
#5
Destiny's Player Offline
A passing by member
***
Posts: 237
Threads: 35
Joined: Dec 2013
RE: Problems with an emitter
Finally figured everything out. With the help of T-16, everything finally works! And also, I had to lift my op parameter or whatever up, cause that was why my chip kept on crashing.

--@name Field Door HP
--@author TheNextDecade
--@sharedscreen

function Drawer(posy,posx,col,hp)
render.setColor(col[1]-100,col[2]-100,col[3]-100,col[4])
render.drawFilledCircle(60,63,22,22)
render.setColor(col[1]-150,col[2]-150,col[3]-150,col[4])
render.drawCircle(60,63,22,22)
render.setColor(col[1],col[2],col[3],col[4])
local Perc = 40/100*hp
render.drawRect(55,43,10,Perc)
render.drawRectOutline(55,43,10,40)
end


if SERVER then
SendoTablo = {}
wire.createInputs({"Active","HP"},{"normal","normal"})

time.timer("think",1,0,function()
SendoTablo.Active = wire.ports.Active
SendoTablo.HP = wire.ports.HP

if net.start() then
--printConsole("sending...")
net.writeTable(SendoTablo)
net.broadcast()
end
end )
else
Data = {}
function netRead()
Tbl = net.readTable()

Data.HP = Tbl.HP
Data.Active = Tbl.Active
end
Itself = entsConfusedelf()

hook("net","netRecieve",netRead)
hook("render","Render",function()
render.start3D2D(Itself:pos(),Itself:ang(),1)
if Data.Active == 1 then
Drawer(60,63,{150,250,250,50},Data.HP)
end
render.end3D2D()
end)
end
Find
Reply
20-04-2015, 01:10 AM,
#6
TheOneAndOnlyTee Offline
GMod Admin
Posts: 755
Threads: 47
Joined: Oct 2013
RE: Problems with an emitter
I would've made it so it only sends data when either of the inputs is changed but i was rather tired and couldn't remember how.

Code:
time.timer("think",1,0,function() Active2 = SendoTablo.Active HP2 = SendoTablo.HP SendoTablo.Active = wire.ports.Active SendoTablo.HP = wire.ports.HP if Active2 ~= SendoTablo.Active or HP2 ~= SendoTablo.HP then if net.start() then --printConsole("sending...") net.writeTable(SendoTablo) net.broadcast() end end end )
this should work though, for Optimization!

Steam Profile
My Discord Server
Need help with E2 or SF? , just ask me and i'll try to help.
[Image: 98Dkcoz.gif]
Find
Reply
20-04-2015, 07:33 PM,
#7
Person8880 Offline
gpj.ecafllorT
*******
Posts: 2,407
Threads: 45
Joined: Sep 2013
RE: Problems with an emitter
You do realise, that you've essentially killed the optimisation that was there.

Go back to what I posted. The DTVar is set on change, propagated once and it's happy. With what you have, not only are you sending way more data than you need due to writeTable, but you're also sending redundant data every second.

What I posted already worked, and it way more optimal than using writeTable and the timer.

Networking Problems
To spell it out, this is what write table does in pseudo Lua code:
for Key, Value in pairs( Table ) do
net.writeUInt( TypeID( Key ), 8 )
net.write<KeyType>( Value )

net.writeUInt( TypeID( Value ), 8 )
net.write<ValueType>( Value )
end
net.writeUInt( 0, 8 )
Thus, the write table code you have is sending:
net.writeUInt( TYPE_STRING, 8 ) --8 bits
net.writeString( "Active" ) -- 7 * 8 bits, 8 bits per character and a null terminating character at the end

net.writeUInt( TYPE_NUMBER, 8 ) -- 8 bits
net.writeDouble( wire.ports.Active ) -- 64 bits for a number that's either 0 or 1!!!

net.writeUInt( TYPE_STRING, 8 ) -- 8 bits
net.writeString( "HP" ) -- 3 * 8 bits, 8 bits per character and a null terminating character at the end

net.writeUInt( TYPE_NUMBER, 8 ) -- 8 bits
net.writeDouble( wire.ports.HP ) -- 64 bits again, when it's a percentage value!

net.writeUInt( 0, 8 ) -- 8 bits to mark the end of the table
Total per send: 8 + ( 7 * 8 ) + 8 + 64 + 8 + ( 3 * 8 ) + 8 + 64 + 8 = 248 bits or 31 bytes per send every time.

Now compare that to using 2 DTVars:
net.setDTVar( "Int", 1, wire.ports.HP )
net.setDTVar( "Bool", 1, wire.ports.Active ~= 0 )
Total sent ONLY WHEN CHANGED: 32 + 1 = 33 bits = 4.125 bytes at the most if both values change.

Input change detection problems
You've also now added a completely redundant and overly complicated way of detecting input changes. The input hook was designed for this purpose.
hook( "input", "OnInputChange", function( Input, Value )
--This section runs when an input changes!!!
if Input == "Active" then
net.setDTVar( "Bool", 1, Value ~= 0 )
elseif Input == "HP" then
net.setDTVar( "Int", 1, Value )
end
end )
This accomplishes changing the DTVars (and note that the DTVars only network if the value you are providing is different to what's already stored, this is their entire purpose!) without a clunky timer or a table or checking against other values.

So, back to what I posted before with the added tweak to the active network value:
--@name Field Door HP
--@author TheNextDecade
--@sharedscreen

if SERVER then
wire.createInputs({"Active","HP"},{"normal","normal"})

--This runs when an input changes, which is what we want
hook("input","InputChange",function(Input,Value)
if Input == "Active" then
net.setDTVar("Bool", 1, Value ~= 0)
elseif Input == "HP" then
net.setDTVar("Int", 1, Value)
end
end)

--You were probably wondering why the initial values aren't set.
--The answer is, you have to set them initially, the input hook doesn't run on startup
net.setDTVar("Bool", 1, ( wire.ports.Active or 0 ) ~= 0)
net.setDTVar("Int", 1, wire.ports.HP or 0)
else
--There's no need for this function to be in the server environment, it's rendering which is client only.
function Drawer(posy,posx,col,hp)
render.setColor(col[1]-100,col[2]-100,col[3]-100,col[4])
render.drawFilledCircle(60,63,22,22)
render.setColor(col[1]-150,col[2]-150,col[3]-150,col[4])
render.drawCircle(60,63,22,22)
render.setColor(col[1],col[2],col[3],col[4])
local Perc = 40/100*hp
render.drawRect(55,43,10,Perc)
render.drawRectOutline(55,43,10,40)
end
--ents.self() is not a method, no need for a :
local Itself = ents.self()

hook("render","Render",function()
local On = net.getDTVar("Bool",1)
if not On then return end

render.start3D2D(Itself:pos(),Itself:ang(),1)
Drawer(60,63,{150,250,150,50},net.getDTVar("Int",1))
render.end3D2D()
end)
end
Nothing is more deterring than when someone you are trying to help completely ignores what you've posted and ends up with something far worse and a load of misconceptions about what the code is doing.

I hope that you will not ignore this post and understand why what you have now is much, much worse. If you have any questions about what I've written please ask rather than ending with a chip that steadily declines into unmaintainable.
Find
Reply
20-04-2015, 08:36 PM, (This post was last modified: 20-04-2015, 08:39 PM by Destiny's Player.)
#8
Destiny's Player Offline
A passing by member
***
Posts: 237
Threads: 35
Joined: Dec 2013
RE: Problems with an emitter
First of all, I'm sorry that it looked like I ignored your post. I really didn't, I just couldn't get it to work by myself even with your advice, and I didn't want to wait another day for another helpful post, so I just asked some people on the server, and this was the result. I just got too happy that this chip finally worked SOMEHOW that I slightly phased out.

Secondly, I totally see that the variant you've made is better. Can't try it out right now, as I'm so tired that I had to force myself to write this post now, and not tomorrow, but from the first glance, I see the difference.

Thirdly: Just wanted to say thanks for still trying to help, even though it looked like I completely ignored you. I think that you, as a much more capable programmer, should know how much it goes into your head when something you've been trying to do for several days without any results finally works at least somehow. Or is it just me?
Find
Reply
21-04-2015, 02:42 PM,
#9
TheOneAndOnlyTee Offline
GMod Admin
Posts: 755
Threads: 47
Joined: Oct 2013
RE: Problems with an emitter
same, I should've looked at your code and tried to help him get that working.
also one question, are DTVars better than tables for screens? (assuming i was only sending ~12 variables)

Steam Profile
My Discord Server
Need help with E2 or SF? , just ask me and i'll try to help.
[Image: 98Dkcoz.gif]
Find
Reply
21-04-2015, 02:51 PM, (This post was last modified: 21-04-2015, 05:31 PM by Destiny's Player.)
#10
Destiny's Player Offline
A passing by member
***
Posts: 237
Threads: 35
Joined: Dec 2013
RE: Problems with an emitter
...aaaaand the code you've given me doesn't work. Doesn't even show the render for a moment. In fact, it appears like it doesn't run on the server at all, i.e. there's no instances when I check it with the profiler, and I really have no idea why it doesn't work.
From what I see, DTVars simply don't work for me, they never return anything.

Sorry if I sound as a douchebag, but that's one way I can tell people that stuff doesn't work. And also I'm slightly angry due to some RL issues, so don't mind me if I do act like a douchebag for some time now...
Find
Reply
« Next Oldest | Next Newest »
Pages (2): 1 2 Next »


  • View a Printable Version
Forum Jump:


Users browsing this thread: 1 Guest(s)
Contact UsTeam Spartan Cookies & MilkReturn to TopLite (Archive) ModeMark all forums readRSS Syndication

Powered By MyBB, © 2002-2026 MyBB Group.