When I click on the potion it does not give me it’s name like the enemies or gems do.
My code isn’t working I would appreciate any help. It is LUA
-- Defeat the ogres and cure the hero.
-- The pet is your only hope.
local function onSpawn()
while true do
local enemy = pet:findNearestByType("munchkin")
if (enemy and pet:isReady("chase")) then
pet:chase(enemy)
end
-- Find and fetch a "potion":
local potion = pet:findNearestByType("potion")
if potion then
pet:fetch(potion)
end
end
end
-- Assign "onSpawn" handler on the "spawn" event:
pet:on("spawn", onSpawn())
Pretty sure it is a bug with the level. If I comment out the potion part the code runs. The game is not showing name for potion so my pet can’t find one.
Also, everytime I go back to this level there is a single return statement before my function which causes my to get an error message while loading into the level saying that there is an infinite loop
return --Commented out to stop infinite loop.
-- Defeat the ogres and cure the hero.
-- The pet is your only hope.
local function onSpawn()
while true do
local enemy = pet:findNearestByType("munchkin")
if (enemy and pet:isReady("chase")) then
pet:chase(enemy)
end
-- pet:moveXY(13,60)
--pet:moveXY(hero.pos.x,hero.pos.y)
-- Find and fetch a "potion":
local potion = pet:findNearestByType("potion")
if potion then
pet:fetch(potion)
end
end
end
-- Assign "onSpawn" handler on the "spawn" event:
pet:on("spawn", onSpawn())
it should be pet.onSpawn("spawn", onSpawn). The second argument should be a function, onSpawn() is the function result. For your function onSpawn() == null.
My pet says “can’t carry that” My hero is never cured.
/ The pet is your only hope.
function onSpawn(e) {
while(true) {
var enemy = pet.findNearestByType(“munchkin”);
if (enemy && pet.isReady(“chase”)) {
pet.chase(enemy);
}
// Find and fetch a “potion”:
var potion = pet.findNearestByType(“potion”);
if (potion) {
pet.fetch(“potion”);
}
}
}
// Assign “onSpawn” handler on the “spawn” event:
pet.on(“spawn”, onSpawn);Preformatted text
Please learn to post your code correctly. The way it is now, we can’t see the structure. Help us help you. It’s very easy to do and just takes a tiny bit of effort. Please read this topic and format your code again correctly
Taking a stab at it without proper formatting. I noticed that when you call the pet.fetch method you don’t use the variable potion that you created. Remove the quotations to use the variable and you might be good to go unless there are other formatting problems we can’t see now.
I’m assuming this was meant for me. As a noob, I was both having problems understanding what I came here to ask for help for, and for your response.
I’ve edited my original text. I don’t really see the difference, but thanks for pointing out what little I could have done in order to format my code properly.
Were you able to point out something as helpful as Brooksy has then that would have been nicer.
No, you put your code inside the backticks. Like this:
```
/ The pet is your only hope.
function onSpawn(e) {
while(true) {
var enemy = pet.findNearestByType(“munchkin”);
if (enemy && pet.isReady(“chase”)) {
pet.chase(enemy);
}
// Find and fetch a “potion”:
var potion = pet.findNearestByType(“potion”);
if (potion) {
pet.fetch(“potion”);
}
}
}
// Assign “onSpawn” handler on the “spawn” event:
pet.on(“spawn”, onSpawn);
```
and it will look like:
/ The pet is your only hope.
function onSpawn(e) {
while(true) {
var enemy = pet.findNearestByType("munchkin");
if (enemy && pet.isReady("chase")) {
pet.chase(enemy);
}
// Find and fetch a "potion":
var potion = pet.findNearestByType("potion");
if (potion) {
pet.fetch("potion");
}
}
}
// Assign "onSpawn" handler on the "spawn" event:
pet.on("spawn", onSpawn);
As chaboi said, you haven’t actually done it right yet, but let me explain why you have to do it.
The main difference between formatted and not formatted code is the indents. Normal text doesn’t keep the indents you have in code, without them it’s extremely hard to read (especially in Javascript) and for all we know your error could lie in your indentation, or placement of brackets.
Here’s an example of how non-formatted code can be confusing:
while(true) {
var enemy = hero.findNearestEnemy();
if(enemy) {
hero.attack(enemy);
if(enemy.health > 100 && hero.isReady(“bash”)) {
hero.bash(enemy);
}
}
}
This could be many different things (Well, there’s only one error free way to write it, but if it was free of errors, why would any one post it here?):
# this is the most obvious version (because it's (syntax) error free, although it's not a great program)
while(true) {
var enemy = hero.findNearestEnemy();
if(enemy) {
hero.attack(enemy);
if(enemy.health > 100 && hero.isReady("bash")) {
hero.bash(enemy);
}
}
}
# This code makes more sense, but contains some errors:
while(true) {
var enemy = hero.findNearestEnemy();
if(enemy) {
hero.attack(enemy);
if(enemy.health > 100 && hero.isReady("bash")) { #what if there wasn't an enemy?
hero.bash(enemy);
} # this should be after "hero.attack(enemy)"
}
}
So can you see what I’m saying here?
I do agree that sometimes one can help just by looking at unformatted code (like Brooksy), but it would be a lot nicer of you to try and make it easier for us. All we want to do is help you, that’s why were here.
One more thing: In future try and be more: a) polite (take alex2 as an example, if he doesn’t stop being rude, he might get muted) and b) more open to advice.
Thanks,