I've been making some "test programs" to try out things that will be neccesary for a big game I want to make at some point.
The game will be a sandbox game, with lots of building blocks and items, thus I want the functions updating and drawing those to be very modular.
Now, in one of my test programs I made a block called a "hopper", this block had 1 inventory slot. It would suck items from a container above it, and put it in a container in the way it was facing (left, right or down).
The thing is, the hopper code worked something along the lines of this:
Code: Select all
--Not "real" code, just want to show the idea.
function hopper.suckItem()
container = map.getName(hopperPosition, hopperPosition-1)
if container then
containerId = map.getId(hopperPosition, hopperPosition-1)
--So here it found out about the type and id of the container above it...
if container == "chest" then
if not chest.getContent(containerId, 1) then --It finds out if there's nothing in the container
chest.setContent(containerId, 1, hopperContent[1]) --So it sets slot 1 of the chest to whatever was in the hopper's slot.
end
elseif container == "furnace" then
if not furnace.getContent(containerId, 1) then
furnace.setContent(containerId, 1, hopperContent[1])
end
elseif container == "hopper" then
if not hopper.getContent(containerId, 1) then
hopper.setContent(containerId, 1, hopperContent[1])
end
end --And it would set its own content to nothing and all, but I didnt include that.
end
end
So, I was wondering if there's a way to make it do something like this:
Code: Select all
container = map.getName(hopperPosition, hopperPosition-1)
if container then
containerId = map.getId(hopperPosition, hopperPosition-1)
if not container.getContent(containerId, 1) then
container.setContent(containerId, 1, hopperContent[1])
end
end
I have a suspission this might be possible with self, but I'm really not sure how to.
Any help would be greatly appriciated.