How to implement go factory

I’m having trouble with implementing a go factory to spawn in enemy NPC’s, I get this error every time:

My .go file
image

My main.collection
image

This is the code in my spawning.script attached to my go factory

local p = go.get_position()
p.y = vmath.lerp(math.random(), 0, 20)
local component = "#factory"
factory.create(component, p)

This is the code for the enemy game object if that helps fix the go factory, I would also like to note that the random number generator isn’t working, it’s only a temporary thing but i would like the enemies to be able to move in random directions at the moment.

go.property("speed", 80) -- sets the speed that the player will move
direction = "down" -- sets down as the default direction
local STATE_WALKING = hash("walking")
local STATE_IDLE = hash("idle")

local Health = require("main.Data Storage") 
enemyHealth = Health.enemy_value -- creates a health variable

function init(self)
	msg.post(".", "acquire_input_focus") -- allows for inputs
	self.vel = vmath.vector3() -- indicates velocity, starts at 0

	random_move = math.random (1,4)
	print(random_move)
	self.state = STATE_WALKING

	if random_move == 1 then -- checks the input
		direction = "up"
		self.vel.y = self.speed -- adds your speed to the your velocity
			sprite.play_flipbook("#sprite", hash("U_Walk")) -- Changes Animation to walk

	elseif random_move == 2 then -- checks the input
		direction = "down"
		self.vel.y = -self.speed -- adds your speed to the your velocity
			sprite.play_flipbook("#sprite", hash("D_Walk")) -- Changes Animation to walk

	elseif random_move == 3 then -- checks the input
		direction = "left"
		self.vel.x = -self.speed -- adds your speed to the your velocity
			sprite.play_flipbook("#sprite", hash("L_Walk")) -- Changes Animation to walk

	elseif random_move == 4 then -- checks the input
		direction = "right"
		self.vel.x = self.speed -- adds your speed to the your velocity
			sprite.play_flipbook("#sprite", hash("R_Walk")) -- Changes Animation to walk
	end	

	if self.vel.x == 0 and self.vel.y == 0  then
		self.state = STATE_IDLE
		if direction == "up" then -- checks what direction the player is facing
			sprite.play_flipbook("#sprite", hash("Log_U_Idle")) -- Sets the animation to idle

		elseif direction == "down" then -- checks what direction the player is facing
			sprite.play_flipbook("#sprite", hash("Log_D_Idle"))  -- Sets the animation to idle

		elseif direction == "left" then -- checks what direction the player is facing
			sprite.play_flipbook("#sprite", hash("Log_L_Idle"))  -- Sets the animation to idle

		elseif direction == "right" then -- checks what direction the player is facing
			sprite.play_flipbook("#sprite", hash("Log_R_Idle"))  -- Sets the animation to idle
		end
	end
end

function update(self, dt)
	local pos = go.get_position() -- gets the current position and stores it

	pos.x = pos.x + speed * dt
	if pos.x < -32 then
		go.delete()
	end
	go.set_position(p)
	
	pos = pos + self.vel * dt -- adds the velocity to the position
	go.set_position(pos) -- sets the position of the game object to the new position
end

The code needs to be in a lifecycle function (e.g. init() or update()).

1 Like

does it matter which?
do i need to use a specific one?

In the context of your problem, no, it doesn’t matter which. The issue is that outside the lifecycle functions, you can’t access *.go functions.

In practice, of course it does matter. init() is probably the one you are looking for given that you are putting it on line 1.

3 Likes

thank you!

1 Like

We will try to figure our a way to add a warning when someone is doing this.

2 Likes