dumb newbie questions

dumb newbie questions

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
User avatar
Caydr
Omnidouche
Posts: 7179
Joined: 16 Oct 2004, 19:40

dumb newbie questions

Post by Caydr »

I've searched my lua directory and can find no files containing the words "sleep" or "wait". There's nothing in the lua guide either. I want to set something on a loop, repeating every second, but I can't figure out what the proper command is to insert a waiting period.

_____


I've got this script here that I made. It resizes my minimap automatically, works perfect, no complaints. I just noticed I never bothered to put a "return" or "return true" or "return false" or any manner of return anywhere in it. It still works though. What effect, if any, will this have?

_____


What's the difference between:

local blabla = 5
blabla = 5

hm? I've noticed people say things like "speedup" but I wonder what things should be made local and what shouldn't, what benefits there are, etc.

_____


I need to track a mouse button being held down. If it's not held down for half a second solid, it does one action. If it's held down for longer, it does another action. Until the button's released, nothing should happen.

The best idea I have to do this goes like this:

widget handler - mouse button down
if mouse button 2 is down then
local mouse2down = clock()
end

widget handler - mouse button up
if mouse button 2 is up then
local mouse2up = clock()
end

local function mousechecker()
if (mouse2down - mouse2up) < 0.5 then
blabla()
else
blablabla()
end

This seems very complicated though. Is there a better way of doing it? I thought about just waiting half a second and doing a check, but that would be influenced by the game's speed and UI shouldn't change like that. Also, you might just check half a second later and find the player holding the button down for something else entirely... so it needs to be solid. Then I thought about doing a few shorter waits, like 5 of them, and if it wasn't held down during any of them, break the count. Seemed kind of excessive.

The fist thing I tried was the simple:

if not (mouse2 down) then...

but this seems to be triggered by other mouse button presses as well, for instance button 0... possibly keyboard inputs even, I didn't try that. The player might also be doing things with the other mouse buttons at the same time, which ruled out some other ideas I had.
Last edited by Caydr on 07 Jan 2010, 03:14, edited 3 times in total.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: dumb newbie questions

Post by zwzsg »

Caydr wrote:I've searched my lua directory and can find no files containing the words "sleep" or "wait". There's nothing in the lua guide either. I want to set something on a loop, repeating every second, but I can't figure out what the proper command is to insert a waiting period.
Using the "simulation" time:
(that stands still when game paused)

Code: Select all

function gadget:GameFrame(f)
	if f%30==0 then
		-- stuff that should be done every second
	end
end
Using the "IRL" time:
(that keeps running when game paused)

Code: Select all

local StuffTimer
local StuffPeriod = 1 -- In seconds

local function DoStuff()
	-- Do what must be done every second
end

function widget:DrawScreenEffects(vsx, vsy)
	if (not StuffTimer) or Spring.DiffTimers(Spring.GetTimer(),StuffTimer) > StuffPeriod then
		StuffTimer=Spring.GetTimer()
		DoStuff()
	end
end
Last edited by zwzsg on 07 Jan 2010, 02:46, edited 2 times in total.
User avatar
Caydr
Omnidouche
Posts: 7179
Joined: 16 Oct 2004, 19:40

Re: dumb newbie questions

Post by Caydr »

So I would say:

Code: Select all

function gadget:GameFrame(f)
	if f%30==0 then
		somecrap()
	end
end

local function somecrap()
spring echo whatever
end
I'm confused though, shouldn't it be f%32, or has that changed? Or are we talking about different kinds of sim frames?
Last edited by Caydr on 07 Jan 2010, 02:43, edited 1 time in total.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: dumb newbie questions

Post by zwzsg »

Caydr wrote:I'm confused though, shouldn't it be f%32?
Maybe. I never know.
User avatar
Caydr
Omnidouche
Posts: 7179
Joined: 16 Oct 2004, 19:40

Re: dumb newbie questions

Post by Caydr »

There's like three different types of sim, each with a different number of frames per second ... and we don't have a "going to be sick" emoticon, looks like. Well, I'll try 30 and 32 and see what loses sync with my computer.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: dumb newbie questions

Post by zwzsg »

Caydr wrote:I've got this script here that I made. It resizes my minimap automatically, works perfect, no complaints. I just noticed I never bothered to put a "return" or "return true" or "return false" or any manner of return anywhere in it. It still works though. What effect, if any, will this have?
http://www.lua.org/manual/5.1/manual.html#2.5.9
If control reaches the end of a function without encountering a return statement, then the function returns with no results.
Let's just say it doesn't matter for what you're doing, and if it mattered, then you would know what to return.




Caydr wrote:What's the difference between:

local blabla = 5
blabla = 5

hm? I've noticed people say things like "speedup" but I wonder what things should be made local and what shouldn't, what benefits there are, etc.
People that say things like "speedup" are (mostly) idiots who believe optimisation is about sprinkling a magic keyword everywhere, disregarding how horribly nested their overall algorithm is.
User avatar
Caydr
Omnidouche
Posts: 7179
Joined: 16 Oct 2004, 19:40

Re: dumb newbie questions

Post by Caydr »

How directly applicable to Spring is that documention you linked me to? I know we have some unique stuff for widgets and such, do we also use special syntax, etc?

Thanks. If I don't reply again it's because I've been poring over this one script for like 6 hours trying to figure out how to do something simple and now I'm really tired. I've learned lots of unrelated stuff along the way though...
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: dumb newbie questions

Post by zwzsg »

Everything in http://www.lua.org/manual/5.1/ in 100% true and applicable when it comes to Spring. However, it only describe the Lua language itself. With Spring, comes tons of Spring-specific additions.

So, to code in Lua for Spring, you need both:
http://www.lua.org/manual/5.1/
http://springrts.com/wiki/Lua_Scripting



Caydr wrote:There's like three different types of sim, each with a different number of frames per second ... and we don't have a "going to be sick" emoticon, looks like. Well, I'll try 30 and 32 and see what loses sync with my computer.
Kloot wrote:No, Spring (and any loaded AI lib) does run at a base-rate of 30 simulation frames per second. However, the economic state of a team is backed up every 32 frames (during a "SlowUpdate" in engine parlance), and the state values for income and usage that AI's have read access to represent summations over the last team slow-update cycle. So, at any frame f that is not an integer multiple of 32, an AI is only able to obtain its cumulative income and usage from frame (int(f / 32) - 32) up until frame int(f / 32).
User avatar
Caydr
Omnidouche
Posts: 7179
Joined: 16 Oct 2004, 19:40

Re: dumb newbie questions

Post by Caydr »

Thanks for your help, very educational.

If anyone else reads this, I was able to solve the issue I was having with tracking mouse up/down time through some trial and error and what seemed at the time to be some rather clever math.

I say that as someone who hasn't used a > or < sign for things besides emoticons in about 6 years.

So /topic
Post Reply

Return to “Lua Scripts”