Jump to content

Crashing on a simple IF_THEN_ELSE loop


Recommended Posts

Hi, I'm really scrub at modding DS, in .lua

 

I have a couple of popular mods out there, but mostly the were friends that helped out getting it to happen - AKA took over (in the end though, I'm very grateful for their work - as Peanut (Jeff-fa-far Dunham) would say.... NNNNGGGEEEEOOOONNNGGGGGG... (aka over my head)) I'm trying to wiggle with my first one on my own, I come from base language stuff from years ago and struggle to get my head around these open ended ideas introduced by modern programming language. So, Please Halp.

 

My editor seems to thing think that the code is OK, but DS clearly does not like it... crashes on boot. No helpful info.

 

Anywasy, I'm sure you can see what I'm trying to do, and I'm sure it's just sintax. I'm sure I may be wrong. But if I comment all the if then bits out, it loads up just great.

 

Can anyone point me? Thanks. Here is the code snippet.

local states={    State{        name = "idle",        tags = {"idle", "canrotate"},                onenter = function(inst, pushanim)            inst.Physics:Stop()            inst.AnimState:PlayAnimation("idle_loop")        end,		        if GetClock():IsDusk() then		{		timeline = 			{			TimeEvent(3*FRAMES, function(inst) inst.SoundEmitter:PlaySound("dontstarve/creatures/mandrake/walk") end),			TimeEvent(16*FRAMES, function(inst) inst.SoundEmitter:PlaySound("dontstarve/creatures/mandrake/walk") end),			},		},		end,	else		{		timeline = 			{			TimeEvent(26*FRAMES, function(inst) inst.SoundEmitter:PlaySound("dontstarve/creatures/mandrake/walk") end),			},		},	        events=        {            EventHandler("animover", function(inst) inst.sg:GoToState("idle") end),        },   },       State{        name = "death",        tags = {"busy"},                onenter = function(inst)            inst.SoundEmitter:PlaySound("dontstarve/creatures/mandrake/death")            inst.AnimState:PlayAnimation("death")            inst.Physics:Stop()            inst:AddTag("dead")        end,    },    

The idea is to make him noisy at dusk, and slow it down at night. Truth is, I played with the multiplier, and found 26 is my highest I can go before I get straight silence. I have not yet figured it out, or what 'frames' are (fps? game calculation frames? Most likely that one.)

 

So, for bonus points, anyone know why I get silence if the multiplier goes 27+? What I'd really like to do is have him chirp for a few seconds every 30 or so if idle.I think I'm going to slow down the yipyap at dusk, too, if i can figure out how to exploit that calculation and whatever it means, properly.

 

Thanks for any help, guys;

 

 

Link to comment
Share on other sites


        if GetClock():IsDusk() then        {        timeline =            {            TimeEvent(3*FRAMES, function(inst) inst.SoundEmitter:PlaySound("dontstarve/creatures/mandrake/walk") end),            TimeEvent(16*FRAMES, function(inst) inst.SoundEmitter:PlaySound("dontstarve/creatures/mandrake/walk") end),            },        },        end,    else        {        timeline =            {            TimeEvent(26*FRAMES, function(inst) inst.SoundEmitter:PlaySound("dontstarve/creatures/mandrake/walk") end),            },        },

 

You have several problems there.  First of all is that curly brackets are used to define tables, not blocks of code.  You seem to be using them to the if/else blocks.  Secondly, that "end" appears to be the orphaned remnant of a function that no longer exists.  Thirdly, you can't start an if/else(or any other control statement) in the middle of defining a table.  You can, however, define an anonymous function to do the test and return the value you want.

 

You would do that similarly to this:

-- This creates an anonymous function and executes it immediately.  The desired timeline is the return value.timeline =     (function()         if GetClock():IsDusk() then            return {                TimeEvent(3*FRAMES, function(inst) inst.SoundEmitter:PlaySound("dontstarve/creatures/mandrake/walk") end),                TimeEvent(16*FRAMES, function(inst) inst.SoundEmitter:PlaySound("dontstarve/creatures/mandrake/walk") end)            }        else            return { TimeEvent(26*FRAMES, function(inst) inst.SoundEmitter:PlaySound("dontstarve/creatures/mandrake/walk") end) }        end    end)()
Link to comment
Share on other sites

@hotmatrixx,

 

Whoops, forgot to answer the other part.  FRAMES is a constant set to 1/30 so that when timing things on a frame-by-frame basis, you don't have to write xxx/30 every time.

 

Your sound event most likely doesn't fire at 27+ frames because the idle animation is under 27 frames.  This normally isn't a problem because you generally want to sync sound up with some sort of animation, or else it's just an ephemeral sound that seems to play for no reason.  If you really don't care about matching the chirping sound up to an animation, you can use a PeriodicTask to initiate the chirps.  Inside the task function, you can use inst:HasStateTag("idle") to make sure it's in the idle state.

 

I would still recommend syncing sound with an animation, though.

Link to comment
Share on other sites

@Corrosive;

 

Firstly, Thanks for being that awesome kind of awesome. I'm really out of my depth with lua, and I'm so used to thinking in linear terms, that it's  a real big leap for this old dog to try and learn a new trick or two. OK, one new trick would be nice.

 

So, thanks so much for being helpful.

 

I plugged your function straight into my game to try and test it (I made a back up of the orig file, and wrote this directly onto the SGMandrake.lua) - basically just trying to replace the current call with your function. It seems to return that I am missing a } near 'events' to close the { on line 24. If I knew where the debug file was I'd copy-paste it for you.

 

I'll continue to muck around with it for a bit, but I'm definately out of my depth here, and it seems like it should be such a simple thing!

 

I'm expecting that the sound file is actually some kind of script withiin itself, that can randomly pick from a soundbank, giving my little man the unique sound that makes him, him.

 

 

I'll use the 26xFRAMES for now to quiet him down, but this is just the first part of a fairly interesting mod I'd like to make for the little guy that just tweaks some of his behaviours. I'll give you more info if you're interested.

Link to comment
Share on other sites

Honestly, Ima gonna give up on it. I don't have the time or inclination to learn a new language now, I'm too old and I just don't care enough.

 

I'm going to find someone that is better at this than me, and just pay them to do what I want. You get first refusal. I'd like to create a mod that modifies several behaviours for the Mandrake. I don't think it's going to be too difficult to do. If you are interested, I'll give you the full list, we can close this thread, and we can just go from there.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...