Coding advice needed

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
User avatar
WaspUK1966
Posts: 135
Joined: Sat Aug 09, 2014 9:17 am

Coding advice needed

Post by WaspUK1966 »

I`m working on a game, and have a creature that, when it attacks, there is a random chance of it taking one of your items that are being worn or held. The section of code I have written for this works as intended (see below):

Code: Select all

onDealDamage = function(self, champion, damage)
		x = math.random(1,6)
		y = math.random(1,10)
		if x == 3 and champion:getItem(y) ~= 0 then
		champion:removeItem(y)
		hudPrint("An item has been absorbed!")
		playSound("consume_food")
		end
		end
I would like to alter the code so that it actually prints out the name of the item that has been taken, rather than the general "An item has been absorbed!" message. Ive tried diff variables in all sorts of combinations, but cant quite get it to work. Probably something simple I need to do, but cant see the wood because of the trees, so to speak. Any advice?
George
Last edited by WaspUK1966 on Thu Dec 04, 2014 9:34 pm, edited 1 time in total.
User avatar
sapientCrow
Posts: 608
Joined: Sun Apr 22, 2012 10:57 am

Re: Coding advice needed

Post by sapientCrow »

I am no coding guru but have you tried printing y?
as that is the variable holding the item name.
Banaora
Posts: 162
Joined: Wed Jun 05, 2013 9:39 am

Re: Coding advice needed

Post by Banaora »

Hey Wasp,

this should do it:

Code: Select all

onDealDamage = function(self, champion, damage)
  x = math.random(1,6)
  y = math.random(1,10)
  it = champion:getItem(y)
  if x == 3 and it ~= 0 then
    hudPrint(it.getUIName() .. " has been absorbed!")
    champion:removeItem(y)
    playSound("consume_food")
  end
end
User avatar
WaspUK1966
Posts: 135
Joined: Sat Aug 09, 2014 9:17 am

Re: Coding advice needed

Post by WaspUK1966 »

Hi. Tried your code. Keeps crashing to desktop when damage done. I`ve tried using UIName myself but couldn't get it to work properly. Still cant figure it out..

George
User avatar
Isaac
Posts: 3179
Joined: Fri Mar 02, 2012 10:02 pm

Re: Coding advice needed

Post by Isaac »

Code: Select all

	onDealDamage = function(self, champion, damage)
				local x = math.random(1,6)
				local y = math.random(1,10)
				local it = champion:getItem(y)
				  if x == 3 and it ~= nil then
					hudPrint(it:getUiName().." has been absorbed!")
					champion:removeItemFromSlot(y)
					playSound("consume_food")
				  end
				end,
User avatar
WaspUK1966
Posts: 135
Joined: Sat Aug 09, 2014 9:17 am

Re: Coding advice needed

Post by WaspUK1966 »

Hi. Tried that. Still crashes to desktop. If its any help, I`ve cloned a green slime but just altered the ondealDamage part and attack power and accuracy. Would this cause the problem? Without using the "getUIName" part in the code, the items are removed and the message "An item has been absorbed" is printed, no problem. Its when I try to print out the name of the actual item removed (as you have written) that problems arise and it crashes. Is there a bug with the getUIName function I wonder?
George
User avatar
Isaac
Posts: 3179
Joined: Fri Mar 02, 2012 10:02 pm

Re: Coding advice needed

Post by Isaac »

WaspUK1966 wrote:Hi. Tried that. Still crashes to desktop. If its any help, I`ve cloned a green slime but just altered the ondealDamage part and attack power and accuracy. Would this cause the problem? Without using the "getUIName" part in the code, the items are removed and the message "An item has been absorbed" is printed, no problem. Its when I try to print out the name of the actual item removed (as you have written) that problems arise and it crashes. Is there a bug with the getUIName function I wonder?
George
But it works for me... [ https://www.dropbox.com/s/slvwcd9va5l8n ... r.avi?dl=0 ]

*One thing though is that it's~ getUiName() [lowercase i].
User avatar
WaspUK1966
Posts: 135
Joined: Sat Aug 09, 2014 9:17 am

Re: Coding advice needed

Post by WaspUK1966 »

Yeh, just checked out your vid. That's what I want it to do..I`ve copied/pasted your code exactly, but it just crashes to desktop when it is supposed to remove an item and print the message. By the way, what's the diff between removeItem() and removeItemFromSlot(), as they both seem to work?

george
User avatar
Isaac
Posts: 3179
Joined: Fri Mar 02, 2012 10:02 pm

Re: Coding advice needed

Post by Isaac »

WaspUK1966 wrote: Yeh, just checked out your vid. That's what I want it to do..I`ve copied/pasted your code exactly, but it just crashes to desktop when it is supposed to remove an item and print the message.
Try adding this monster to your map, and see if it crashes.

Code: Select all

defineObject{
	name = "klepto_beast",
	baseObject = "zarchton",
	components = {
					{
				class = "MonsterAttack",
				name = "basicAttack",
				attackPower = 10,
				accuracy = 15,
				cooldown = 4,
				sound = "zarchton_attack",

				onDealDamage = function(self, champion, damage)
				local x = 3 --math.random(1,6)
				local y = 1 --math.random(1,10)
				local it = champion:getItem(y)
				  if x == 3 and it ~= nil then
					hudPrint(it:getUiName().." has been absorbed!")
					champion:removeItemFromSlot(y)
					playSound("consume_food")
				  end
				end,

		},

}
}
By the way, what's the diff between removeItem() and removeItemFromSlot(), as they both seem to work?

george
RemoveItemFromSlot() accepts the slot# as target, deleting any item in that slot; while removeItem() accepts the item itself ~specifically and deletes it.
User avatar
WaspUK1966
Posts: 135
Joined: Sat Aug 09, 2014 9:17 am

Re: Coding advice needed

Post by WaspUK1966 »

That code. Are you talking about LOG 2 here, as I don't recognise the creature or class? I`m coding a LOG 1 game...
Ive tried everything. Cant get the UiName or UIName to work, so ive changed my code now so that it prints out the name of the champion who has lost something instead:

Code: Select all

cloneObject{
	name = "stealer",
	baseObject = "scavenger",
	health = 60,
	exp = 160,
	attackPower = 12,
	accuracy = 10,
	   onDealDamage = function(self, champion, damage)
            local x = math.random(1,3)
            local y = math.random(1,10)
	    local name1 = champion:getName() 
              if x == 1 and champion:getItem(y) ~= nil then
               hudPrint(""..name1.." has lost an item!...")
               champion:removeItem(y)
               playSound("consume_food")
              end
            end


}
But if anyone can figure a way that works for printing out the item name, please let me know!

George
Last edited by WaspUK1966 on Thu Dec 04, 2014 4:00 pm, edited 1 time in total.
Post Reply