Page 1 of 1

Can't clamp a variable value through Lua script in node

Posted: Wed Mar 14, 2018 2:58 pm
by Abelius
Hi there,

I'm trying to make this work as a script in a node...:

Code: Select all

Variable["Lust"] = Variable["Lust"] + math.clamp((Variable["MaxLustCenvirHJ"]-Variable["Lust"]),0,Variable["LustAddCenvirHJ"])
However, it is throwing me this error output...:

Code: Select all

Dialogue System: Lua code 'Variable["Lust"] = Variable["Lust"] + math.clamp((Variable["MaxLustCenvirHJ"]-Variable["Lust"]),0,Variable["LustAddCenvirHJ"])' threw exception 'Tried to invoke a function call on a non-function value. If you're calling a function, is it registered with Lua?'
UnityEngine.Debug:LogError(Object)
PixelCrushers.DialogueSystem.Lua:RunRaw(String, Boolean, Boolean)
PixelCrushers.DialogueSystem.Lua:Run(String, Boolean, Boolean)
PixelCrushers.DialogueSystem.ConversationModel:GetState(DialogueEntry, Boolean, Boolean, Boolean)
PixelCrushers.DialogueSystem.ConversationModel:GetState(DialogueEntry)
PixelCrushers.DialogueSystem.ConversationController:OnFinishedSubtitle(Object, EventArgs)
PixelCrushers.DialogueSystem.ConversationView:FinishSubtitle()
PixelCrushers.DialogueSystem.ConversationView:HandleContinueButtonClick()
PixelCrushers.DialogueSystem.ConversationView:OnConversationContinue(IDialogueUI)
UnityEngine.Component:SendMessage(String, Object, SendMessageOptions)
PixelCrushers.DialogueSystem.AbstractDialogueUI:OnContinueConversation()
PixelCrushers.DialogueSystem.AbstractDialogueUI:OnContinue()
UnityEngine.EventSystems.EventSystem:Update()
I understand it is a somewhat complex call, but I need it working to save me from a world of pain...

Now, after some research I've found out that math.clamp is not really a function that exists in Lua scripting (?) :lol:

I know there are two functions called .ceil and .floor, but as I'll get negative numbers that should always be converted to zero, I don't think they're useful to me. In fact, I'm extremely surprised that math.clamp is not a standard function in Lua. :?

So... I'm wondering if there's some way to implement it manually so I can use it in DS node scripts...?

Thanks!

Edit: just realized that I already asked something very similar in the past.

I guess I could add an intermediate step to "process" the number in question with an FSM component (waiting for a "done" message before continuing), but... don't you think a built in clamp function would be useful to have in DS? :roll:

Re: Can't clamp a variable value through Lua script in node

Posted: Wed Mar 14, 2018 3:18 pm
by Tony Li
Hi,

You could add a script like this to the Dialogue Manager. It adds a function clamp(value,min,max).

ExtraLuaMath.cs

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

public class ExtraLuaMath : MonoBehaviour {

    void OnEnable() {
        Lua.RegisterFunction("clamp", this, 
            SymbolExtensions.GetMethodInfo(() => clamp((double)0, (double)0, (double)0)));
    }

    void OnDisable() {
        Lua.UnregisterFunction("clamp");
    }

    public double clamp(double value, double min, double max) { // (Lua uses doubles)
        return Mathf.Clamp((float)value, (float)min, (float)max);
    }
}
I don't mind adding new built-in functions that are obviously not native Lua functions, such as SetQuestState(), but I'm hesitant to add a new built-in math function because it may confuse people as to what's valid Lua and what isn't.

Re: Can't clamp a variable value through Lua script in node

Posted: Wed Mar 14, 2018 3:29 pm
by Abelius
Wow, you're so blindingly fast replying to posts...! :lol:

I understand your reserves. In fact, I was kind of confused at first because I searched for that function in Google, and some results showed up... but they were from a game mod. :roll:

Anyway, I'll try to use that script. I'm guessing I need to add it to the Dialogue Manager object? Here...?

Image

Thank you so much!

Re: Can't clamp a variable value through Lua script in node

Posted: Wed Mar 14, 2018 3:44 pm
by Tony Li
When the stars are in alignment, I can occasionally be fast. But if it's past 10 PM EDT, you'll be waiting 9-10 hours. :-)

Yes, that's the place to add it. Alternatively, you could make it a static class and register the clamp() function in its constructor. But I prefer to make it a MonoBehaviour and add it to the Dialogue Manager so I'm actually aware that I've added it.

Re: Can't clamp a variable value through Lua script in node

Posted: Wed Mar 14, 2018 3:57 pm
by Abelius
Okay, I'll try to capitalize your attention then. :lol:

I've got it working, but partially.

I have reasons to think that this part in my script...:

Variable["Lust"] = Variable["Lust"] + clamp((Variable["MaxLustCenvirHJ"]-Variable["Lust"]),0,Variable["LustAddCenvirHJ"])

...returns a zero value, regardless of their values (they were 220 and 203 respectively, in testing).

If I put the numbers instead of the variables, it works properly.

Is there something I can do, short of doing that operation before the clamp function is executed?

Re: Can't clamp a variable value through Lua script in node

Posted: Wed Mar 14, 2018 4:04 pm
by Tony Li
Can you try adding spaces around the minus sign? LuaInterpreter might be having a parser problem.

Code: Select all

Variable["Lust"] = Variable["Lust"] + clamp((Variable["MaxLustCenvirHJ"] - Variable["Lust"]),0,Variable["LustAddCenvirHJ"])

Re: Can't clamp a variable value through Lua script in node

Posted: Wed Mar 14, 2018 4:17 pm
by Abelius
I've tried that and even putting a variable only (just for testing), like this...:

Code: Select all

Variable["Lust"] = Variable["Lust"] + clamp(Variable["number"],0,Variable["LustAddCenvirHJ"])
(number is 15)

...to no avail.

It seems that it can't do anything to return variable values in that specific parameter... :? (because the min and max params are fine)

That's a little problem for me because the whole point of this was to clamp a variable value, not a static one. :lol:

Re: Can't clamp a variable value through Lua script in node

Posted: Wed Mar 14, 2018 5:40 pm
by Tony Li
I just tested the script, and it appears to work correctly. Try adding a Lua Console (or use the Watches tab) and testing the values. Is it possible that Variable["LustAddCenvirHJ"] is zero or invalid? Or that there's a typo in your expression?

Try checking each part. When using the Lua Console, I initially had a typo in the line:

Code: Select all

return Variable["MaxLustCenvirHJ"]
I had accidentally typed:

Code: Select all

return Variable["MaxLustAddCenvirHJ"]
It returned "nil", indicating an invalid variable. When evaluated as a number, it's treated as 0.

Re: Can't clamp a variable value through Lua script in node

Posted: Thu Mar 15, 2018 7:33 am
by Abelius
Oh gods... I'm so fucking stupid... yeah, it was a typo. :oops:

Sorry about it, it seems I was too tired last evening. It's working flawlessly now, thank you for your patience and support! :P

Re: Can't clamp a variable value through Lua script in node

Posted: Thu Mar 15, 2018 9:36 am
by Tony Li
No worries; I typoed it myself, which is what made me think of that possibility. :-) That's why I added dropdowns for the more common actions such as simple assignments and checks.