ID:2945357
 
(See the best response by Lummox JR.)
Code:
world
mob=/mob/player
mob/player
icon='icons.dmi'
icon_state="player"
turf
icon='icons.dmi'
snow
icon_state="snow"
mob/Login()
if(LoadFile())
else
loc=locate(1,1,1)
mob/Logout()
SaveFile()
mob/proc/SaveFile()
if(fexists("Players/[ckey].sav")) fdel("Players/[ckey].sav")
var/savefile/F=new("Players/[ckey].sav")
F["LastX"]<<src.x
F["LastY"]<<src.y
F["LastZ"]<<src.z
mob/proc/LoadFile()
if(fexists("Players/[ckey].sav"))
var/savefile/F=new("Players/[ckey].sav")
loc=locate(F["LastX"],F["LastY"],F["LastZ"])
return 1


Problem description:
In the process of writing my own code from scratch, I discovered two key issues:

I am currently unable to utilize the world/New() or mob/New() procedures to create a new mob and transfer control of it to the user's client. While I have successfully implemented the blocking of turf generation from world/New(), I am also attempting to spawn a new mob as part of this process.
When using mob/Login() to create a new mob and swap client.mob, it results in the spawning of infinite mobs. ALSO I adapted Falacy's save/load tutorial to match my own coding style and found that it works with just var/savefile/F = new("[ckey].sav"). However, I had to include return 1 at the end of the Load procedure for it to function properly.


Best response
The key var saves with a mob by default. When a mob loads with a key, it will automatically pull the matching client, if any, into that mob. This is one of the reasons it's also important to make sure no other mob references are accidentally saved with your character, and to use /tmp on vars that need it.

The loop you're experiencing is because Login/Logout are called when you switch mobs.

I would advise switching to a different tutorial. Saving/loading xyz coords should be done inside of Read and Write anyway, not outside.
To maintain optimal game performance, it's crucial to avoid issues like infinite loops in the "world new" function, which can potentially crash the game. Additionally, balancing map sizes is challenging; having a 10x10 and a 50x50 map requires careful design, such as using a dense, opaque box around the smaller map to prevent overlap or interference. Furthermore, the "walk_to" function may not have sufficient sleep intervals between instructions, leading to incomplete pathing actions, which need to be addressed for smoother navigation.
In response to Ridkacez1
I don't believe you are a bot. But why do you keep posting Ai generated paragraphs?
for(var/obj/item/I in usr.client.screen)


Apologies for relying on AI; it's a tool I use for my well-being. Over the course of two to three sessions, I've completed the following code. I'm sharing it because I managed to implement key functionalities like hand-swapping, picking up and dropping items, depositing and withdrawing from inventory, targeting animals, and interacting with NPCs. Additionally, when retrieving a water bottle from inventory, it automatically triggers a drinking action. However, I feel that my code is somewhat disorganized, and I'm concerned that I'm not adhering to best practices, particularly in how I handle item usage and controls. Managing each hand and item individually seems overly complex, and I believe there’s a more efficient approach through procedural methods to simplify the process.


turf
grass
icon = 'grass.dmi'
icon_state = "grass"
mob/var
hand_selected
left_hand_item
right_hand_item

obj/hud
Click()
if(istype(src, /obj/hud/Left_Hand))
usr.hand_selected="left_hand"
src.icon_state="left_handE"
for(var/obj/hud/H in usr.client.screen)
if(istype(H,/obj/hud/Right_Hand))
H.icon_state="right_hand"
if(istype(src, /obj/hud/Right_Hand))
usr.hand_selected="right_hand"
src.icon_state="right_handE"
for(var/obj/hud/H in usr.client.screen)
if(istype(H,/obj/hud/Left_Hand))
H.icon_state="left_hand"
Left_Hand
icon = 'hudBase.dmi'
icon_state="left_hand"
screen_loc="2,1"
Right_Hand
icon = 'hudBase.dmi'
icon_state="right_hand"
screen_loc="1,1"
Drop_Item
icon = 'hudBase.dmi'
icon_state="drop_item"
screen_loc="4,1"
Click()
if(usr.hand_selected=="right_hand")
if(usr.right_hand_item)
var/obj/item/A=usr.right_hand_item
usr<<"You drop the [A]."
usr.client.screen-=usr.right_hand_item
A.Move(usr.loc)
usr.right_hand_item=null
else
usr<<"You have nothing in your right hand to drop."
if(usr.hand_selected=="left_hand")
if(usr.left_hand_item)
var/obj/item/A=usr.left_hand_item
usr<<"You drop the [A]."
usr.client.screen-=usr.left_hand_item
A.Move(usr.loc)
usr.left_hand_item=null
else
usr<<"You have nothing in your left hand to drop."
Inventory
icon='hudBase.dmi'
icon_state="inventory"
screen_loc="6,1"
Click()
if(!usr.hand_selected)
return
if(usr.hand_selected=="right_hand")
if(!usr.right_hand_item)
usr<<"You have nothing in your hand."
if(usr.hand_selected=="left_hand")
if(!usr.left_hand_item)
usr<<"You have nothing in your hand."
for(var/obj/hud/H in usr.client.screen)
for(var/obj/item/I in usr.client.screen)
if(istype(H, /obj/hud/Right_Hand)&&usr.hand_selected=="right_hand")
if(I==usr.right_hand_item)
usr.client.screen-=I
I.Move(usr)
usr.right_hand_item=null
usr<<"You put the item in your inventory."

if(istype(H, /obj/hud/Left_Hand)&&usr.hand_selected=="left_hand")
if(I==usr.left_hand_item)
usr.client.screen-=I
I.Move(usr)
usr.left_hand_item=null
usr<<"You put the item in your inventory."

world
mob=/mob/player
mob/player
icon='player.dmi'
mob
Stat()
statpanel("Inventory",usr.contents)
mob
Login()
..()
var/obj/hud/Left_Hand=new/obj/hud/Left_Hand
src.client.screen+=Left_Hand
var/obj/hud/Right_Hand=new/obj/hud/Right_Hand
src.client.screen+=Right_Hand
var/obj/hud/Drop_Item=new/obj/hud/Drop_Item
src.client.screen+=Drop_Item
var/obj/hud/Inventory=new/obj/hud/Inventory
src.client.screen+=Inventory
obj
item
Click()
if(usr.hand_selected=="right_hand")
if(src==/obj/item/bottle)
return
if(src==usr.right_hand_item)
usr<<"This is the same item."
else if(!usr.right_hand_item&&usr.left_hand_item==src)
usr.right_hand_item=src
src.loc=null
usr<<"You swap the item to your other hand."
usr.left_hand_item=null
src.screen_loc="1,1"
usr.client.screen+=src
else if(usr.right_hand_item)
usr<<"You already have something in your hand."
else if(!usr.right_hand_item&&src.loc!=usr)
usr.right_hand_item=src
src.loc=null
usr<<"You pickup the item."
src.screen_loc="1,1"
usr.client.screen+=src
else if(!usr.right_hand_item&&src.loc==usr)
usr.right_hand_item=src
src.loc=null
usr<<"You pull the item out from your inventory."
src.screen_loc="1,1"
usr.client.screen+=src
if(usr.hand_selected=="left_hand")
if(src==/obj/item/bottle)
return
if(src==usr.left_hand_item)
usr<<"This is the same item."
else if(!usr.left_hand_item&&usr.right_hand_item==src)
usr.left_hand_item=src
src.loc=null
usr<<"You swap the item to your other hand."
usr.right_hand_item=null
src.screen_loc="2,1"
usr.client.screen+=src
else if(usr.left_hand_item)
usr<<"You already have something in your hand."
else if(!usr.left_hand_item&&src.loc!=usr)
usr.left_hand_item=src
src.loc=null
usr<<"You pickup the item."
src.screen_loc="2,1"
usr.client.screen+=src
else if(!usr.left_hand_item&&src.loc==usr)
usr.left_hand_item=src
src.loc=null
usr<<"You pull the item out from your inventory."
src.screen_loc="2,1"
usr.client.screen+=src
atom
Click()
..()

for(var/mob/E in src)
if(istype(E, /mob/animal))
if(!usr.target)
E.overlays+=/obj/target
usr.target=E
usr<<"You target [E]."
while(usr.target)
for(var/mob/animal/A in view(1))
if(A==usr.target)
world<<"You attack [A]."
A.HP-=5
A.Die()
sleep(10)
else if(usr.target==E)
E.overlays-=/obj/target
usr.target=null
usr<<"You untarget [E]."
else if(istype(E, /mob/npc))
world<<"[E.dialogue[1]]"
mob/var/target=null
mob/var/dialogue=list()
mob/var/list/loot_list=list()
mob/npc
icon='npc.dmi'
npc1
icon_state="npc1"
dialogue=list(
"Howdy! You look like a fine traveller.")

obj/item
meat
icon='meat.dmi'
icon_state="meat"
mob/var/thirst
mob/var/HP
mob/var/hunger
mob/proc
Die()
if(HP<=0)
for(var/A in src.loot_list)
if(prob(loot_list[A]+(src.hunger)))
new A(src.loc)
world<<"The [src] has died."
del(src)



obj/target
icon='target.dmi'


mob/animal/cow
icon='cow.dmi'
HP=10
loot_list=list(/obj/item/meat=0)
hunger=0
proc
Walk()
var/direction=pick(NORTH,SOUTH,EAST,WEST)
step(src,direction)
Moo()
if(prob(5))
world<<"Moo!"
Graze()
if(prob(10))
world<<"The cow eats grass."
src.hunger+=10
if(src.hunger>100)
src.hunger=100
New()
..()
while(src)
Walk()
Moo()
Graze()
sleep(10)
obj/item/bottle
icon='bottle.dmi'
var/water=0
Click()
..()
for(var/obj/item/bottle/A in usr.client.screen)
if(usr.right_hand_item==A)
if(usr.hand_selected=="right_hand")
//when you withdraw a bottle of water from your inventory it drinks it immediately
//checking if it's in the inventory doesn't seem to fix this
if(A.water>0)
world<<"You drink the water."
usr.thirst+=A.water
A.icon_state=null
A.water=0
if(usr.left_hand_item==A)
if(usr.hand_selected=="left_hand")
if(A.water>0)
world<<"You drink the water."
usr.thirst+=A.water
A.icon_state=null
A.water=0
turf/water
icon='water.dmi'
density=1
Click()
..()
for(var/obj/item/bottle/E in usr.client.screen)
if(usr.right_hand_item==E)
if(usr.hand_selected=="right_hand")
if(!E.water)
E.icon_state="water"
E.water=100
else
world<<"The bottle is already full."
if(usr.left_hand_item==E)
if(usr.hand_selected=="left_hand")
if(!E.water)
E.icon_state="water"
E.water=100
else
world<<"The bottle is already full."


This code manages hand-swapping, inventory operations, and various other functionalities within the environment. While it allows for actions like swapping hands, dropping items, depositing and withdrawing from the inventory, as well as targeting animals and interacting with NPCs, I believe I could streamline these processes by utilizing more focused functions. Specifically, using procedures to handle item usage could help reduce the complexity of managing left and right hand actions.

Thank you in advance for your understanding. If you have any suggestions or advice, feel free to leave a reply, and I’ll be sure to review it.
This is what I mean... Is there a way to handle all these functions procedurally?
 mob
icon='Icons.dmi'
person
icon_state="person"
Login()
..()
Load_Hud()
var/hand_selected
world
mob=/mob/person
turf
icon='Icons.dmi'
grass
icon_state="grass"
dirt
icon_state="dirt"
water
icon_state="water"
density=1
sand
icon_state="sand"
mob
proc
Load_Hud()
src.client.screen+=new/obj/hud/l_hand
src.client.screen+=new/obj/hud/r_hand
src.client.screen+=new/obj/hud/drop
src.client.screen+=new/obj/hud/inv
Swap_Hands()
Highlight_Hands()
if(hand_selected=="l_hand")
var/obj/hud/hud_e/A=new/obj/hud/hud_e
A.screen_loc="2,1"
client.screen+=A
if(hand_selected=="r_hand")
var/obj/hud/hud_e/A=new/obj/hud/hud_e
A.screen_loc="1,1"
client.screen+=A

mob/verb/Test()
Highlight_Hands()
mob/person
hand_selected="r_hand"



obj
icon='Icons.dmi'
hud
l_hand
icon_state="hud_l"
screen_loc="2,1"
r_hand
icon_state="hud_r"
screen_loc="1,1"
drop
icon_state="hud_drop"
screen_loc="4,1"
inv
icon_state="hud_inv"
screen_loc="6,1"
hud_e
icon_state="hud_e"

Login to reply.