Page 1 of 1

echo, drawing stuff and other noobness

Posted: 15 Oct 2009, 18:22
by knorke
So I made a totally leet widget that warns you if your commander is under attack. :shock:

Looks like this:

Code: Select all

function widget:UnitDamaged (unitID, unitDefID, unitTeam, damage, paralyzer, weaponID, attackerID, attackerDefID, attackerTeam)
  local ud = UnitDefs[unitDefID]
    if (ud.isCommander) then
      current_time = widgetHandler:GetHourTimer()
	if (current_time > last_attack+2) then
		Echo( "OH FUCK! Commander under attack!" )
		end
	last_attack = current_time
	end
end
I use echo( ) but I am not sure if these are public chat messages or are they like the "unit can not reach location" and are only seen by 'the' player.
In wiki it says "prints to the ingame chat and infolog.txt" but I also read somewhere "prints to console"

Also, will this also warn about your allys commander? (your enemies??) so that I have to check the attacked commanders team?
Or is this unneeded because UnitDamaged only gets called for "your" units anyway?

---------

So I want to draw some basic stuff, like lines or rectangles. For example make the screenborders flash red when the commander is attacked.
Whats the easiest way to do so? OpenGL scares me atm with renderlists and matrix and stuff. I was told to use something like chiliUI - this seems very overkill? What would you recommend?

Re: echo, drawing stuff and other noobness

Posted: 15 Oct 2009, 19:19
by FLOZi
Echo is seen by everyone, use

Code: Select all

SendMessageToTeam ( number teamID, string "message" ) -> nil
It will warn you about your ally's, not sure about enemies though I think it probably will (indeed it depends on how UnitDamaged is called in widgetland).

Re: echo, drawing stuff and other noobness

Posted: 15 Oct 2009, 19:55
by Regret
You could try Red UI Framework.

Or learn how to draw basic stuff with just spring lua gl., should take about a day or two.

f.e. rectangle:

Code: Select all

function widget:DrawScreen()
	gl.ResetState() --cleanup
	gl.ResetMatrices()

	--draw a white rectangle
	gl.Color(1,1,1,1)
	gl.Rect(px,py,px+sx,py+sy) --px,py - position,  sx,sy - size
	--end

	gl.ResetState() --cleanup
	gl.ResetMatrices()
end

Re: echo, drawing stuff and other noobness

Posted: 15 Oct 2009, 20:04
by very_bad_soldier
FLOZi wrote:Echo is seen by everyone, use
It does?? I could be totally wrong but I was quite sure that Spring.Echo is just a local output for the player that is running the script.

Re: echo, drawing stuff and other noobness

Posted: 15 Oct 2009, 20:13
by Regret
very_bad_soldier wrote:I could be totally wrong but I was quite sure that Spring.Echo is just a local output for the player that is running the script.
You are right, echo is only for the widget owner.

Re: echo, drawing stuff and other noobness

Posted: 15 Oct 2009, 20:30
by FLOZi
hmm, I suppose that makes sense after a fashion. I'm too used to working with gadgets, I've done very little LuaUI work.

Re: echo, drawing stuff and other noobness

Posted: 15 Oct 2009, 20:41
by knorke
ok, thanks everybody for clearing that up.

also thanks regret for the example.
So basicly I just call

Code: Select all

gl.ResetState()
 gl.ResetMatrices()
then I draw my stuff and then call these 2 functions again and the buffer gets refreshed to the screen or something? No need to intialize anything?
Ok, thats seems simple enough :)

And when first loading the widget I would probally use gl.GetViewSizes to get the screensize so I do not draw outside the screen and can scale stuff.

Re: echo, drawing stuff and other noobness

Posted: 15 Oct 2009, 20:45
by Regret
knorke wrote:ok, thanks everybody for clearing that up.

also thanks regret for the example.
So basicly I just call

Code: Select all

gl.ResetState()
 gl.ResetMatrices()
then I draw my stuff and then call these 2 functions again and the buffer gets refreshed to the screen or something? No need to intialize anything?
Ok, thats seems simple enough :)

And when first loading the widget I would probally use gl.GetViewSizes to get the screensize so I do not draw outside the screen and can scale stuff.
Those 2 lines are for cleaning up any previously set gl states by other widgets and then at end for cleaning up your set states like gl.color.

gl works like a state machine.

screen size:

Code: Select all

local vsx,vsy = widgetHandler:GetViewSizes()
if (vsx == 1) then --hax for windowed mode
	vsx,vsy = Spring.GetWindowGeometry()
end
function widget:ViewResize(viewSizeX, viewSizeY)
	vsx,vsy = widgetHandler:GetViewSizes()
end

Re: echo, drawing stuff and other noobness

Posted: 15 Oct 2009, 23:39
by jK
knorke wrote:So basicly I just call

Code: Select all

gl.ResetState()
 gl.ResetMatrices()
then I draw my stuff and then call these 2 functions again and the buffer gets refreshed to the screen or something? No need to intialize anything?
Ok, thats seems simple enough :)
NEVER EVER USE gl.ResetState!!!
It does NOT do what you expect! Don't use it as long as you really, really, - and yeah, before you forget - really know what you are doing.

Re: echo, drawing stuff and other noobness

Posted: 16 Oct 2009, 00:17
by Regret
jK wrote:NEVER EVER USE gl.ResetState!!!
It does NOT do what you expect! Don't use it as long as you really, really, - and yeah, before you forget - really know what you are doing.
http://springrts.com/wiki/Lua_OpenGL_Api#Basics
That's pretty clear what it does, and the values it sets are not interfering with anything.

Every widget SHOULD in fact use resetstate to clean up after itself. And before drawing just in case someone forgot to do so.

Re: echo, drawing stuff and other noobness

Posted: 16 Oct 2009, 00:26
by jK
Regret wrote:
jK wrote:NEVER EVER USE gl.ResetState!!!
It does NOT do what you expect! Don't use it as long as you really, really, - and yeah, before you forget - really know what you are doing.
http://springrts.com/wiki/Lua_OpenGL_Api#Basics
That's pretty clear what it does, and the values it sets are not interfering with anything.

Every widget SHOULD in fact use resetstate to clean up after itself. And before drawing just in case someone forgot to do so.
wiki wrote:gl.ResetState DON'T USE IT AS LONG AS YOU KNOW WHAT YOU ARE DOING! each callin has different default states, but this function resets all states no matter what to the states below!

Re: echo, drawing stuff and other noobness

Posted: 16 Oct 2009, 00:35
by knorke
well, I use non of this variables and seeing as the comm_ends warning widget does work without ResetState, I think I do not need it either.

thanks, found the screensize thing used in another widget already.

as far as i can tell, the UnitDamaged() does not get called for enemy units.

its a bit strange, one can only draw to the screen in this DrawScreen function, but I think ive figured it out now.

Re: echo, drawing stuff and other noobness

Posted: 16 Oct 2009, 11:02
by Regret
jK wrote:
wiki wrote:gl.ResetState DON'T USE IT AS LONG AS YOU KNOW WHAT YOU ARE DOING! each callin has different default states, but this function resets all states no matter what to the states below!

Code: Select all

 ShadeModel = GL_SMOOTH
 Scissor    = false
 Texture    = false
 Lighting    = false,
 ColorMask  = true, true, true, true
 DepthMask  = false
 DepthTest  = false  (GL_LEQUAL)
 Culling    = false  (GL_BACK)
 LogicOp    = false  (GL_INVERT)
 AlphaTest  = false  (GL_GREATER, 0.5f)
 Blending   = true (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)