Help finishing my game from tutorial

It does not look like the gist contain this line of code. Can you post the script?

ok so Iā€™m making a code that spawns the tanks at the beginning? the game object is at 0,0,0. I was trying to get them to continuously spawn

yeah i was trying a lot of different ways ill get that real quick

local function random_position(position)
	return vmath.vector3(math.random(1, sys.get_config("display.width")),  math.random(1, sys.get_config("display.height")), 0)
end
local function spawn_enemy(position)
	return factory.create("#enemy_factory", random_position())
end
local function spawn_medic(position)
	return factory.create("#medic_factory", random_position())
end
local function spawn_helicopter(position)
	return factory.create("#helicopter_factory", random_position())
end

local function spawn_tank(position)
	return factory.create("#tank_factory", random_position())
end
local function spawn_powerup(position)
	return factory.create("#powerup_factory", random_position())
end

function init(self)
	math.randomseed(os.time())
	self.spawned_tank =factory.create("#tank_factory",tank_position,nil,{parent=msg.url() })
	self.tank = {}
	self.powerup = {}
	self.medic={}
	self.helicopter={}
	self.enemy={}
	for i = 1, 10 do
		local id = factory.create("#tank_factory", go.get_position(), go.get_rotation())
		--table.insert(self.tank, id)
		--local p = go.get_position()
		--p.y = vmath.lerp(math.random(), min_y, max_y)
		--local component = "#tank_factory"
		--factory.create(component, p)
		table.insert(self.tank, id)
	end
	msg.post(".", "accuire_input_focus")
end

function final(self)
	msg.post(".", "release_input_focus")
end
function update(self, dt)
	if message_id == hash("drone_dead") then
		self.spawed_drone = nil
	elseif messag_id==("collision_response") then 
		table.insert(self.tank, id(vmath.vector3(random_position(40,996),random_position(40,996),0)))
		if #self.tank==0 then
			msg.post("controller:/controller","show_menu")
		end
	end
end

function on_input(self, action_id, action)

end

Ok. The error is Lua telling you that you try to count the number of integer-indexed entries of a table, but the variable you supply is not a table (but nil).

~/Documents/projects/site [master]% lua
Lua 5.3.4  Copyright (C) 1994-2017 Lua.org, PUC-Rio
> t = {1, 2, 3}
> #t
3
> u = nil -- for clarity.
> #u
stdin:1: attempt to get length of a nil value (global 'u')
stack traceback:
	stdin:1: in main chunk
	[C]: in ?
``

oh ok that makes sense, what about my controller script how come i can load the title screen or game but if i load one then it wonā€™t load the other

But I wonder if that code in update runs at all:

function update(self, dt)
	if message_id == hash("drone_dead") then
		self.spawed_drone = nil
	elseif messag_id==("collision_response") then 
		table.insert(self.tank, id(vmath.vector3(random_position(40,996),random_position(40,996),0)))
		if #self.tank==0 then
			msg.post("controller:/controller","show_menu")
		end
	end
end

First of all, there is no parameter ā€œmessage_idā€ coming in to update() so the comparison will be against a global ā€œmessage_idā€ and ā€œmessag_idā€. Unless you set these somewhere (and you should not do that!) those will be nil.

Second, and most important: the code in update() looks like it belongs in an on_message() function. You should really spend some time reading up on message passing in Defold. It is crucial that you understand how it works. See https://www.defold.com/manuals/message-passing/

2 Likes

that was some code from the documentation on the website i donā€™t know how that got there

Can you point to where you found that code?

What error message do you get in the console?

close to the bottom, and i have been reading it all day. I just started learning this code 3 days ago from using unity a year ago, i was taking 3d modeling and animation at school until my recent graduation and it is also 3 in the morning here when you are awake over there so iā€™m usually tired and itā€™s not like i have a lot of people i can ask during the summer break, tomorrow i can write down my questions i have while im working so im more prepared

The code on the page is not what you have in your script:

function on_message(self, message_id, message, sender)
    if message_id == hash("drone_dead") then
        self.spawed_drone = nil
    end
end

This defines an on_message() function that is a special function in Defold. When you send messages to a script component that function is called with the parameters set to the message id, message data and sender.

update() is a different special function that is called once each frame by the engine. If you define it in your script code (as you have), it is called with a single parameter (apart from ā€œselfā€) that is set to the delta time since last call (ā€œdtā€). There is no messages going into this function.

2 Likes

i think i miss placed some of the code when i was rearranging it and commenting out things, i ment to put ā€œtank_deadā€ and self.spawned_tank there. i was playing around with some code snipets from the manual i was trying to incorporate a raycast for the ai. I was thinking the music script just needs to be instantiated and it will work and for the spawning maybe i could do a loop and just make sure the objects are deleted after they are spawned and destroyed so it doesnā€™t loop forever stacking objects until it crashes. iā€™ll work on it tomorrow when i wake up and look at more examples, that helps i didnā€™t know i had on message functions that were in updates instead also I havenā€™t tried to see if youtube has anybody besides yall that talks about defold i can probably find some more helpful information

what do you mean the code on the page is not what you have in your script? it is the exact same code lol look above dynamic loading with factory resources

oh my lordy, so he was dieing and i was like whats going on either its spawning tanks that have no sprite or i thought the tanks are spawned on the game object but the game object was at 0,0,0 so i put it at 0,0,1 and it works thereā€™s a lot of tanks all over the map

I was referring to your code. The one I pasted was from the manual.

Here is your code:

function update(self, dt)
	if message_id == hash("drone_dead") then
		self.spawed_drone = nil
	elseif messag_id==("collision_response") then 
		table.insert(self.tank, id(vmath.vector3(random_position(40,996),random_position(40,996),0)))
		if #self.tank==0 then
			msg.post("controller:/controller","show_menu")
		end
	end
end

And here is the code from the manual:

function on_message(self, message_id, message, sender)
    if message_id == hash("drone_dead") then
        self.spawed_drone = nil
    end
end
2 Likes

I didnā€™t have a lot of time today to work on my project, but i was reading up on lua and the main difference from c# the original language i learned programming with is that it doesnā€™t use inheritance. however the script can still be used similar to inheritance with metabables. also you donā€™t have to specify arrayā€™s. It is actually pretty easy to understand, Iā€™m still new with game programming like when you call functions every frame in the update and then when youā€™re passing messages. It just really helps when you can ask somebody to explain something to you a little more when you donā€™t understand it completely.

hey guys long time no talk, sorry about that! OK, so I have gotten my game quite far and found a lot of stuff on github from yall to look at that i didnā€™t know was there. Now i understand why my questions seemed annoying to answer because they were answered and I wasnā€™t looking hard enough. Anyways Iā€™m still having a problem with my title proxy not loading my game proxy, but i can initialize both of them like if i run the game first instead of title it still runs the game but when i run the title and click play it gives me the error that the ui script canā€™t post message to the controller script. Also you have told me this before but now i think i understand so when it talks about returning a nil value that means its looking for a table value right and to correct that i would need to make something like local units={helicopter, speed=1}, something like that where i make a table and populate it with units (more besides helicopter just writing off the top of my head and i woke up lol) but i make the unit id and stats in the table and then i can uses them when preforming arithmetic like calculating damage and health

Youā€™re welcome to zip the project and share it with me (bjorn.ritzl@king.com) so I can take a look.

1 Like

cool man, iā€™ll get on that after memorial day