Jump to content

How to create a code generator?

ArmswieldTheHero

I know the languages python and C#, what I need is to make a program, preferably with a GUI, that can take parameters and make a specific code out of them.

 

Let me be more specific, I have a game I'm creating in unity, and the movement system teleports you to an adjacent empty gameobject (pretty much an invisible gameobject placed at a specific location) based on your input and location, but the drawback to this system is that you have to code every single square, I was fine just making the movement, but now that its time to make attacks that's brought up some extreme problems, like for example to code a gun that shoots down a row. I have to say


If (player.transform.position == west.transform.position && enemyplayer.transform.position == west1,transform.position)

{

     //register a hit

}

 Which isnt a big deal, except for the fact that I have to do this for every single panel the player AND the enemy can stand on. So what I need is a generator where I can place the player on the grid and click all the squares he can hit and then click generate and it spits out a code.

Link to comment
Share on other sites

Link to post
Share on other sites

What you're trying to do is called "object oriented programming", so maybe you need to learn more python and C#?

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Enderman said:

What you're trying to do is called "object oriented programming", so maybe you need to learn more python and C#?

I'd love to learn, can you point me in the right direction for some references where I can learn how to do that? I'm already more than halfway done creating the game, I just need a little help for this part.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, ArmswieldTheHero said:

I'd love to learn, can you point me in the right direction for some references where I can learn how to do that? I'm already more than halfway done creating the game, I just need a little help for this part.

Object oriented programming is a huge part of any programming language, you should definitely spend a lot of time learning how to do this.

https://www.tutorialspoint.com/python/python_classes_objects.htm

In your case, each square would have the same "square" class and all you need to do is write one piece of code which will apply to all panels. Then each panel will take care of itself in detecting hits.

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Enderman said:

Object oriented programming is a huge part of any programming language, you should definitely spend a lot of time learning how to do this.

https://www.tutorialspoint.com/python/python_classes_objects.htm

In your case, each square would have the same "square" class and all you need to do is write one piece of code which will apply to all panels. Then each panel will take care of itself in detecting hits.

Could I get a reference in C#? Python has always been a struggle for me. And right now my brain has been in "C# mode" For unity.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, ArmswieldTheHero said:

Could I get a reference in C#? Python has always been a struggle for me. And right now my brain has been in "C# mode" For unity.

http://www.blackwasp.co.uk/csharpobjectoriented.aspx

http://csharp.net-tutorials.com/classes/introduction/

 

Also lynda.com has classes if you want :)

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Enderman said:

That doesnt answer my question at all. I want to know how to generate a code based on parameters given with a GUI, I already have the movement system made and I dont want to rewrite it because it already works on photon unity networking.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, ArmswieldTheHero said:

That doesnt answer my question at all. I want to know how to generate a code based on parameters given with a GUI, I already have the movement system made and I dont want to rewrite it because it already works on photon unity networking.

So you want to write a code which writes code for you.

Have you ever thought about why we can't just make computers code themselves?

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, Enderman said:

So you want to write a code which writes code for you.

Have you ever thought about why we can't just make computers code themselves?

Ok inform me, why is it impossible to make a computer write

if player.transform.position ==  west.transform.position && enemyplayer.transform.position == west1.transform.position

"

when I drag my player onto the west panel and click on the west1 panel in a GUI?

Link to comment
Share on other sites

Link to post
Share on other sites

If you have a ton of code reuse, you need to rethink how you're coding. Your design isn't sufficient enough for what you're doing. You have to think about the common actions you're doing, the parameters it takes, and the output it spits. There's no getting around having to manually write something for every event handler (I'm just going to assume you want something to happen whenever a player goes on an object), but you don't have to copy and paste code if you have code designed for re-use. You call maybe one or two things, instead of having dozens of lines that describe the same thing.

 

The method you're describing sounds like a way to shortcut copying, pasting, and editing. This is still terrible design. What if you have to change the behavior of the thing? Now you have to change x amount of copies of that code.

Link to comment
Share on other sites

Link to post
Share on other sites

32 minutes ago, M.Yurizaki said:

If you have a ton of code reuse, you need to rethink how you're coding. Your design isn't sufficient enough for what you're doing. You have to think about the common actions you're doing, the parameters it takes, and the output it spits. There's no getting around having to manually write something for every event handler (I'm just going to assume you want something to happen whenever a player goes on an object), but you don't have to copy and paste code if you have code designed for re-use. You call maybe one or two things, instead of having dozens of lines that describe the same thing.

 

The method you're describing sounds like a way to shortcut copying, pasting, and editing. This is still terrible design. What if you have to change the behavior of the thing? Now you have to change x amount of copies of that code.

Well I already posted a question on unity answers as to how I make grid movement, no reply. So I did the only thing I knew. I'll ask you instead. How do I make grid movement in unity?

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, ArmswieldTheHero said:

Well I already posted a question on unity answers as to how I make grid movement, no reply. So I did the only thing I knew. I'll ask you instead. How do I make grid movement in unity?

I don't know Unity. But I can give you something generic and maybe you can use it to help your case.

 

If I were to have a grid based movement system, with 2D movement coordinates, then I would have a 2D array (in C this would be something like int coordinates[200][200]). When the user does an input, I move the player object in the appropriate direction. So it would be something:

  • Going up: player.location.y = player.location.y - 1
  • Going down: player.location.y = player.location.y + 1
  • Going left: player.location.x = player.location.x - 1
  • Going right: player.location.x = player.location.x + 1

If an enemy is attacking an entire row, you'd go something like:

if ( attack.x == player.x ) {
  /* Do something. */
}

You have to think about your coordinate system in terms of some kind of graph that you can easily manipulate. Even if your physical representation is something like the Risk board game, you can deduce it into some abstract for that makes it easy for a computer to process and for you to program.

Link to comment
Share on other sites

Link to post
Share on other sites

17 minutes ago, M.Yurizaki said:

I don't know Unity. But I can give you something generic and maybe you can use it to help your case.

 

If I were to have a grid based movement system, with 2D movement coordinates, then I would have a 2D array (in C this would be something like int coordinates[200][200]). When the user does an input, I move the player object in the appropriate direction. So it would be something:

  • Going up: player.location.y = player.location.y - 1
  • Going down: player.location.y = player.location.y + 1
  • Going left: player.location.x = player.location.x - 1
  • Going right: player.location.x = player.location.x + 1

If an enemy is attacking an entire row, you'd go something like:


if ( attack.x == player.x ) {
  /* Do something. */
}

You have to think about your coordinate system in terms of some kind of graph that you can easily manipulate. Even if your physical representation is something like the Risk board game, you can deduce it into some abstract for that makes it easy for a computer to process and for you to program.

Thank you!

Link to comment
Share on other sites

Link to post
Share on other sites

20 minutes ago, M.Yurizaki said:

I don't know Unity. But I can give you something generic and maybe you can use it to help your case.

 

If I were to have a grid based movement system, with 2D movement coordinates, then I would have a 2D array (in C this would be something like int coordinates[200][200]). When the user does an input, I move the player object in the appropriate direction. So it would be something:

  • Going up: player.location.y = player.location.y - 1
  • Going down: player.location.y = player.location.y + 1
  • Going left: player.location.x = player.location.x - 1
  • Going right: player.location.x = player.location.x + 1

If an enemy is attacking an entire row, you'd go something like:


if ( attack.x == player.x ) {
  /* Do something. */
}

You have to think about your coordinate system in terms of some kind of graph that you can easily manipulate. Even if your physical representation is something like the Risk board game, you can deduce it into some abstract for that makes it easy for a computer to process and for you to program.

Also, How would I make it to where the player cant move past a certain point? 

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, ArmswieldTheHero said:

Also, How would I make it to where the player cant move past a certain point? 

Bounds checking. Say your maximum coordinate is 200,200. Then you would do:

/* Player is trying to move down */
if (player.location.y == 200) {
  /* Do something */
}

/* Player is trying to move up */
if (player.location.y == 0) {
  /* Do something */
}

/* Player is trying to move right */
if (player.location.x == 200) {
  /* Do something */
}

/* Player is trying to move left */
if (player.location.x == 0) {
  /* Do something */
}

Though you shouldn't hard code the numbers like I did.

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, M.Yurizaki said:

Bounds checking. Say your maximum coordinate is 200,200. Then you would do:


/* Player is trying to move down */
if (player.location.y == 200) {
  /* Do something */
}

/* Player is trying to move up */
if (player.location.y == 0) {
  /* Do something */
}

/* Player is trying to move right */
if (player.location.x == 200) {
  /* Do something */
}

/* Player is trying to move left */
if (player.location.x == 0) {
  /* Do something */
}

Though you shouldn't hard code the numbers like I did.

So instead of hard coding the numbers, make a variable with the bounds in them and reference the variable instead?

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, ArmswieldTheHero said:

So instead of hard coding the numbers, make a variable with the bounds in them and reference the variable instead?

Yes. You could name them something like "min_coordinate" and "max_coordinate." In practice, unless the value is actually a constant (like pi or a conversion value), don't hard code numbers. And even then, you should give them names so you know what they are. i.e., magic numbers are bad :)

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, M.Yurizaki said:

Yes. You could name them something like "min_coordinate" and "max_coordinate." In practice, unless the value is actually a constant (like pi or a conversion value), don't hard code numbers. And even then, you should give them names so you know what they are. i.e., magic numbers are bad :)

Hey would it be too much for me to ask for your skype (if you have one) so I can ask you questions quickly and easily? I'd love to have somebody I can ask anytime for help, so that I dont have to bug an entire forum.

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, ArmswieldTheHero said:

Hey would it be too much for me to ask for your skype (if you have one) so I can ask you questions quickly and easily? I'd love to have somebody I can ask anytime for help, so that I dont have to bug an entire forum.

You can PM me if you'd like.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×