Page 1 of 1

A bit of help regarding Lua Basics

Posted: Mon Mar 08, 2021 10:43 am
by LunarCatUSA
1. How do I create a variable and set it to zero?
2. How do I make it so that hitting a block will add to that variable?
3. How do I display an in-game message via scripting?
4. How do I make a script check variables? (Specifically equal to and greater than)
5. How do I kill the player via scripting. (Or should I just do that via event?)

I’m very new to Lua and would like some help if it’s not too much trouble.

Re: A bit of help regarding Lua Basics

Posted: Mon Mar 08, 2021 12:39 pm
by Emral
LunarCatUSA wrote:
Mon Mar 08, 2021 10:43 am
1. How do I create a variable and set it to zero?
local myVar = 0
LunarCatUSA wrote:
Mon Mar 08, 2021 10:43 am
2. How do I make it so that hitting a block will add to that variable?
function onBlockHit()
myVar = myVar + 1
end
LunarCatUSA wrote:
Mon Mar 08, 2021 10:43 am
3. How do I display an in-game message via scripting?
function onDraw()
Text.print("Hello", 100, 100)
end
LunarCatUSA wrote:
Mon Mar 08, 2021 10:43 am
4. How do I make a script check variables? (Specifically equal to and greater than)
function onDraw()
if myVar > 5 or myVar == 2 then
Text.print("Hello", 100, 100)
end
end
LunarCatUSA wrote:
Mon Mar 08, 2021 10:43 am
5. How do I kill the player via scripting. (Or should I just do that via event?)
function onStart()
player:kill()
end

Re: A bit of help regarding Lua Basics

Posted: Mon Mar 08, 2021 5:49 pm
by LunarCatUSA
Is there anything I should put after naming my variable? Like the semicolon?
When I run it, it gives me an error 13, syntax message.

Added in 27 minutes 59 seconds:
Also, I’ve been using 1.4.4’s scripting. I’m wondering if I should update to SMBX2. I’m kind of worried I might have to start everything over as it tells me my levels are a different version.

Re: A bit of help regarding Lua Basics

Posted: Mon Mar 08, 2021 8:31 pm
by Hoeloe
So-called "1.4.4" isn't technically a version of SMBX, and doesn't support Lua at all. It's a separate program called SMBX38A, and has no connection to SMBX2 (and only a tangential one to 1.3, since it was a recreation of that). Confusing? Yes, but that's how it is.

Re: A bit of help regarding Lua Basics

Posted: Mon Mar 08, 2021 8:39 pm
by LunarCatUSA
Guess I better transfer all my level files to 2.0 then.

Added in 6 minutes 41 seconds:
That being said, say I wanted a message box to appear as soon as the game started. I know that would involve the function:
onStart
And then I’d add text.print(“Welcome... blah blah blah”, 100, 100)
And then an end

I’m trying to make that appear, but I’m getting all sorts of odd errors.

Added in 27 minutes 36 seconds:
Oh nevermind, I’ve figured it out.

Added in 16 minutes 35 seconds:
Alright, next question:
- I want to make 10 questions for a quiz.
- A variable (qnumber) is going to be randomized to select one of the 10 questions.
- The answers will be entered via the player’s keyboard input.
- Should the time to answer the question (qtimer) reach 0, the question is dropped and move on to the next one.

As of now, I just wish to know how to randomize numbers.
And I imagine the keys are like, player.altkeyleft or player.a key or something among those lines?

Added in 1 hour 55 minutes 36 seconds:
Update: Okay, so how does the onEvent() work?
Does it trigger when the event occurs? Because the event is supposed to set the question number to a number from the question bank, but I’m not triggering the if-statement I have for that question number.

Re: A bit of help regarding Lua Basics

Posted: Tue Mar 09, 2021 6:01 am
by Marioman2007
LunarCatUSA wrote:
Mon Mar 08, 2021 11:25 pm
Update: Okay, so how does the onEvent() work?
Does it trigger when the event occurs? Because the event is supposed to set the question number to a number from the question bank, but I’m not triggering the if-statement I have for that question number.
take look at this code:

Code: Select all

function onEvent(eventname)
    if eventname == "your event name here" then    
        Audio.playSFX("your sound name here.ogg")
    end
end
However it cannot be used to draw images of print text permanently, because the event runs only for a second.

Re: A bit of help regarding Lua Basics

Posted: Tue Mar 09, 2021 6:44 am
by LunarCatUSA
Okay then, the thing is. I’ll need the text to appear for at least 5 seconds before it disappears. Wonder what I can use for that.

Re: A bit of help regarding Lua Basics

Posted: Tue Mar 09, 2021 11:46 am
by Hoeloe
You can use a timer, set it to around 320, decrement it once every frame, and print some text as long as it's larger than 0.

Re: A bit of help regarding Lua Basics

Posted: Tue Mar 09, 2021 12:23 pm
by LunarCatUSA
Hmm okay then,
I imagine it’s the
onTick()
qtimer = qtimer-1
end

And do I add an IF statement for when it gets to zero that sets its back to 320?
I also plan to add a random number generator for one of seven responses that quiz host will give for taking too long. I imagine it’s like RNG.random(7) or something like that.

Re: A bit of help regarding Lua Basics

Posted: Tue Mar 09, 2021 12:26 pm
by Hoeloe
RNG.random(7) will give a random decimal number between 0 and 7. You likely want RNG.randomInt(7), or better yet, a table with all the possible responses as entries and RNG.irandomEntry to select one.

Re: A bit of help regarding Lua Basics

Posted: Tue Mar 09, 2021 12:36 pm
by LunarCatUSA
Ah so make an array, something like

LateRespTab = [1, 2, 3, 4, 5, 6, 7]

Or should they be strings? (I’ve only worked with tables like this in MatLab.
And then the RNG thing will be like

LongRespNum = RNG.randomInt(1,7)
If LongRespNum = LateRespTab(1)
Trigger.event(“TooLong”)
end

And then the game displays a text box of the host yelling at us for taking too long.

Added in 20 minutes 28 seconds:
Okay new question, how do I end and reset the timer?

Added in 10 minutes 33 seconds:
Ah nevermind, just made a table of strings containing the event names, all I have to do is call an index for them. I just really need to know how to stop and reset that damn the timer while I have the onTick() going.

Added in 1 hour 43 minutes 39 seconds:
Okay, great news! The “too long” timer and response thing is working well. Now I just need to add:
- A button input that will interrupt the timer and allow the player to answer.
- 3 button inputs from keyboard to enter an answer.
- A chance for one of the two AI opponents to answer the question.
- And then some system for their intelligence (chance of them getting the question correct)

Re: A bit of help regarding Lua Basics

Posted: Wed Mar 10, 2021 10:43 am
by LunarCatUSA
Okay so some new trouble popped up this morning.
I’m trying to make it so that a message remains as long as a timer is above zero.

The thing is, the message disappears but the timer is not zero yet. I’d like to know how to remedy this.

I’m using:
remain = timer.get()
qnumber == 1 and remain > 0 then
(Text here)
end

Re: A bit of help regarding Lua Basics

Posted: Wed Mar 10, 2021 11:28 am
by Hoeloe
I think you need to go through some basic Lua tutorials. The code you are writing here does not make any sense. When I said to use a timer, I literally told you how to do that. I did not say to use the timer library, which 1) exists to add a level timer that kills the player when it runs out and 2) is a basegame feature that does not need to be loaded.

I strongly suggest going over some beginner Lua tutorials or you are going to have serious problems with getting anything to work.

Re: A bit of help regarding Lua Basics

Posted: Wed Mar 10, 2021 11:32 am
by LunarCatUSA
It’s alright, found something else that works, I just went back to my original timer thing and used onTick() and a display for that which used intergers instead of ticks via the math.floor() so I’m good there.

And any tutorial I look up shows me a video showcase that doesn’t go over the script at all and is just for show. I’m best at learning via example, not description really.

Added in 33 minutes 7 seconds:
Sorry to bother again but, this is what I understand based on the knowledge given from my tutorials:
I make:
function onKeyboardPress(“some key code here”)
I should note that the player is currently in an event where their controls are dropped so that might be the problem

Then I run the game, but when it comes to the event where the player has to press a button on their keyboard,
I get warning messages on the upper left hand screen that say:

“Warning: Overwritten handler ‘onKeyboardPress’ in worlds/etc”

Re: A bit of help regarding Lua Basics

Posted: Thu Mar 11, 2021 8:42 am
by LunarCatUSA
Okay, thanks for the help everyone.
I managed to find a good Lua guide online. I already got most of the gameplay commands given above but now that I actually found a pretty good documentation source, I should be good now. The player is able to answer questions and get them right or wrong or whatever. Now I just need to work on the opponent AI.

Re: A bit of help regarding Lua Basics

Posted: Fri Mar 12, 2021 6:07 pm
by LunarCatUSA
So I’ve been looking through Lua for a certain algorithm but I’m having trouble.
Let’s say I have a table of 5 strings, but I want to shuffle only the 2-4 elements and keep the 1st and 5th untouched. I’ve seen the Fisher-Yates function, but I don’t know how to keep the first and last element untouched.

Re: A bit of help regarding Lua Basics

Posted: Sat Mar 13, 2021 2:58 am
by Emral
LunarCatUSA wrote:
Fri Mar 12, 2021 6:07 pm
So I’ve been looking through Lua for a certain algorithm but I’m having trouble.
Let’s say I have a table of 5 strings, but I want to shuffle only the 2-4 elements and keep the 1st and 5th untouched. I’ve seen the Fisher-Yates function, but I don’t know how to keep the first and last element untouched.
You could keep the first and last element outside of the table, stored elsewhere.

Re: A bit of help regarding Lua Basics

Posted: Tue Mar 16, 2021 4:14 pm
by LunarCatUSA
Unrelated to my main project, but is there any way I can make an event field for an enemy. Say that an enemy has a field of light around them in a circular field. And I want to trigger an event for if the player enters the field of light from this npc. Anyway I can go about doing this?

Re: A bit of help regarding Lua Basics

Posted: Tue Mar 16, 2021 5:29 pm
by Emral
LunarCatUSA wrote:
Tue Mar 16, 2021 4:14 pm
Unrelated to my main project, but is there any way I can make an event field for an enemy. Say that an enemy has a field of light around them in a circular field. And I want to trigger an event for if the player enters the field of light from this npc. Anyway I can go about doing this?
Create a circle collider in code and center its x and y on the center of the NPC. The collision check is then a Colliders.collide between the player and the npc's collider.