Page 32 of 50

Re: Presenting: Lunadll for Lua!

Posted: 11 Mar 2015, 18:03
by Kevsoft
Arctangent wrote:the sounds are all strange.
Did you add the ogg sounds into the sounds folder?
Arctangent wrote:But when I go make a level and shove lunadll.lua into its resource folder, I can't even get a "Hello, world!" to pop up. Is there some sort of extra step I'm missing?
The only reasons for this would be:
1.) You didn't update LuaScriptsLib correctly
2.) When you created the lua file, you might accidentally created a text file and your filename is now lunadll.lua.txt. If you created your file with Rightclick > New > New Textfile, this issue is most likely.

Re: Presenting: Lunadll for Lua!

Posted: 11 Mar 2015, 18:20
by Arctangent
Kevsoft wrote:
Arctangent wrote:the sounds are all strange.
Did you add the ogg sounds into the sounds folder?
Mhm.
Kevsoft wrote:
Arctangent wrote:But when I go make a level and shove lunadll.lua into its resource folder, I can't even get a "Hello, world!" to pop up. Is there some sort of extra step I'm missing?
The only reasons for this would be:
1.) You didn't update LuaScriptsLib correctly
2.) When you created the lua file, you might accidentally created a text file and your filename is now lunadll.lua.txt. If you created your file with Rightclick > New > New Textfile, this issue is most likely.
Could you explain the former in more detail? It really can't be the latter, as I have hide file extensions off and Notepad++ instantly switches to its Lua highlighting upon opening the file.

Re: Presenting: Lunadll for Lua!

Posted: 11 Mar 2015, 18:31
by Kevsoft
Arctangent wrote: Mhm.
About "strange", you probably got the A2XT sounds pack. I just updated the main post where to find the SMBX sounds.
Arctangent wrote:Could you explain the former in more detail?
Well the best way to do is to remove LuaScriptsLib entierly and copy the LuaScriptsLib from the .zip into the SMBX folder.

Re: Presenting: Lunadll for Lua!

Posted: 11 Mar 2015, 18:40
by Arctangent
Ah, that fixed it, thanks! Wonder what exactly went wrong with the first installation.

Re: Presenting: Lunadll for Lua!

Posted: 12 Mar 2015, 11:37
by Hoeloe
Horikawa Otane wrote: Okay, so I have the following code and it *works* just fine, but if you alt-tab out the music stops (fine) and when you come back to the window, it's still silent (not fine).
That sounds like a bug in LunaLua, and there's not much you can do in the code to solve that.
Horikawa Otane wrote: I'd also like it to play a random other song upon the current song's completion with proper nice fading in and out. Would that be possible?

(Also it doesn't seem to be random...? Each time I start it, it plays the same song...)
Playing another random song on completion should be possible, though I don't believe there's a way of getting the song's length from Lua, you'd just have to fill it in manually.

As for not being random, that could be because you're setting randomseed to os.time() in the base file. It sounds like seed it being set to the same value each time (because that function is being called at the same time every time you run the function), so it will always return the same value every time you run it. I recommend getting rid of the randomseed call and seeing if that helps. It's only necessary under very specific circumstances anyway. You should also probably just feed the random call directly into MusicOpen, rather than caching it:

Code: Select all

 MusicOpen(MUSICTIME[math.random(#MUSICTIME)]);
So, for the fading out, I'd recommend using the eventu.lua library to play the music. You'll also probably want to make a list of tables to store song names and lengths. That way, you can do this:

Code: Select all

local eventu = loadAPI("eventu");

local MUSICTIME = {};
local MUSIC_COUNT = 2;
local readyPlaySong = true;

function onLoad()
	for i = 1, MUSIC_COUNT do
   		MUSICTIME[i] = { song = "trials" .. tostring(i) .. ".mp3", length = 60 }; --length needs to be the length of each song in seconds.
	end
end

function onLoop()
	if(readyPlaySong) then
		PlayRandomSong(2);
	end
end

function PlayRandomSong(fadeLength)
	local song = MUSICTIME[math.random(#MUSICTIME)];
	MusicOpen(song.song);
   	MusicPlay();
   	readyPlaySong = false;
   	eventu.run(function() 
   					eventu.waitSeconds(song.length - fadeLength);
   					MusicStopFadeOut(fadeLength*1000);
   					eventu.waitSeconds(fadeLength);
   					readyPlaySong = true;
   				end);
end
That should play a random song, then fade it out 2 seconds from the end of the song (assuming you input the lengths correctly), and play another song.

Re: Presenting: Lunadll for Lua!

Posted: 12 Mar 2015, 14:39
by Rednaxela
One thing I'll note with regards to inputting song length like that... it won't be particularly precise. The audio clock and system time often don't operate in perfect lock-step, and probably more importantly starting the fadeout and such is limited to the timing of SMBX frames as well. For this reason, I would suggest adding a little silent padding to the end of the music files which you *won't* include in the song length you code in, as to give a little leeway to avoid the case of the audio starting to loop before you've fully faded out and switched tracks.

A bigger problem though... is pausing. Pretty sure that'll screw up the timing method Hoeloe has shown above, because I believe it pauses the music, and eventu's wait seconds routine uses os.time which doesn't get paused. You could count SMBX frames instead, but that will be less precise for the case of not being paused.

I think it would be best to extend LunaLua to allow getting the music playback state information (The SDL API doesn't let the C++ code get the playback time easily either... but it is where the music pausing is implemented so it's easy enough to make an approximation that's safe for that). I could implement that this weekend if wanted. Can also look into that music not coming back after an alt-tab issue.

Re: Presenting: Lunadll for Lua!

Posted: 12 Mar 2015, 15:30
by Wohlstand
The reason of this problem:
When we releasing music stream, SMBX doesn't send own commands like "Pause/Resume" to MCI proxy to SDL, and when we switching with Alt+Tab, music continues playing.

My code example (I was set random music trigger), it switching musics when it needed and doesn't stop music playback when you click Alt+Tab.

Code: Select all

-- Music Box, will play each music file from playlist. Switching automatically after 20 sec.

-- Timer
TickTack = 0
-- Music track
CurTrack = 1

-- playlist
Tracks = {
 	   "i_bach_joke.it",
	   "schwing.mod",
	   "Double_Cherry_Pass.it",
	   "ice_ow.mid",
	   "AliBaba_4Cumbia.mp3",
	   "the_reincarnation_of_yammah.s3m",
	   "backtoth.mod",
	   "8bitenized.xm",
	   "monkey_island_v1.xm",
	   "D_MESSAG.it"
	}

-- Play SFX
function playSwitchSnd()
	playSFXSDL("pswitch.ogg")	
end

function switchMusic()
	if(TickTack==0)then
		CurTrack = math.random(table.getn(Tracks))
		-- Open sound track and store
		MusicOpen(Tracks[CurTrack])
		-- Set timeout 20 sec.
		TickTack=1300
		playSwitchSnd()
		MusicVolume(127)
	else
		TickTack=TickTack-1
	end


	-- Start playback and resume if paused
	if(TickTack==130)then
		MusicStopFadeOut(2000)
	else
		if(TickTack<=1172)then
			MusicPlay()
		else
			if(TickTack==1298)then
			MusicVolume(127)
			MusicPlay()
			end
		end	
	end
	printText(Tracks[CurTrack], 10, 10);
end

function onLoop()
end


-- Play all custom musics in the second section
function onLoadSection1(playerIndex)
	if(playerIndex==1) then
		-- Open current track to overwrite previouse music file
		MusicOpen(Tracks[CurTrack])
	end
end

-- Do loop in second section
function onLoopSection1(playerIndex)
	if(playerIndex==1) then
		switchMusic()
	end
end


function onLoad()
	TickTack=0
end
My solution of problem is:
Start level from section which has the default SMBX's music ID (even any custom music)
Second section with "None" music which will be used for LUA music player.

You can even make dummy section with any default music and with instant warp which will teleport character into musical section. When we started level playthrough, SMBX will still send pause() command even if music "none", but when we started level from "none" music, SMBX will don't send music commands.

Also, music stops when you entered into another section with "none" music and after alt+tab. If you doesn't manually stopped previous music or if you don't started one of default SMBX musics, last LunaLUA's music will continue playing before you will start any default SMBX music or before you will switch Alt+Tab

P.S. By anyway I will make the "Audio" namespace in the LUA and I will put into them more functions than musicOpen(), musicPlay(), musicStop(), etc.

Re: Presenting: Lunadll for Lua!

Posted: 12 Mar 2015, 15:51
by Rednaxela
Wohlstand wrote:When we started level playthrough, SMBX will still send pause() command even if music "none", but when we started level from "none" music, SMBX will don't send music commands.
I'm thinking this quirk could be worked around if we stopped relying on the SMBX engine for music pause/resume commands. What I have in mind is, I'm pretty sure we can detect when the game engine is paused in other ways, and implement music pause/resume such that it works "as normal" even when SMBX doesn't send music commands.

Re: Presenting: Lunadll for Lua!

Posted: 12 Mar 2015, 16:06
by Wohlstand
Rednaxela wrote:
Wohlstand wrote:When we started level playthrough, SMBX will still send pause() command even if music "none", but when we started level from "none" music, SMBX will don't send music commands.
I'm thinking this quirk could be worked around if we stopped relying on the SMBX engine for music pause/resume commands. What I have in mind is, I'm pretty sure we can detect when the game engine is paused in other ways, and implement music pause/resume such that it works "as normal" even when SMBX doesn't send music commands.
With LUA we can directly send commands to SDL Mixer (to pause/stop/play/open,etc.), but SMBX Engine sends own commands which proxing to SDL Mixer and which usually preventing usage of music stream with LUA (because SMBX is trashing with play() command each tick which is really crap, therefore to use SDL with LUA we should switch SMBX's music to "none").
By anyway while I will organize audio engine in the PGE Engine, I will organize API to direct SDL Mixer usage to give LUA able to customize level music freely without SMBX's crap.

BUT, I just got a little idea: I will add commands like "seizeMusicStream(section_ID)/freeMusicStream(section_ID)" which will enable ignoring of SMBX engine commands except "pause"/"resume" commands, and SMBX will think "now playing native music" while really SDL Mixer used by LUA. Ow yea, we are sly foxes :mrgreen:
With this thing we can freely set ANY default SMBX music to section, and LUA will seize music stream, and we will play any other musics manually

EDIT: I found DUMB bug:

Code: Select all

void LuaProxy::MusicStop()
{
#ifndef NO_SDL
	PGE_MusPlayer::MUS_playMusic();
#endif
}
which I should fix now :P

Re: Presenting: Lunadll for Lua!

Posted: 12 Mar 2015, 20:01
by Hoeloe
Rednaxela wrote: A bigger problem though... is pausing. Pretty sure that'll screw up the timing method Hoeloe has shown above, because I believe it pauses the music, and eventu's wait seconds routine uses os.time which doesn't get paused. You could count SMBX frames instead, but that will be less precise for the case of not being paused.
Not a problem. Yes, eventu uses os.time, but it also takes into account the game being paused, so timers won't decrease until the game is resumed.
Rednaxela wrote: I think it would be best to extend LunaLua to allow getting the music playback state information (The SDL API doesn't let the C++ code get the playback time easily either... but it is where the music pausing is implemented so it's easy enough to make an approximation that's safe for that). I could implement that this weekend if wanted. Can also look into that music not coming back after an alt-tab issue.
That would be the best option, rather than having to fill in the length on purpose.

Horikawa Otane wrote: Nope - still utterly non-random!
Ah, I think the problem might be this:

Code: Select all

#MUSICTIME
I'm not sure that gives you the number of elements in a table. Try this:

Code: Select all

table.getn(MUSICTIME)
instead.

Re: Presenting: Lunadll for Lua!

Posted: 12 Mar 2015, 20:20
by S1eth
Question: Is onEvent(eventName) supposed to give "[NONE^" as parameter for my own events or am I doing something wrong?
I wanted to execute Lua code depending on the event I call.

Re: Presenting: Lunadll for Lua!

Posted: 12 Mar 2015, 20:28
by Hoeloe
S1eth wrote:Question: Is onEvent(eventName) supposed to give "[NONE^" as parameter for my own events or am I doing something wrong?
I wanted to execute Lua code depending on the event I call.
What's the name of your event?

Re: Presenting: Lunadll for Lua!

Posted: 12 Mar 2015, 20:42
by S1eth
Hoeloe wrote:
S1eth wrote:Question: Is onEvent(eventName) supposed to give "[NONE^" as parameter for my own events or am I doing something wrong?
I wanted to execute Lua code depending on the event I call.
What's the name of your event?
I have a coin block with Hit: "test". So it calls the "test" event. (but I also tried different names)
This calls onEvent with the parameter "[NONE^".

But then I tried chaining multiple events together with the "Trigger 0 seconds", and onEvent is called only once, and with the last event triggered in the chain. I needed to move multiple different layers in different direction through a single event, and to my understanding, that isn't possible in smbx without chaining events together. So I wanted to alter layer speed through lua and a single event.

Re: Presenting: Lunadll for Lua!

Posted: 12 Mar 2015, 21:25
by romajiQuadhash
S1eth wrote:Question: Is onEvent(eventName) supposed to give "[NONE^" as parameter for my own events or am I doing something wrong?
I wanted to execute Lua code depending on the event I call.
Are you doing

Code: Select all

function onEvent("[NONE^") 
	#code
end
or is eventName=="[NONE^" evaluating to true?

Re: Presenting: Lunadll for Lua!

Posted: 12 Mar 2015, 21:40
by S1eth
romajiQuadhash wrote:
S1eth wrote:Question: Is onEvent(eventName) supposed to give "[NONE^" as parameter for my own events or am I doing something wrong?
I wanted to execute Lua code depending on the event I call.
Are you doing

Code: Select all

function onEvent("[NONE^") 
	#code
end
or is eventName=="[NONE^" evaluating to true?
I added all event strings to a table and printed them on the screen. It's actually "[None]" in font 2 (eventName=="[None]" is true).
But still, I expected the name of the event I call as parameter, not "None" (probably the event that is set as "Trigger" in my event?)

EDIT: Yeah, I made two events. "Test" and "CalledByTest". Test triggers "CalledByTest". When I hit the coin block ("test"), onEvent is called with "CalledByTest". And if "test" triggers no other event, onEvent is called with "[None]", is this intended?

The only way onEvent("test") is used is if the trigger on "test" is set to a number other than 0, even if it triggers "[None]". And onEvent can only be called once per frame? With only 1 event string? So if I have multiple events in the same frame, I may miss them?

Code: Select all

local events = {};

function onLoop() 
	for i = 1, #events do
  		printText(i..": "..events[i],3,0,60+i*20);
	end
end

function onEvent(eventName)
	table.insert(events, eventName);
	if(eventName == "[None]") then
		printText("I JUST ADDED THIS FOR TESTING",3,0,60);
	end
end

Re: Presenting: Lunadll for Lua!

Posted: 12 Mar 2015, 23:15
by Hoeloe
Horikawa Otane wrote:

Code: Select all

function onLoad()
	math.randomseed(os.time())
	math.random();
	math.random();
	math.random();
	MusicOpen(MUSICTIME[math.random(1, table.getn(MUSICTIME))]);
	--MusicOpen(MUSICTIME[10]);
	MusicPlay();
end
This works =)
This was what I thought the problem was. This was why I ran my random operations in onLoop, because it's not always going to be run at the same time in the program. I don't know how Lua's random function works, though, so I can be certain that would have helped.

Re: Presenting: Lunadll for Lua!

Posted: 12 Mar 2015, 23:24
by Rednaxela
Pretty sure it's not running math.randomseed(os.time()) in onLoad, as opposed to in the top level of the lua file that made the difference Hoeloe.

Some pseudorandom number generators need a bit of a "warmup" generating dummy values, before the output becomes sufficiently 'random' as compared to the seed, which is why that quoted code from the lua wiki has the "math.random(); math.random(); math.random();"

Re: Presenting: Lunadll for Lua!

Posted: 12 Mar 2015, 23:37
by Wohlstand
Good news!
I just implemented the new audio API which has 33 API functions to work with sounds and musics!
https://github.com/Wohlhabend-Networks/ ... n.cpp#L235
Later there are will be available in next build, now I documenting a new stuff.
New stuff uses more useful things of SDL Mixer API, including the looped SFXed with ability to control them! Etc.
Also I was implemented the fix of annoy bug with Alt-Tab: It will work, but with new rules:
- Set ANY default music into section with LUA-music
- Add into onLoad() event the Audio.SeizeStream(xxx) (where XXX - is a section ID from 0...20. If you will write -1 - will be seized ALL sections). This function will seize music stream from SMBX engine and we can freely use music stream with LUA. This also fixing a bug: SMBX think than "Music is playing" and sends "pause" and "resume" commands to play/pause music by alt+tab switching.

Also I fixed bug with playing long SFXes after exiting from level: all custom SFXes and LUA Musics will be aborted when level/world will be closed.

Later I can bind more functions of SDL Mixer API:
http://www.libsdl.org/projects/SDL_mixe ... mixer.html

EDIT: Just documented them: http://engine.wohlnet.ru/pgewiki/LunaLu ... _functions

EDIT-2: Pre-Release patch http://engine.wohlnet.ru/docs/_laborato ... -patch.zip to test new Audio API (take latest LunaLUA package and replace files in them with files which presented in this archive)

EDIT-3: New LUA example: https://drive.google.com/open?id=0B3QNP ... authuser=0
- Random music switcher
- Panned plane flight SFX effect (please re-install patch now, panning functions was implemented today (13 march 2015))

Re: Presenting: Lunadll for Lua!

Posted: 13 Mar 2015, 01:31
by Hoeloe
Rednaxela wrote:Pretty sure it's not running math.randomseed(os.time()) in onLoad, as opposed to in the top level of the lua file that made the difference Hoeloe.

Some pseudorandom number generators need a bit of a "warmup" generating dummy values, before the output becomes sufficiently 'random' as compared to the seed, which is why that quoted code from the lua wiki has the "math.random(); math.random(); math.random();"
Yeah, that's what I meant. Calling math.randomseed(os.time()) right at program start will always produce the same value, because os.time() is predictable there. Repeated calls to "random" are not usually necessary. They're only really required under specific circumstances, to get away from the first seed value. In this case, it may be necessary because of how Lua handles its random numbers and seed generation.

Re: Presenting: Lunadll for Lua!

Posted: 13 Mar 2015, 01:50
by Rednaxela
Hoeloe wrote:Yeah, that's what I meant. Calling math.randomseed(os.time()) right at program start will always produce the same value, because os.time() is predictable there. Repeated calls to "random" are not usually necessary. They're only really required under specific circumstances, to get away from the first seed value. In this case, it may be necessary because of how Lua handles its random numbers and seed generation.
It sounds like you think it'll return a value relative to level start or program start or something. Calling os.time() right at program start will not always produce the same value. Take a look at it's output. os.time() returns the current unix timestamp in integer seconds.

To explain the symptoms Horikawa observed, I'm going to guess that Lua's choice of PRNG algorithm (and details of it's use such as seeding procedure) allows the most significant bits of the seed to be the primary influence for the first math.random(somesmallinteger) call that is run, until a few iterations of the PRNG scramble the state further.

Re: Presenting: Lunadll for Lua!

Posted: 13 Mar 2015, 11:15
by Hoeloe
Rednaxela wrote: To explain the symptoms Horikawa observed, I'm going to guess that Lua's choice of PRNG algorithm (and details of it's use such as seeding procedure) allows the most significant bits of the seed to be the primary influence for the first math.random(somesmallinteger) call that is run, until a few iterations of the PRNG scramble the state further.
Yeah, sorry, I was phrasing myself badly. It won't always return the same value, but the random calls have some state that is predictable, so produce the same random value. I'm not familiar with the exact algorithm that Lua uses, so I couldn't be more specific, but now I'm aware it's the MSBs of the value that is predictable. My point about setting randomseed was mostly that it's not necessary, because I believe the seed is already set to os.time on program start (at least if Lua is at all sensible. I've used random numbers without setting the seed, and it worked fine).

Re: Presenting: Lunadll for Lua!

Posted: 14 Mar 2015, 09:36
by Kevsoft
In the next version there will be a new "Defines"-Namespace. You can edit know global values now way easier and don't have to bother with mem calls.

Here is a sneak preview code:

Code: Select all

function onLoad()
    Defines.coinValue = 0
    Defines.coin5Value = 0
    Defines.coin20Value = 0
end
This code above will make coins worth nothing.

All new defines are already on the wiki.

Re: Presenting: Lunadll for Lua!

Posted: 14 Mar 2015, 16:40
by Kevsoft
Yes, if you need a build straight away I can give you to it.

Re: Presenting: Lunadll for Lua!

Posted: 15 Mar 2015, 01:14
by Rednaxela
Two points of order:

1) I've extended he new Lua Audio API that Wohlstand started with "AudioClock()" and "MusicClock()". These return a clock as floating point seconds, in multiples of the audio buffer length. Unlike os.clock()/os.time() these are guaranteed to stay in perfect sync with the audio engine's notion of time. The difference between "AudioClock()" and "MusicClock()" is that the former will return the total time since the game started running (including when the game is paused, as the audio engine is still running), and the later will return the time since the current song started playing (resets to 0 when the song changes, and pauses when the song is paused). For taking an action based on how long a song has been playing, MusicClock should be a rather good way as it should stay in perfect sync in the long term.

2) I've noticed that we keep a 4096 byte audio buffer in the SDL audio engine. This is ~23ms (as compared to our frame length of ~15ms) worth of latency. I find this is enough to create a noticeable lag in audio when jumping though in the past I thought this was just a SMBX thing. Today I did local builds of LunaDLL which reduce this audio buffer to 1024 or even 512 bytes, which at least for me very noticably improves how responsive the audio sounds for effects such as jumping. So I ask this... does anyone have any objections of cutting down that audio buffer to improve player experience? A potential disadvantage of cutting it back, is that there's an increased risk of crackle on slow computers, and I don't have a slow computer to test on... maybe it would be best if I made a set of test builds with a smaller buffer length for others to test? Thoughts?
EDIT: Maybe 2048 bytes (~12ms) would be a good enough compromise? Based on searching I see some people using SDL_mixer reporting crackle with 1024 but not 2048. The unfortunate thing is, I'd still call even ~12ms a rather sizeable amount of latency with respect to audio... but how small the buffer can safely be varies from computer to computer.

Re: Presenting: Lunadll for Lua!

Posted: 15 Mar 2015, 03:53
by Mabel
trying to work on a basic AI partner and it seems ok so far; throws fireballs when an NPC gets within a certain range(its a wonky setup with generators but it works) but i have one problem

if the Actor needs to teleport to Demo the generators attached to the AI Actor kinda...dissapear upon teleporting. Is there a way to keep the attached generator on the Actor when it teleports? or possibly a way to have the actor throw things without the need of generators?