Question about alternate ways of coding. Topic is solved

Ask gaming related questions
Louis Tully
Posts: 31
Joined: 25 Apr 2018, 03:03

Question about alternate ways of coding.

09 Jun 2018, 05:48

Hello, everyone.

Like many, I've got a very basic understanding of coding with AHK. The thing I want to work on is learning a different approach to writing. Since I have no experience with any coding languages, my approach to problems and tasks is to reproduce keystrokes and mouse clicks when I make a script. It gets the job done but I want to get better.

I like to use games as a way to learn and, earlier this week, my kid got me into playing Minecraft. It's a pretty fun game, basically digital Lego building. I thought it would be cool to figure out a way to build a structure using AHK.


Here's my auto wall builder code and it works well. I was just wondering if there's a different way to write it, a different approach? I don't know if that makes sense. Sorry. lol Maybe Minecraft is a bad example.

Image


Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

wolf_II
Posts: 1975
Joined: 08 Feb 2015, 20:55

Re: Question about alternate ways of coding.

09 Jun 2018, 06:34

A possible approach would be: Try to make a function out of existing code, and figure out how you want the script to use the function.

Example use: Numpad1:: BuildWall(5, 4). Now the challenge is to write this function.
That function should not change what happens in MineCraft when user presses the hotkey Numpad1!

I don't want to spoil your fun, so I ask instead: is this sufficient to get you started?

Hint: I suggest to use indentation, otherwise things get ugly fast.
swagfag
Posts: 965
Joined: 11 Jan 2017, 17:59

Re: Question about alternate ways of coding.

09 Jun 2018, 10:59

wolf_ii pretty much provided u with an outline of what to do, but i figure this might be useful for others skimming the thread at some point in the future,
so heres how id go about refactoring your function:
1. Indentation

2. DRY

3. Functions

4. More DRY

5. More functions

6. Finalize


and now u can build a wall of any dimensions in minecraft
of course its not necessary to go through all the steps. if all u wanted was a single wall type, sure, get rid of the algorithm and hardcode it in.
maybe u need 10 different types of walls and youre strapped for time, hardcode it in.
but the next time u CTRL + C to add the 11th wall in, give it some thought before hitting V

take care
Louis Tully
Posts: 31
Joined: 25 Apr 2018, 03:03

Re: Question about alternate ways of coding.

10 Jun 2018, 03:32

wolf_II wrote:...


swagfag wrote:...



Thank you guys so much.

Swag...

That's great looking code. That's the kind of stuff I'm hoping to understand and write one day instead of a wall of repeating text. The code above buildWall is next level for me. The buildWall code is next-next level, or more. lol I understand what it's doing but... damn. It's going to take a while to get to that one. I actually didn't know about fall damage, believe it or not. I've only played in creative mode so far.


I've got some questions, but don't answer just yet, I'm "hitting the books" so to speak.

I don't understand how the moveRight () and moveLeft () sections get the specifics from the move(strafe, counterStrafe) section. Anyway...
wolf_II
Posts: 1975
Joined: 08 Feb 2015, 20:55

Re: Question about alternate ways of coding.

10 Jun 2018, 03:42

Before you head for the books, be inspired by a possible aim, such as build a DW20 starter house (4 walls to form a 9x9, 7x7 inside) without a roof.
I think a height limit of 4 will not cause you fall damage, unless that got changed since I played last.

Edit: I just noticed in a spoiler above a confirmation of height limit.
Last edited by wolf_II on 10 Jun 2018, 06:17, edited 1 time in total.
Louis Tully
Posts: 31
Joined: 25 Apr 2018, 03:03

Re: Question about alternate ways of coding.

10 Jun 2018, 04:38

We're on the same page. lol Coding definitely works better when you've got a goal. I was already tinkering with that idea before I made this thread.

At the beginning of the script I've got to specify a direction first because, and I might be wrong, there isn't just a generic "Turn Left 90°" command for this game, correct?. It only has north south east and west.

I was using this to get my character squared up so it would build in a straight line. How I get those lines into a script like Swag's is still over my head. But I'll work on it. I'll be back with tons of noob questions though without a doubt.

Code: [Select all] [Download] GeSHi © Codebox Plus

Send, t
sleep 500
send /tp @p ~ ~ ~ 90 ~
sleep 500
Send {Enter}


That's another thing... I've got to look up how to disable the mouse while the script is running because if it gets bumped, the direction gets changed. I set the script to build a massive wall and walked away. When I checked on it after a few minutes later it was all chaos. Apparently, our cat walked across the table and hit the mouse.
wolf_II
Posts: 1975
Joined: 08 Feb 2015, 20:55

Re: Question about alternate ways of coding.

10 Jun 2018, 05:13

Louis Tully wrote:Apparently, our cat walked across the table and hit the mouse.
ROFL see BlockInput

I find it useful to divide and conquer: make the coding part easier by shifting responsibility.
For example: I you can educate your users to only use the hotkey AFTER they made sure of best possible alignment, then you don't have to code alignment.
Since you are the only user, no problem. write easy code, make it work, then re-consider coding alignment.
In short, delay what you don't momentarily need and review later.

Is there a orientation option in the teleport command in MineCraft?
You might be able to use that, as you already said, but I don't know either, sry.

Otherwise build a wall with Character moving North, South, East, West.
by making a function for BuildWall(Width, Height, Direction)
Louis Tully
Posts: 31
Joined: 25 Apr 2018, 03:03

Re: Question about alternate ways of coding.

11 Jun 2018, 04:06

wolf_II wrote:
Louis Tully wrote:Since you are the only user, no problem. write easy code, make it work, then re-consider coding alignment.
In short, delay what you don't momentarily need and review later.



That makes sense. Baby Steps by Dr Leo Marvin. lol



Image


Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

wolf_II
Posts: 1975
Joined: 08 Feb 2015, 20:55

Re: Question about alternate ways of coding.

11 Jun 2018, 04:15

Congratulations.
How about refactoring all "faceXXX()"-functions to a single "face(Direction)"-function next?
Louis Tully
Posts: 31
Joined: 25 Apr 2018, 03:03

Re: Question about alternate ways of coding.

11 Jun 2018, 06:48

wolf_II wrote:Congratulations.
How about refactoring all "faceXXX()"-functions to a single "face(Direction)"-function next?



Thanks, man.

I'm having a bit of trouble with that one and my google attempts aren't so hot either. It's tough to search for what you don't know. This is what I've got and it's no good.


Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

wolf_II
Posts: 1975
Joined: 08 Feb 2015, 20:55

Re: Question about alternate ways of coding.

11 Jun 2018, 06:53

Very close. I only inserted a few double quotes. untested.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Louis Tully
Posts: 31
Joined: 25 Apr 2018, 03:03

Re: Question about alternate ways of coding.

12 Jun 2018, 08:39

Man... I was pretty close.

I cleaned it up a bit and changed the movement a little. For some reason, with the other movement lines, it would move the character a little too far to the right after the second stack of blocks. Now it's more precise.


Here's what I've got now.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

wolf_II
Posts: 1975
Joined: 08 Feb 2015, 20:55

Re: Question about alternate ways of coding.  Topic is solved

12 Jun 2018, 08:57

Example for compacting: apply DRY principle on common lines need not be repeated.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Louis Tully
Posts: 31
Joined: 25 Apr 2018, 03:03

Re: Question about alternate ways of coding.

12 Jun 2018, 09:38

Doh! Good point. Thanks. Sorry if this is amateur hour, I appreciate the help.



In Swag's code, how do the move left and right lines work? I don't get how "a" "d" %strafe% and %counterStrafe% interact.


Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Then there this wizardry:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



If I didn't need to worry about a height limit and just built the structure one row, turn, one row, turn, etc... kind of like a spring, what do I look into to understand that kind of thing?
swagfag
Posts: 965
Joined: 11 Jan 2017, 17:59

Re: Question about alternate ways of coding.

12 Jun 2018, 16:45

read about: https://autohotkey.com/docs/Functions.htm#intro

as for the second question, the algorithm is modeled after what youve show in your first gif. Build a partial W x SAFE_HEIGTH sized wall going right, then repeat it going left and keep repeating building partial wall going left/right until the desired height is achieved

verbose:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Louis Tully
Posts: 31
Joined: 25 Apr 2018, 03:03

Re: Question about alternate ways of coding.

18 Jun 2018, 02:24

wolf_II wrote:...

swagfag wrote:...



Well, damnit! I was going to checkmark posts from both of you guys but it only let the first one through. Sorry. Just wanted to thank you both for being so helpful. I appreciate the time you took to point me in the right direction.

Cheers guys
neomulemi6
Posts: 216
Joined: 30 Jun 2016, 06:01

Re: Question about alternate ways of coding.

19 Jun 2018, 15:03

Just commenting to say that What About Bob is a fantastic movie, and it makes me proud to see somebody making ~30 year old movie references.
User avatar
gregster
Posts: 1352
Joined: 30 Sep 2013, 06:48

Re: Question about alternate ways of coding.

19 Jun 2018, 16:00

neomulemi6 wrote:Just commenting to say that What About Bob is a fantastic movie, and it makes me proud to see somebody making ~30 year old movie references.
I see, I just had noticed the 'Ghostbusters' reference :)
Louis Tully
Posts: 31
Joined: 25 Apr 2018, 03:03

Re: Question about alternate ways of coding.

19 Jun 2018, 17:54

gregster wrote:I see, I just had noticed the 'Ghostbusters' reference :)


neomulemi6 wrote:Just commenting to say that What About Bob is a fantastic movie, and it makes me proud to see somebody making ~30 year old movie references.


Image



Just stickin' with the good stuff.

Return to “Gaming”

Who is online

Users browsing this forum: No registered users and 8 guests