[SOLVED] Using A.I. to generate Haxe code for Armory3D

:alien: Hello! I started testing out ChatGPT recently and it’s pretty interesting. Personally I don’t think it’s coding abilities are at a point to be extremely useful, or replace expert programmers. It’s especially not super helpful for me since I don’t know coding much at all (always struggled to retain the information I get from tutorials and demonstrations… in one ear out the other as they say). My question is: is this code I generated using Openai.com’s chatbot even remotely close to accurate or usable? is it worth struggling to get it to work or should I just stick to the nodes system, which is easier for me to grasp.

here is the code :zzz: (the purpose is to update the player object’s rotation only when the left joystick input changes):

package arm;

import iron.math.Quat;
import iron.system.Input;
import iron.system.GamepadEvent;

class RotatePlayerScript extends iron.App {

	private var playerObj: iron.object.Object;
	private var lastX: Float = 0.0;
	private var lastY: Float = 0.0;

	override function init() {
		super.init();
		// Find the player object by name
		playerObj = findObject("PlayerObject");
		// Listen for gamepad input events
		Input.gamepadEvent.add(onGamepadInput);
	}

	private function onGamepadInput(event: GamepadEvent) {
		if (event.type == GamepadEvent.TYPE_AXIS && (event.id == Input.LEFT_STICK_X || event.id == Input.LEFT_STICK_Y)) {
			// Get the horizontal and vertical axis values from the left joystick
			var x = Input.getGamepadAxis(event.gamepad, Input.LEFT_STICK_X);
			var y = Input.getGamepadAxis(event.gamepad, Input.LEFT_STICK_Y);

			// Only update the rotation if the joystick input has changed
			if (x != lastX || y != lastY) {
				// Calculate the angle of rotation based on the joystick input
				var angle = Math.atan2(y, x);

				// Set the rotation of the player object to face the direction of movement
				var rotation = Quat.fromAxisAngle(0, 1, 0, angle);
				playerObj.getTransform().set_rotation(rotation);

				// Store the current joystick input values
				lastX = x;
				lastY = y;
			}
		}
	}
}
1 Like

I’m not a Haxe pro, but here are some things I noticed to be incorrect (I could be mistaken):

#1

Wrong:

override function init() {

Correct:

public function new() {

#2

I think:

private var

Can simply be:

var

And likewise:

private function

Can be:

function

Basically remove all mentions of private if they don’t need to be.

#3

Wrong:

extends iron.App {

Correct:

extends Trait {

#4

Wrong:

super.init();

Correct:

super(tree);
tree.notifyOnInit(init);

#5

This:

Float = 0.0;

Should be this for optimal performance I believe:

kha.FastFloat = 0.0;

Again, I could be mistaken…

#6

This function does not exist:

findObject("PlayerObject");

It should be:

iron.Scene.active.getChild("PlayerObject");

Here’s some other ways to get a scene object with Haxe: Find objects in the scene · armory3d/armory Wiki · GitHub

1 Like

Thanks! Very helpful! I don’t know where I could go to fact check which parts need fixed or not without having to bother the forum every time I generate a new script though…

1 Like

The source from the online demos are a great way to learn Armory Haxe. So is the source from the Armory logic nodes.

Feel free to join the Armory Discord too for quicker replies and collaborations.

2 Likes

Hey! New member here, excited about armory3d, but can’t even run Blender haha :smiley: So yeah I wanted to mention the fact that ChatGPT can help you code much faster when you know how to code. It won’t come up with the sleekest and most beautiful mathematical algorithms, but hey, it does the job. And @Brelecian don’t be worried about the fact that you can’t code NOW. You will learn, my lad. The more you use it the more it becomes hard printed into your memory. It will be like muscle memory to you in a bit. Anyways, that’s all I had to say. Cya mates! <3

1 Like