How do i test this code?

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

How do i test this code?

by Stix » Mon Feb 26, 2018 02:23

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
GreenDimond
Member
 
Posts: 1293
Joined: Wed Oct 28, 2015 01:26
Location: SCP-3008
GitHub: GreenXenith
IRC: GreenDimond
In-game: GreenDimond

Re: How do i test this code?

by GreenDimond » Mon Feb 26, 2018 04:47

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 GreenDimond on Mon Feb 26, 2018 15:45, edited 1 time in total.
Y▹uTube | Mods | Patre●n | Twitter | Minetest Discord | GreenDimond#3232

Hey, you. You're finally awake.
You were trying to view their profile, right? Found the rest of their signature, same as us, and that guest over there.
 

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

Re: How do i test this code?

by sofar » Mon Feb 26, 2018 06:11

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: 2102
Joined: Fri Jan 16, 2015 07:31
GitHub: sofar
IRC: sofar
In-game: sofar

Re: How do i test this code?

by sofar » Mon Feb 26, 2018 06:13

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: 2345
Joined: Wed Oct 19, 2016 15:18
Location: Russia, Moscow
GitHub: Andrey2470T
In-game: Andrey01

Re: How do i test this code?

by Andrey01 » Mon Feb 26, 2018 08:06

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: 2336
Joined: Wed Sep 17, 2014 11:21
Location: Germany
In-game: Linuxdirk

Re: How do i test this code?

by Linuxdirk » Mon Feb 26, 2018 10:40

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: 601
Joined: Thu Oct 13, 2016 10:45
Location: DE

Re: How do i test this code?

by hajo » Mon Feb 26, 2018 10:54

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: 2336
Joined: Wed Sep 17, 2014 11:21
Location: Germany
In-game: Linuxdirk

Re: How do i test this code?

by Linuxdirk » Mon Feb 26, 2018 11:23

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: 1379
Joined: Fri Aug 04, 2017 14:19
Location: USA
IRC: nil
In-game: Stix [+alts]

Re: How do i test this code?

by Stix » Mon Feb 26, 2018 15:40

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
GreenDimond
Member
 
Posts: 1293
Joined: Wed Oct 28, 2015 01:26
Location: SCP-3008
GitHub: GreenXenith
IRC: GreenDimond
In-game: GreenDimond

Re: How do i test this code?

by GreenDimond » Mon Feb 26, 2018 15:46

Stix wrote:*ifconfig

Whoops. #TyposFTW.
Y▹uTube | Mods | Patre●n | Twitter | Minetest Discord | GreenDimond#3232

Hey, you. You're finally awake.
You were trying to view their profile, right? Found the rest of their signature, same as us, and that guest over there.
 

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

Re: How do i test this code?

by Andrey01 » Mon Feb 26, 2018 18:42

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: 2102
Joined: Fri Jan 16, 2015 07:31
GitHub: sofar
IRC: sofar
In-game: sofar

Re: How do i test this code?

by sofar » Mon Feb 26, 2018 18:53

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: 2345
Joined: Wed Oct 19, 2016 15:18
Location: Russia, Moscow
GitHub: Andrey2470T
In-game: Andrey01

Re: How do i test this code?

by Andrey01 » Mon Feb 26, 2018 19:07

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: 1379
Joined: Fri Aug 04, 2017 14:19
Location: USA
IRC: nil
In-game: Stix [+alts]

Re: How do i test this code?

by Stix » Mon Feb 26, 2018 19:21

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: 2345
Joined: Wed Oct 19, 2016 15:18
Location: Russia, Moscow
GitHub: Andrey2470T
In-game: Andrey01

Re: How do i test this code?

by Andrey01 » Mon Feb 26, 2018 19:35

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: 2336
Joined: Wed Sep 17, 2014 11:21
Location: Germany
In-game: Linuxdirk

Re: How do i test this code?

by Linuxdirk » Mon Feb 26, 2018 19:59

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: 1379
Joined: Fri Aug 04, 2017 14:19
Location: USA
IRC: nil
In-game: Stix [+alts]

Re: How do i test this code?

by Stix » Mon Feb 26, 2018 20:02

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

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

Re: How do i test this code?

by sofar » Mon Feb 26, 2018 23:46

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: 2102
Joined: Fri Jan 16, 2015 07:31
GitHub: sofar
IRC: sofar
In-game: sofar

Re: How do i test this code?

by sofar » Mon Feb 26, 2018 23:46

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

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

Re: How do i test this code?

by Stix » Mon Feb 26, 2018 23:54

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: 3080
Joined: Sat Sep 29, 2012 01:14
IRC: Inocudom
In-game: Inocudom

Re: How do i test this code?

by Inocudom » Tue Feb 27, 2018 19:03

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.
You can now find my videos at BitChute: https://www.bitchute.com/channel/some_cheeky_jinuskian/
 

shivajiva
Member
 
Posts: 29
Joined: Fri Nov 27, 2015 09:41
In-game: shivajiva

Re: How do i test this code?

by shivajiva » Sun Mar 11, 2018 16:35

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
 

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

Re: How do i test this code?

by Stix » Wed Jul 25, 2018 21:11

Xrafede wrote:lol poor CoolJar10

CoolJar10 is evil...
Hey, what can i say? I'm the bad guy.
 


Return to General Discussion



Who is online

Users browsing this forum: No registered users and 2 guests