Creating a simple module data for character tom

Planning to put character tom health, speed in a separate module data. trying to add them to current function on the self.orient.y = 1 * tom_speed bottom part of the 2nd code. Just one for testing

-- module.lua
local tom_speed = {} -- creates a new table in the local scope
tom.value = 250
return tom_speed
local tom = require ("game.data.data")


function init(self)
	msg.post(".", "acquire_input_focus")
	self.orient = vmath.vector3()
	-- orient ---- short for orientation: the relative physical position or direction of something.
	
end

function final(self)
	
end

function update(self, dt)
	local pos = go.get_position()
	pos = pos + self.orient * dt
	go.set_position(pos)

	self.orient.x = 0
	self.orient.y = 0
end

function on_message(self, message_id, message, sender)
	
end

function on_input(self, action_id, action)

	if action_id == hash("action_up") then
		self.orient.y = 1 * tom_speed     -- not sure about this
	elseif action_id == hash("action_down") then
		self.orient.y = -100
	elseif action_id == hash("action_left") then
		self.orient.x = -100
	elseif action_id == hash("action_right") then
		self.orient.x = 100
	end
end
1 Like
2 Likes

I think you should study the basics of Lua programing first (outside of Defold), because looking at this code makes me think you dont understand what you are doing…
Don’t burn steps, you should be a bit more proficient in coding before coding inside a game engine, I think you’ll lose less time understanding defold and making mistakes.

Just try some Lua tutorials first using variables, functions and modules. When you are good on these subjects, go back to Defold.

Already went through that, didn’t get it. Even went through these

idk Ill keep trying and finding. But Im guessing maybe Ill try switching it, module is the wasd movement and tom script is the regular script with defined health and speed.

I tried the free Zenva Defold beginner course on youtube, it covers everything but modules. Ill try it.

Usually this is how you make a simple module:
1. Create a module file (e.g., module.lua in game/data/ folder)\

-- module.lua
local tom_speed = {}
tom_speed.value = 250
return tom_speed

This creates a simple module that holds a value (in this case, speed).

2.Requiring and using the module

local tom_speed = require("game.data.module")
self.orient.y = tom_speed.value -- use the value from the module

Note: require("game.data.module") looks for module.lua inside the game/data/ folder.

3. Use the module in your script

local tom_speed = require("game.data.module")  -- match the folder and filename

function init(self)
	msg.post(".", "acquire_input_focus")
	self.orient = vmath.vector3()
end

function update(self, dt)
	local pos = go.get_position()
	pos = pos + self.orient * dt
	go.set_position(pos)

	self.orient.x = 0
	self.orient.y = 0
end

function on_input(self, action_id, action)
	if action_id == hash("action_up") then
		self.orient.y = tom_speed.value
	elseif action_id == hash("action_down") then
		self.orient.y = -tom_speed.value
	elseif action_id == hash("action_left") then
		self.orient.x = -tom_speed.value
	elseif action_id == hash("action_right") then
		self.orient.x = tom_speed.value
	end
end

I hope this helps in your journey! :slight_smile:

2 Likes

WOW! Ow MY GOOOODNESS it worked!!! Thank you so much!

The Unexpected Journey from CodingWithNV Baggins!