Projectiles, Global Variables and Other Questions

Hey Everyone,

I’m coming to Defold from GameMaker Studio and a couple things I can’t figure out are projectiles and global variables.

In GameMaker Studio you can use instance_create() and it will create a certain object at the given x and y coordinates, this is the first thing I can’t figure out in Defold. I’ve looked at the tutorial code and looked on the forums and saw something about factories but they made no sense to me. I’d basically just like a simple bit of code that says “if space was pressed then create bullet”. The if statement I can do but as I said I can’t figure out how to create a new instance of an object.

The other thing I can’t quite figure out yet (which sorta relates to my first problem) is global variables. What I’d like to do is when a bullet is created have in its init() something like “direction = player.direction”. I heard something about properties but I’m not sure if other objects can access the properties or how to do it.

It would be greatly appreciated if you guys could help me out, thanks :slight_smile:

Ok, so the way you solve this is through what we call factories. A factory is a component that can at runtime spawn/create game objects. What this means is that you can have a bullet game object and a factory that creates those game objects. I’ll create an example for you today. Hold tight!

I’ve updated my “Rotate and Move” example and added the ability to fire bullets as well. The bullets are fired in the direction in which the player is facing.

CODE: https://github.com/britzl/publicexamples/tree/master/examples/rotate_and_move
DEMO: http://britzl.github.io/publicexamples/rotate_and_move/index.html

The bullets are spawned on this line: https://github.com/britzl/publicexamples/blob/master/examples/rotate_and_move/rotate_and_move.script#L62
And moved here (based in the rotation/direction): https://github.com/britzl/publicexamples/blob/master/examples/rotate_and_move/bullet.script#L7-L11

3 Likes

Thanks for the example code, it did help clear a few things up, but I still don’t quite understand how factories work. I see how to create objects with a factory, but how do you set up a factory? I assume it’s a component that you add to an object but are you meant to add it to the object that’s going to use it? Or do you have it in a completely different object?

Factories are components just like everything else in Defold. When you create a factory you specify a game object that it is supposed to create.

Factories are explained in detail in the Factories section of the manual.

You can have the factory anywhere you want to. It could be on the player game object like in my example, but it could also be somewhere else, perhaps on a bullet spawner game object of some kind. You decide which factory to use when you call factory.create:

factory.create("some_gameobject#id_of_factory")

Thanks for the help :slight_smile: I just went back in to my project and successfully added in rudimentary projectiles. When I press the space button a bullet is created and in the bullet code it just moves to the left at a speed of 5 and gets destroyed if it goes off the screen.

I haven’t added in the rotation yet because of the way I’ve set up my player. Basically when an arrow key is pressed the direction gets changed to 1 (up), 2 (right), 3 (down) or 4 (left) and when none of them are being pressed then the direction is 0 (idle) then I have my movement code based on that. I’ll see if I can do a bit more research on global variables and see what I can come up with :slight_smile:

1 Like

Don’t! :slight_smile:

If the player is spawning projectiles and you want to pass on some information about the player, for instance the direction, there is a better way of doing this than to set a global variable.

In your bullet.script you can expose properties that can be set when the bullet is created. You do this using go.property(). Like this:

bullet.script:

go.property("direction", 0)  -- default value is 0

function update(self, dt)
	print("Do stuff with", self.direction)
end

player.script:

function on_input(self, action_id, action)
	if action_id == hash("fire") and action.pressed then
		-- spawn bullet at player position with no rotation and direction set to 3
		factory.create("#factory", go.get_position(), nil, { direction = 3 })
	end
end
3 Likes

Thanks for the example code, makes a lot of sense :slight_smile: Just one more thing, in the “{ direction = 3 }” part is there a way to set that to a local variable within the player.script file? Like if I had the player’s direction as “direction” and the bullet’s direction as “dir” can I do “{ dir = direction }”?

If the player’s direction is in a local variable “direction” then yes. But you likely have the player’s direction in a property or a self member, then you do { dir = self.direction }.

Lua table syntax has the left hand side being the key name and the right hand side being an expression:

local value = 10
local t = { some_key = 1234, another_key = 1234 + value }

if you want to create a key from an expression you can do that like this:

local key = "some_key"
local t = { [key] = 1234, [key .. "_2"] = 5678 }
pprint(t)

gives:

DEBUG:SCRIPT: 
{
  some_key_2 = 5678,
  some_key = 1234,
}
1 Like

Mikael has answered the question, but judging by the question itself I believe you should read up a bit on the Lua language itself, and how it is used in Defold. A very good starting point is this manual: Lua programming in Defold

2 Likes

8 posts were split to a new topic: War Battles tutorial - Rocket positions are wrong! (SOLVED)