How do i test this code?

Post Reply
User avatar
Stix
Member
Posts: 1385
Joined: Fri Aug 04, 2017 14:19
IRC: nil
In-game: Stix [+alts]
Location: USA

How do i test this code?

by Stix » Post

Hi! i have just written some simple code from scratch when i realized: how am i gonna test it? So got any ideas?
Heres the code:

Code: Select all

 player = "CoolJar10"

if player == "stix"
 then
print("Hiya Stix :P")
 end
if player == "CoolJar10"
 then
print("uh-oh...")
 end
(player can be changed manually to CoolJar10 to get different msg)
Last edited by Stix on Tue Feb 27, 2018 00:49, edited 4 times in total.
Hey, what can i say? I'm the bad guy.

User avatar
GreenXenith
Member
Posts: 1356
Joined: Wed Oct 28, 2015 01:26
GitHub: GreenXenith
Location: UTC-8:00
Contact:

Re: How do i test this code?

by GreenXenith » Post

Open Minetest, select a world with your mod enabled, tick "host server" and log in as "stix". Open a terminal and type "ipconfig" (windows I think) or "ifconfig" (Linux). Find your IP (should be something like 192.168.1.###). Open a second client and connect to a server with the name "CoolJar10" using your IP as the address and leave the port at 30000.
Last edited by GreenXenith on Mon Feb 26, 2018 15:45, edited 1 time in total.
YouTube | Mods | Patreon | Minetest Discord @greenxenith

You should not be able to read this message.

sofar
Developer
Posts: 2146
Joined: Fri Jan 16, 2015 07:31
GitHub: sofar
IRC: sofar
In-game: sofar

Re: How do i test this code?

by sofar » Post

Code: Select all

if player = "stix"
this is a typical mistake for new programmers. the `=` symbol assigns the value on the right to the variable on the left. It doesn't compare values. You want to use `==` here instead.

sofar
Developer
Posts: 2146
Joined: Fri Jan 16, 2015 07:31
GitHub: sofar
IRC: sofar
In-game: sofar

Re: How do i test this code?

by sofar » Post

when you're staring to goof around with lua, I advise you to play around with the interpreter in an interactive session, or use a lua playground.

To use lua interactively, you can just run `lua` from a shell and voila, you're in a lua interpreter.

A good lua sandbox can be used in a webbrowser from here: https://www.lua.org/cgi-bin/demo

User avatar
Andrey01
Member
Posts: 2577
Joined: Wed Oct 19, 2016 15:18
GitHub: Andrey2470T
In-game: Andrey01
Location: Russia, Moscow

Re: How do i test this code?

by Andrey01 » Post

Stix wrote:Hi! i have just written some simple code from scratch when i realized: how am i gonna test it? So got any ideas?
Heres the code:

Code: Select all

player = "stix"

if player = "stix"
 then
print("Hiya Stix :P")
if player = "CoolJar10"
 then
print("uh-oh...")
(player can be changed manually to CoolJar10 to get different msg)
I`ve noticed some errors in your code. It should be rewritten so:

Code: Select all

mes1 = "Hiya Stix :P"
mes2 = "uh-oh..."

if player == "stix" then
print(mes1)

elseif player == "CoolJar10" then
print(mes2)

User avatar
Linuxdirk
Member
Posts: 3219
Joined: Wed Sep 17, 2014 11:21
In-game: Linuxdirk
Location: Germany
Contact:

Re: How do i test this code?

by Linuxdirk » Post

Andrey01 wrote:
Stix wrote:Hi! i have just written some simple code from scratch when i realized: how am i gonna test it? So got any ideas?
Heres the code:

Code: Select all

player = "stix"

if player = "stix"
 then
print("Hiya Stix :P")
if player = "CoolJar10"
 then
print("uh-oh...")
(player can be changed manually to CoolJar10 to get different msg)
I`ve noticed some errors in your code. It should be rewritten so:

Code: Select all

mes1 = "Hiya Stix :P"
mes2 = "uh-oh..."

if player == "stix" then
print(mes1)

elseif player == "CoolJar10" then
print(mes2)
Oh god, please no. At least use proper indentation.

hajo
Member
Posts: 606
Joined: Thu Oct 13, 2016 10:45
Location: DE
Contact:

Re: How do i test this code?

by hajo » Post

sofar wrote:lua sandbox can be used in a webbrowser from https://www.lua.org/cgi-bin/demo
ideone is another online-ide that supports lua and many other programming-languages.
Also, you can program basic_robots in lua, while playing minetest.

User avatar
Linuxdirk
Member
Posts: 3219
Joined: Wed Sep 17, 2014 11:21
In-game: Linuxdirk
Location: Germany
Contact:

Re: How do i test this code?

by Linuxdirk » Post

hajo wrote:
sofar wrote:lua sandbox can be used in a webbrowser from https://www.lua.org/cgi-bin/demo
ideone is another online-ide that supports lua and many other programming-languages.
I prefer repl.it (not ad infested): https://repl.it/languages/lua

User avatar
Stix
Member
Posts: 1385
Joined: Fri Aug 04, 2017 14:19
IRC: nil
In-game: Stix [+alts]
Location: USA

Re: How do i test this code?

by Stix » Post

GreenDimond wrote:Open Minetest, select a world with your mod enabled, tick "host server" and log in as "stix". Open a terminal and type "ipconfig" (windows I think) or "ifconfic" (Linux). Find your IP (should be something like 192.168.1.###). Open a second client and connect to a server with the name "CoolJar10" using your IP as the address and leave the port at 30000.
*ifconfig; i know how to find ip's :P
BTW: thx!
Hey, what can i say? I'm the bad guy.

User avatar
GreenXenith
Member
Posts: 1356
Joined: Wed Oct 28, 2015 01:26
GitHub: GreenXenith
Location: UTC-8:00
Contact:

Re: How do i test this code?

by GreenXenith » Post

Stix wrote: *ifconfig
Whoops. #TyposFTW.
YouTube | Mods | Patreon | Minetest Discord @greenxenith

You should not be able to read this message.

User avatar
Andrey01
Member
Posts: 2577
Joined: Wed Oct 19, 2016 15:18
GitHub: Andrey2470T
In-game: Andrey01
Location: Russia, Moscow

Re: How do i test this code?

by Andrey01 » Post

Linuxdirk wrote:
Andrey01 wrote:
Stix wrote:Hi! i have just written some simple code from scratch when i realized: how am i gonna test it? So got any ideas?
Heres the code:

Code: Select all

player = "stix"

if player = "stix"
 then
print("Hiya Stix :P")
if player = "CoolJar10"
 then
print("uh-oh...")
(player can be changed manually to CoolJar10 to get different msg)
I`ve noticed some errors in your code. It should be rewritten so:

Code: Select all

mes1 = "Hiya Stix :P"
mes2 = "uh-oh..."

if player == "stix" then
print(mes1)

elseif player == "CoolJar10" then
print(mes2)
Oh god, please no. At least use proper indentation.
What`s an indentation?

sofar
Developer
Posts: 2146
Joined: Fri Jan 16, 2015 07:31
GitHub: sofar
IRC: sofar
In-game: sofar

Re: How do i test this code?

by sofar » Post

Andrey01 wrote:What`s an indentation?
It's about treating code with care and avoiding mistakes by "working cleanly".

You don't write:

Code: Select all

if bar    ==3 then print    ("banana")
end
Instead, you write:

Code: Select all

if bar == 3 then
        print("banana")
end

User avatar
Andrey01
Member
Posts: 2577
Joined: Wed Oct 19, 2016 15:18
GitHub: Andrey2470T
In-game: Andrey01
Location: Russia, Moscow

Re: How do i test this code?

by Andrey01 » Post

sofar wrote:
Andrey01 wrote:What`s an indentation?
It's about treating code with care and avoiding mistakes by "working cleanly".

You don't write:

Code: Select all

if bar    ==3 then print    ("banana")
end
Instead, you write:

Code: Select all

if bar == 3 then
        print("banana")
end
That is i wrote the code wrong? Really are indentations so important? I looked through many times a code that people had written and i saw they wrote as me now without those indentations.

User avatar
Stix
Member
Posts: 1385
Joined: Fri Aug 04, 2017 14:19
IRC: nil
In-game: Stix [+alts]
Location: USA

Re: How do i test this code?

by Stix » Post

Andrey01 wrote:
Stix wrote:Hi! i have just written some simple code from scratch when i realized: how am i gonna test it? So got any ideas?
Heres the code:

Code: Select all

player = "stix"

if player = "stix"
 then
print("Hiya Stix :P")
if player = "CoolJar10"
 then
print("uh-oh...")
(player can be changed manually to CoolJar10 to get different msg)
I`ve noticed some errors in your code. It should be rewritten so:

Code: Select all

mes1 = "Hiya Stix :P"
mes2 = "uh-oh..."

if player == "stix" then
print(mes1)

elseif player == "CoolJar10" then
print(mes2)
how is my code wrong? either way should work.
Hey, what can i say? I'm the bad guy.

User avatar
Andrey01
Member
Posts: 2577
Joined: Wed Oct 19, 2016 15:18
GitHub: Andrey2470T
In-game: Andrey01
Location: Russia, Moscow

Re: How do i test this code?

by Andrey01 » Post

Stix wrote:
Andrey01 wrote:
Stix wrote:Hi! i have just written some simple code from scratch when i realized: how am i gonna test it? So got any ideas?
Heres the code:

Code: Select all

player = "stix"

if player = "stix"
 then
print("Hiya Stix :P")
if player = "CoolJar10"
 then
print("uh-oh...")
(player can be changed manually to CoolJar10 to get different msg)
I`ve noticed some errors in your code. It should be rewritten so:

Code: Select all

mes1 = "Hiya Stix :P"
mes2 = "uh-oh..."

if player == "stix" then
print(mes1)

elseif player == "CoolJar10" then
print(mes2)
how is my code wrong? either way should work.
You made an error in that line "if player = "stix"". Here you are trying to set a value for player variable. But you need to compare value of variable player and "stix". Here is your error.

User avatar
Linuxdirk
Member
Posts: 3219
Joined: Wed Sep 17, 2014 11:21
In-game: Linuxdirk
Location: Germany
Contact:

Re: How do i test this code?

by Linuxdirk » Post

Andrey01 wrote:Really are indentations so important?
Since Lua does not use indentation as syntax element (like Python for example) technically indentation is not important. But over time some conventions took place.

If you roughly adapt to this it makes it easier for everyone to read the code.

User avatar
Stix
Member
Posts: 1385
Joined: Fri Aug 04, 2017 14:19
IRC: nil
In-game: Stix [+alts]
Location: USA

Re: How do i test this code?

by Stix » Post

Andrey01: i had already changes the '=' to '=='
Hey, what can i say? I'm the bad guy.

sofar
Developer
Posts: 2146
Joined: Fri Jan 16, 2015 07:31
GitHub: sofar
IRC: sofar
In-game: sofar

Re: How do i test this code?

by sofar » Post

Andrey01 wrote:That is i wrote the code wrong? Really are indentations so important? I looked through many times a code that people had written and i saw they wrote as me now without those indentations.
I'd probably lose my job if I coded without indentation. I'm not joking, it's just unacceptable in a professional environment to be lazy and not properly format, clean, and make your code presentable.

For those who are just starting out programming: Keeping your code clean helps you, and others, later to understand what you are writing, and shows that you took care to make it presentable. This is critical if it ever needs to be changed, or worse, if it needs fixing. Would you feel comfortable living next to a nuclear reactor that was half-painted and had all sorts of mismatched colors and half finished roofs? How about driving a car where each seat has a different material, the steering wheel is "roughly on the left" and the right front wheel is half the size as the others? Awkward? Then you should also expect code to be decent and organized, debugged and presentable :). If not then you're either living in a former USSR state or too dangerous to be near anyone I care about :).

sofar
Developer
Posts: 2146
Joined: Fri Jan 16, 2015 07:31
GitHub: sofar
IRC: sofar
In-game: sofar

Re: How do i test this code?

by sofar » Post

I'm somewhat shocked nobody has pointed out the missing `end` yet, btw. :D

User avatar
Stix
Member
Posts: 1385
Joined: Fri Aug 04, 2017 14:19
IRC: nil
In-game: Stix [+alts]
Location: USA

Re: How do i test this code?

by Stix » Post

sofar wrote:I'm somewhat shocked nobody has pointed out the missing `end` yet, btw. :D
omg how did i miss that! (will fix asap)
Hey, what can i say? I'm the bad guy.

User avatar
Inocudom
Member
Posts: 3121
Joined: Sat Sep 29, 2012 01:14
IRC: Inocudom
In-game: Inocudom

Re: How do i test this code?

by Inocudom » Post

sofar wrote:
Andrey01 wrote:That is i wrote the code wrong? Really are indentations so important? I looked through many times a code that people had written and i saw they wrote as me now without those indentations.
I'd probably lose my job if I coded without indentation. I'm not joking, it's just unacceptable in a professional environment to be lazy and not properly format, clean, and make your code presentable.

For those who are just starting out programming: Keeping your code clean helps you, and others, later to understand what you are writing, and shows that you took care to make it presentable. This is critical if it ever needs to be changed, or worse, if it needs fixing. Would you feel comfortable living next to a nuclear reactor that was half-painted and had all sorts of mismatched colors and half finished roofs? How about driving a car where each seat has a different material, the steering wheel is "roughly on the left" and the right front wheel is half the size as the others? Awkward? Then you should also expect code to be decent and organized, debugged and presentable :). If not then you're either living in a former USSR state or too dangerous to be near anyone I care about :).
Video game developers have to bow to these vowels and syllables too, I am sure. This is especially true for employees of Nintendo.

User avatar
shivajiva
Member
Posts: 32
Joined: Fri Nov 27, 2015 09:41
In-game: shivajiva

Re: How do i test this code?

by shivajiva » Post

Have you considered ZeroBraneStudio to test and debug simple lua code whilst you are learning?

BTW I think you are trying to do this...

Code: Select all

minetest.register_on_joinplayer(function(player)
    local name = player:get_player_name()
    local msg
    if name == "stix" then
        msg = "Hiya Stix :P"
    elseif  name == "CoolJar10" then
        msg = "uh-oh..."
    end
    If msg then
        minetest.after(1, minetest.chat_send_all, msg)
    end
end)
The print statement sends the text to std out so it wouldn't appear in the chat console when running as a mod. I'm using minetest.after to create a small delay before sending the msg to the chat console of all connected clients. You could use minetest.chat_send_player to make it target the player that is joining if you preferred.

Xrafede
New member
Posts: 1
Joined: Wed Jul 25, 2018 17:32

Re: How do i test this code?

by Xrafede » Post

lol poor CoolJar10

User avatar
Stix
Member
Posts: 1385
Joined: Fri Aug 04, 2017 14:19
IRC: nil
In-game: Stix [+alts]
Location: USA

Re: How do i test this code?

by Stix » Post

Xrafede wrote:lol poor CoolJar10
CoolJar10 is evil...
Hey, what can i say? I'm the bad guy.

Post Reply

Who is online

Users browsing this forum: No registered users and 40 guests