Page 1 of 2

Coding advice needed

Posted: Sat Nov 29, 2014 11:58 am
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

Re: Coding advice needed

Posted: Sat Nov 29, 2014 10:29 pm
by sapientCrow
I am no coding guru but have you tried printing y?
as that is the variable holding the item name.

Re: Coding advice needed

Posted: Tue Dec 02, 2014 7:03 am
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

Re: Coding advice needed

Posted: Wed Dec 03, 2014 12:07 am
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

Re: Coding advice needed

Posted: Wed Dec 03, 2014 2:07 am
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,

Re: Coding advice needed

Posted: Thu Dec 04, 2014 12:06 am
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

Re: Coding advice needed

Posted: Thu Dec 04, 2014 12:17 am
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].

Re: Coding advice needed

Posted: Thu Dec 04, 2014 12:58 am
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

Re: Coding advice needed

Posted: Thu Dec 04, 2014 2:58 am
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.

Re: Coding advice needed

Posted: Thu Dec 04, 2014 1:43 pm
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